试图从archiver获取Buffer作为输出。 archiver只能将Stream用作管道,因此我决定使用stream-buffers包:
var archiver = require('archiver');
var streamBuffers = require('stream-buffers');
var myWritableStreamBuffer = new streamBuffers.WritableStreamBuffer({
initialSize: (10000 * 1024), // start at 100 kilobytes.
incrementAmount: (1000 * 1024) // grow by 10 kilobytes each time buffer overflows.
});
var archive = archiver('zip', {
gzip: true,
zlib: { level: 9 } // Sets the compression level.
});
archive.on('error', function(err) {
console.log(err);
throw err;
});
archive.pipe(myWritableStreamBuffer);
archive.append(buffer1, { name: title1 });
archive.append(buffer2, { name: title2 });
archive.finalize();
console.log(myWritableStreamBuffer.getContentsAsString("base64"));
它返回false(我相信意味着缓冲区为空) 我正在尝试处理“数据”上的事件,但似乎没有任何内容写入流中