最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何使用yazl压缩缓冲区?

IT培训 admin 4浏览 0评论

如何使用yazl压缩缓冲区?

如何通过yazl压缩/解压缩缓冲区?

我不想创建/保存zip文件,只是压缩缓冲区并将其转发到另一个服务。任何示例代码都会有所帮助

var yazl = require("yazl");

var buf = fs.readFileSync(__dirname + '/testArchive.txt');
var zipfile = new yazl.ZipFile();
zipfile.addBuffer(buf, "TEMPENTRY");
zipfile.end();

那么它在这一点上存档了吗?那么如何使用yauzl来膨胀缓冲区呢?

回答如下:

这种函数应该有帮助,手动检索流的数据包,然后将它们连接成一个单独的Buffer

function stream2buffer(readableStream) {
  return new Promise((resolve, reject) => {
    const chunks = [];
    readableStream.on('data', (chunk) => {
      chunks.push(chunk);
    }).on('end', () => {
      resolve(Buffer.concat(chunks));
    }).on('error', (err) => {
      reject(err);
    });
  });
}

然后你可以在yazl.ZipFile.outputStream上使用它,另一边在yauzl.ZipFile.openReadStream的回调提供的可读流上使用它。

Edit

我的意思更像是在yazloutputStream上使用这个函数,而不是在你要压缩的源文件上。更像是这样的:

function zipper(mapping) { // mapping is expected to be a Map here
  const handler = new yazl.ZipFile(); // yazl = require('yazl');
  for (mapItem of mapping) {
    if (typeof mapItem[1] === 'string' || mapItem[1] instanceof String) {
      handler.addFile(mapItem[1], mapItem[0]);
    } else if (mapItem[1] instanceof Buffer) {
      handler.addBuffer(mapItem[1], mapItem[0]);
    } else if (mapItem[1] instanceof stream.Readable) { // stream = require('stream');
      handler.addReadStream(mapItem[1], mapItem[0]);
    } else throw new Error('unsupported type');
  }
  handler.end();
  return stream2buffer(handler.outputStream);
}

解压缩也是如此:

function unzipper(buffer) {
  return new Promise((resolve, reject) => {
    unzippedContents = {};
    yauzl.fromBuffer(buffer, {lazyEntries: true}, (err, zip) => { // yauzl = require('yauzl')
      if (err) return reject(err);
      zip.on('entry', (entry) => {
        if (/\/$/.test(entry.fileName)) return zip.readEntry(); // no directories
        zip.openReadStream(entry, async (err, rs) => {
          try {
            if (err) throw err;
            unzippedContents[entry.fileName] = await stream2buffer(rs);
          } catch (err) {
            zip.close();
            return reject(err);
          }
          zip.readEntry();
        });
      }).on('end', () => {
        zip.close();
        resolve(unzippedContents);
      });
      zip.readEntry(); // start the process
    });
  });
}

如何使用yazl压缩缓冲区?

如何通过yazl压缩/解压缩缓冲区?

我不想创建/保存zip文件,只是压缩缓冲区并将其转发到另一个服务。任何示例代码都会有所帮助

var yazl = require("yazl");

var buf = fs.readFileSync(__dirname + '/testArchive.txt');
var zipfile = new yazl.ZipFile();
zipfile.addBuffer(buf, "TEMPENTRY");
zipfile.end();

那么它在这一点上存档了吗?那么如何使用yauzl来膨胀缓冲区呢?

回答如下:

这种函数应该有帮助,手动检索流的数据包,然后将它们连接成一个单独的Buffer

function stream2buffer(readableStream) {
  return new Promise((resolve, reject) => {
    const chunks = [];
    readableStream.on('data', (chunk) => {
      chunks.push(chunk);
    }).on('end', () => {
      resolve(Buffer.concat(chunks));
    }).on('error', (err) => {
      reject(err);
    });
  });
}

然后你可以在yazl.ZipFile.outputStream上使用它,另一边在yauzl.ZipFile.openReadStream的回调提供的可读流上使用它。

Edit

我的意思更像是在yazloutputStream上使用这个函数,而不是在你要压缩的源文件上。更像是这样的:

function zipper(mapping) { // mapping is expected to be a Map here
  const handler = new yazl.ZipFile(); // yazl = require('yazl');
  for (mapItem of mapping) {
    if (typeof mapItem[1] === 'string' || mapItem[1] instanceof String) {
      handler.addFile(mapItem[1], mapItem[0]);
    } else if (mapItem[1] instanceof Buffer) {
      handler.addBuffer(mapItem[1], mapItem[0]);
    } else if (mapItem[1] instanceof stream.Readable) { // stream = require('stream');
      handler.addReadStream(mapItem[1], mapItem[0]);
    } else throw new Error('unsupported type');
  }
  handler.end();
  return stream2buffer(handler.outputStream);
}

解压缩也是如此:

function unzipper(buffer) {
  return new Promise((resolve, reject) => {
    unzippedContents = {};
    yauzl.fromBuffer(buffer, {lazyEntries: true}, (err, zip) => { // yauzl = require('yauzl')
      if (err) return reject(err);
      zip.on('entry', (entry) => {
        if (/\/$/.test(entry.fileName)) return zip.readEntry(); // no directories
        zip.openReadStream(entry, async (err, rs) => {
          try {
            if (err) throw err;
            unzippedContents[entry.fileName] = await stream2buffer(rs);
          } catch (err) {
            zip.close();
            return reject(err);
          }
          zip.readEntry();
        });
      }).on('end', () => {
        zip.close();
        resolve(unzippedContents);
      });
      zip.readEntry(); // start the process
    });
  });
}

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论