【发布时间】:2017-10-05 09:28:35
【问题描述】:
我有 node.js 应用程序,我需要使用来自的命令压缩所有当前文件夹 并获取根目录上的 zip
为此,我想使用archiver npm 包,但我不明白以下内容:
- 我放置当前文件夹的位置(因为我想压缩所有应用程序)
- 我应该把 zip 的名称放在哪里(执行命令时应该创建的 zip)
我的应用具有以下结构
MyApp
Node_modules
server.js
app.js
package.json
arc.js
在arc.js 中我已经放置了所有的 zip 逻辑,所以我想我需要提供 zipPath(在我的例子中是'./')
和像 myZip 这样的 zip 名称……
我尝试了以下但没有成功,有什么想法吗?
var fs = require('fs');
var archiver = require('archiver');
// create a file to stream archive data to.
var output = fs.createWriteStream(__dirname + '/.');
var archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
// listen for all archive data to be written
output.on('close', function() {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
// log warning
} else {
// throw error
throw err;
}
});
// good practice to catch this error explicitly
archive.on('error', function(err) {
throw err;
});
// pipe archive data to the file
archive.pipe(output);
当我打开命令行时需要它
folder->myApp-> 运行 zip arc 并将在当前路径下创建压缩文件(在这种情况下是根目录....)
【问题讨论】:
标签: javascript node.js zip archive