使用任何节点模块在内存中创建 ZIP 文件

Tor*_*eto 6 zip stream node.js

是否有任何节点模块可以在内存中创建 zip?(我不想将 zip 文件保存在磁盘上),以便我们可以将这个创建的 zip 文件发送到其他服务器(从内存)。做这个的最好方式是什么?这是我的例子:

var file_system = require('fs');
var archiver = require('archiver');
var dirToCompress = 'directoryToZIP';

module.exports = function (req, res, next) {
var archive = archiver.create('zip', {});
archive.on('error', function (err) {
    throw err;
});

    var output = file_system.createWriteStream('/testDir/myZip.zip',{flags:'a'});//I don't want this line
    output.on('close', function () {
        console.log(archive.pointer() + ' total bytes');
        console.log('archiver has been finalized and the output file descriptor has closed.');
    });

    archive.pipe(output);

    archive.directory(dirToCompress);

    archive.finalize();

};
Run Code Online (Sandbox Code Playgroud)

car*_*ram 7

我正在使用Adm-Zip

// create archive
var zip = new AdmZip();

// add in-memory file
var content = "inner content of the file";
zip.addFile("test.txt", Buffer.alloc(content.length, content), "entry comment goes here");

// add file
zip.addLocalFile("/home/me/some_picture.png");

// get in-memory zip
var willSendthis = zip.toBuffer();

Run Code Online (Sandbox Code Playgroud)


Tro*_*ott 3

如果 GZip 可以,您可以使用内置zlib模块,甚至不必加载外部模块。

const gzip = zlib.createGzip();

// If you have an inputStream and an outputStream:

inputStream.pipe(gzip).pipe(outputStream);
Run Code Online (Sandbox Code Playgroud)

对于 Zip 而不是 GZip,您可以查看archiverzip-stream