Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node-archiver: Archive multiple directories

Is it possible to archive multiple directories when you know their paths? Let's say: ['/dir1','dir2', .., 'dirX']. What I am doing now, is to copying directories in a single directory, let's say: /dirToZip and do the following:

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

Is there an approach to append directories into the archive and not with a specific pattern that bulk requires? Thanks in advance!

like image 685
sstauross Avatar asked Dec 17 '15 13:12

sstauross


3 Answers

You can use bulk(mappings). Try:

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

UPDATED

Or you can do it even more easier:

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();
like image 136
Alexandr Lazarev Avatar answered Nov 14 '22 22:11

Alexandr Lazarev


@sstauross's answer works perfectly for me. I added a bit more code to allow adding a mix of directories and files into the zip process:

Suppose the folder /Users/x/baseFolder has the following structure:

 /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

And I can do the following to create a /Users/x/baseFolder/result.zip folder with the following structure and contents:

/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;
  });
}
like image 42
T.PHK Avatar answered Nov 14 '22 23:11

T.PHK


To everyone with the same issue, what finally worked for me is: lets say you have the following structure:

/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();
like image 23
sstauross Avatar answered Nov 14 '22 22:11

sstauross