node-archiver: Archive multiple directories

sstauross picture sstauross · Dec 17, 2015 · Viewed 10.3k times · Source

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!

Answer

Alexandr Lazarev picture Alexandr Lazarev · Dec 17, 2015

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