node-archiver:归档多个目录

sst*_*oss 4 zip node.js node-archiver

当你知道它们的路径时,是否可以存档多个目录?让我们说:['/dir1','dir2', .., 'dirX'].我现在正在做的是将目录复制到一个目录中,让我们说:/dirToZip并执行以下操作:

var archive = archiver.create('zip', {});
archive.on('error', function(err){
    throw err;
});
archive.directory('/dirToZip','').finalize(); 
Run Code Online (Sandbox Code Playgroud)

是否有方法将目录附加到存档中而不是批量需要的特定模式?提前致谢!

Ale*_*rev 7

你可以用bulk(mappings).尝试:

var output = fs.createWriteStream(__dirname + '/bulk-output.zip');
var archive = archiver('zip');

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('error', function(err) {
    throw err;
});

archive.pipe(output);

archive.bulk([
    { expand: true, cwd: 'views/', src: ['*'] },
    { expand: true, cwd: 'uploads/', src: ['*'] }
]);

archive.finalize();
Run Code Online (Sandbox Code Playgroud)

更新

或者你可以更容易地做到这一点:

var output = fs.createWriteStream(__dirname + '/bulk-output.zip');
var archive = archiver('zip');

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('error', function(err) {
    throw err;
});

archive.pipe(output);

archive.directory('views', true, { date: new Date() });
archive.directory('uploads', true, { date: new Date() });

archive.finalize();
Run Code Online (Sandbox Code Playgroud)


sst*_*oss 3

对于遇到同样问题的每个人,最终对我有用的是:假设您有以下结构:

/用户/x/桌面/tmp

| __ 目录1

| __ 目录2

| __ 目录3

var baseDir = '/Users/x/Desktop/tmp/';
var dirNames = ['dir1','dir2','dir3']; //directories to zip

var archive = archiver.create('zip', {});
archive.on('error', function(err){
    throw err;
});

var output = fs.createWriteStream('/testDir/myZip.zip'); //path to create .zip file
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);

dirNames.forEach(function(dirName) {
    // 1st argument is the path to directory 
    // 2nd argument is how to be structured in the archive (thats what i was missing!)
    archive.directory(baseDir + dirName, dirName);
});
archive.finalize();
Run Code Online (Sandbox Code Playgroud)