6

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

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

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

4

3 回答 3

8

您可以使用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();
于 2015-12-17T14:52:29.650 回答
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();
于 2015-12-18T15:01:05.367 回答
3

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

假设文件夹/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;
  });
}
于 2016-10-07T21:55:18.313 回答