我想在node.js中创建一个zip压缩文件并解压它。
我找不到任何node实现。
node-core内置了压缩功能:http://nodejs.org/api/zlib.html
使用它们:
var zlib = require('zlib');
var gzip = zlib.createGzip();
var fs = require('fs');
var inp = fs.createReadStream('input.txt');
var out = fs.createWriteStream('input.txt.gz');
inp.pipe(gzip).pipe(out);
我最终是这样做的(我使用Express)。我正在创建一个包含给定目录(SCRIPTS_PATH)中所有文件的ZIP。
我只在Mac OS X Lion上测试过,但我猜在安装了Cygwin的Linux和Windows上也可以正常工作。
var spawn = require('child_process').spawn;
app.get('/scripts/archive', function(req, res) {
// Options -r recursive -j ignore directory info - redirect to stdout
var zip = spawn('zip', ['-rj', '-', SCRIPTS_PATH]);
res.contentType('zip');
// Keep writing stdout to res
zip.stdout.on('data', function (data) {
res.write(data);
});
zip.stderr.on('data', function (data) {
// Uncomment to see the files being added
// console.log('zip stderr: ' + data);
});
// End the response on zip exit
zip.on('exit', function (code) {
if(code !== 0) {
res.statusCode = 500;
console.log('zip process exited with code ' + code);
res.end();
} else {
res.end();
}
});
});
var Archiver = require('archiver'),
fs = require('fs');
app.get('download-zip-file', function(req, res){
var archive = Archiver('zip');
archive.on('error', function(err) {
res.status(500).send({error: err.message});
});
//on stream closed we can end the request
res.on('close', function() {
console.log('Archive wrote %d bytes', archive.pointer());
return res.status(200).send('OK').end();
});
//set the archive name
res.attachment('file-txt.zip');
//this is the streaming magic
archive.pipe(res);
archive.append(fs.createReadStream('mydir/file.txt'), {name:'file.txt'});
//you can add a directory using directory function
//archive.directory(dirPath, false);
archive.finalize();
});
我发现自己编写7-zip的包装器最容易,但您也可以轻松地使用zip或任何命令行zip工具,只要在运行时环境中可用。这个特定的模块只做一件事:压缩一个目录。
const { spawn } = require('child_process');
const path = require('path');
module.exports = (directory, zipfile, log) => {
return new Promise((resolve, reject) => {
if (!log) log = console;
try {
const zipArgs = ['a', zipfile, path.join(directory, '*')];
log.info('zip args', zipArgs);
const zipProcess = spawn('7z', zipArgs);
zipProcess.stdout.on('data', message => {
// received a message sent from the 7z process
log.info(message.toString());
});
// end the input stream and allow the process to exit
zipProcess.on('error', (err) => {
log.error('err contains: ' + err);
throw err;
});
zipProcess.on('close', (code) => {
log.info('The 7z exit code was: ' + code);
if (code != 0) throw '7zip exited with an error'; // throw and let the handler below log it
else {
log.info('7zip complete');
return resolve();
}
});
}
catch(err) {
return reject(err);
}
});
}
假设您已将上述代码保存到zipdir.js
中,请像这样使用它。第三个log
参数是可选的。如果您有自定义日志记录器,请使用它。或者完全删除我讨厌的日志语句。
const zipdir = require('./zipdir');
(async () => {
await zipdir('/path/to/my/directory', '/path/to/file.zip');
})();
您可以使用edge.js模块,在node.js和.NET进程之间支持互操作性,然后调用.NET框架的ZipFile类,以便您可以操纵ZIP归档文件。这里有一个使用edge.js创建ZIP包的完整示例。还可以查看使用edge.js解压缩示例。