node-archiver:归档多个目录

node-archiver: Archive multiple directories

是否可以在知道路径的情况下对多个目录进行归档?比方说:['/dir1','dir2', .., 'dirX']。我现在正在做的是将目录复制到一个目录中,比方说:/dirToZip 并执行以下操作:

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

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

您可以使用 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();

已更新

或者你可以做得更简单:

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();

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

/Users/x/Desktop/tmp

| __ dir1

| __ dir2

| __ dir3

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();

@sstauross 的回答非常适合我。我添加了更多代码以允许将目录和文件混合添加到压缩过程中:

假设文件夹 /Users/x/baseFolder 具有以下结构:

 /Users/x/baseFolder
 | __ dir1
   | __ file1.txt 
   | __ file2.txt
 | __ dir2
   | __ file3.txt
   | __ file4.txt
 | __ dir3
   | __ file5.txt
   | __ file6.txt
   | __ file7.txt
 | __ file8.txt
 | __ file9.txt
 | __ file10.txt

然后我可以执行以下操作来创建具有以下结构和内容的 /Users/x/baseFolder/result.zip 文件夹:

/Users/x/baseFolder/result.zip 
 | __ dir1
   | __ file1.txt 
 | __ dir2
   | __ file3.txt
   | __ file4.txt
 | __ dir3
   | __ file6.txt
   | __ file7.txt
 | __ file8.txt
 | __ file9.txt


function zipFolder(baseFolder) {
  var archive = archiver('zip');

  var fileNames = [
    'dir1/file1.txt',
    'dir3/file6.txt',
    'dir3/file7.txt',
    'file8.txt',
    'file9.txt'
  ];
  var folderNames = [
    'dir2',
  ]

  var output = fs.createWriteStream(path.join(baseFolder, "result.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);

  for (i = 0; i < fileNames.length; i++) {
    var stream = fs.readFileSync(path.join(baseFolder, fileNames[i]));
    archive.append(stream, { name: fileNames[i] });
  }
  for (i = 0; i < folderNames.length; i++) {
    archive.directory(path.join(baseFolder, folderNames[i]), folderNames[i]);
  }

  archive.finalize(function (err, bytes) {
    if (err) throw err;
  });
}