尝试将Archiver与流一起使用,但附加操作无效

时间:2019-10-22 07:08:09

标签: node.js archiverjs

使用存档器库处理Node。 我想添加缓冲区并创建存档流。似乎附加文件无法正常工作,并且归档文件也未附加。

这是功能

async function archiveFiles(files: Buffer[]): Promise<Buffer> {
  const archive = archiver("zip", {
    zlib: {level: 9} // Sets the compression level.
  });

  archive.on("warning", wrn => logger.warn(wrn));
  archive.on("error", err => {
    logger.warn(err);
    throw err;
  });
  const outputStreamBuffer = new streamBuffers.WritableStreamBuffer({
    initialSize: (1000 * 1024),   // start at 1 Gig
    incrementAmount: (100 * 1024) // grow by 100 kilobytes each time buffer overflows.
  });
  archive.pipe(outputStreamBuffer);
  // to be continued.....
  const date = new Date();
  const filesBaseName = `report_${date.toISOString().slice(0, 10)}--${date.getHours()}-${date.getMinutes()}_`;
  files.map((b, i) => {
    // const tempF = Buffer.from(b);
    const name = filesBaseName + i;
    archive.append(b, {name});
  });

  logger.debug(archive.pointer() + " total bytes");
  archive.finalize();
  outputStreamBuffer.end();
  logger.debug("archiver has been finalized and the output file descriptor has closed.");
  return outputStreamBuffer.getContents();
}

文件的值是一个具有两个缓冲区大小(199000字节)的数组 当我调试它时,记录器将写入“ 0个总字节”。 即使_entriesCount(在调试中)鞋子2看起来实际上没有数据 而且,当我查看outputStreamBuffer.getContents()时,得到的是“ false”,而不是实际的zip内容。

0 个答案:

没有答案