node.js - 使用任何 Node 模块在内存中创建 ZIP 文件

标签 node.js stream zip

是否有任何 Node 模块可以在内存中创建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();

};

最佳答案

我正在使用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();

关于node.js - 使用任何 Node 模块在内存中创建 ZIP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42992604/

相关文章:

node.js - 具有多个身份验证提供程序的 passport.js?

python - 在 Python 中使用音频流 RTMP 通过管道和 OpenCV 到 FFmpeg

android - 在 Android gstreamer 中使用多个进程!!

c# - 如何使用 C# 将 base64 值从数据库转换为流

python - 增量压缩和一次性压缩有什么区别?

javascript - Express.js - Helmet.js 和其他中间件,以及安装的应用程序?

javascript - React Native x OneSignal 不显示通知

linux - 在 linux 中压缩和解压缩目录及其文件

java - 可以捕获 java.util.zip.ZipError 吗?

javascript - client.startTyping 不是一个函数(Discord.JS)