我想使用Node.js从现有文件夹创建一个zip文件,并保留其结构。
我希望有一个简单的模块来实现这种操作:
archiver.create("../folder", function(zipFile){
console.log('et viola');
});
但我找不到类似的东西!
我已经在谷歌上搜索了一番,目前为止最好的结果是zipstream,但据我所知,没有办法做到我想要的。我不想调用命令行实用程序,因为应用程序必须跨平台。
非常感谢任何帮助。
谢谢。
我想使用Node.js从现有文件夹创建一个zip文件,并保留其结构。
我希望有一个简单的模块来实现这种操作:
archiver.create("../folder", function(zipFile){
console.log('et viola');
});
但我找不到类似的东西!
我已经在谷歌上搜索了一番,目前为止最好的结果是zipstream,但据我所知,没有办法做到我想要的。我不想调用命令行实用程序,因为应用程序必须跨平台。
非常感谢任何帮助。
谢谢。
虽然不完全是无代码,但您可以使用node-native-zip与folder.js结合使用。用法:
function zipUpAFolder (dir, callback) {
var archive = new zip();
// map all files in the approot thru this function
folder.mapAllFiles(dir, function (path, stats, callback) {
// prepare for the .addFiles function
callback({
name: path.replace(dir, "").substr(1),
path: path
});
}, function (err, data) {
if (err) return callback(err);
// add the files to the zip
archive.addFiles(data, function (err) {
if (err) return callback(err);
// write the zip file
fs.writeFile(dir + ".zip", archive.toBuffer(), function (err) {
if (err) return callback(err);
callback(null, dir + ".zip");
});
});
});
}
var execFile = require('child_process').execFile;
execFile('zip', ['-r', '-j', zipName, pathToFolder], function(err, stdout) {
console.log(err);
logZipFile(localPath);
});