node.jszipnode-archiver

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!


Solution

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