6

I'm trying to use Node.js to create a zip file from an existing folder, and preserve the structure.

I was hoping there would be a simple module to allow this kind of thing:

archiver.create("../folder", function(zipFile){ 
    console.log('et viola');
});

but I can't find anything of the sort!

I've been googling around, and the best I've found so far is zipstream, but as far as I can tell there's no way to do what I want. I don't really want to call into commandline utilities, as the the app has to be cross platform.

Any help would be greatly appreciated.

Thanks.

user1257359
  • 105
  • 1
  • 6

3 Answers3

2

This can be done even simpler using node's built-in execfile function. It spawns a process and executes the zip command through the os, natively. Everything just works.

var execFile = require('child_process').execFile;

execFile('zip', ['-r', '-j', zipName, pathToFolder], function(err, stdout) {
        console.log(err);
        logZipFile(localPath);
    });

The -j flag 'junks' the file path, if you are zipping a sibdirectory, and don't want excessive nesting within the zip file.

Here's some documentation on execfile. Here's a man page for zip.

Gopherkhan
  • 4,317
  • 4
  • 32
  • 54
  • The op specifically said they wanted to native and cross platform and NOT a command line call. – JohnC Jun 12 '13 at 17:14
2

It's not entirely code free, but you can use node-native-zip in conjunction with folder.js. Usage:

function zipUpAFolder (dir, callback) {
    var archive = new zip();

    // map all files in the approot thru this function
    folder.mapAllFiles(dir, function (path, stats, callback) {
        // prepare for the .addFiles function
        callback({ 
            name: path.replace(dir, "").substr(1), 
            path: path 
        });
    }, function (err, data) {
        if (err) return callback(err);

        // add the files to the zip
        archive.addFiles(data, function (err) {
            if (err) return callback(err);

            // write the zip file
            fs.writeFile(dir + ".zip", archive.toBuffer(), function (err) {
                if (err) return callback(err);

                callback(null, dir + ".zip");
            });                    
        });
    });    
}
Jan Jongboom
  • 26,598
  • 9
  • 83
  • 120
  • Thanks very much! The zip is created, but unfortunately it's corrupted and appears empty. I'll play around with it and try and see what the problem is. – user1257359 Mar 23 '12 at 16:56
  • Actually, it only seems to be *slightly* corrupted ;) When I try and explore the created zip, it appears empty, but when I extract it, the contents look fine. Thanks for a great, swift answer! – user1257359 Mar 23 '12 at 17:21
0

Using Easy-zip, npm install easy-zip, you can do:

var zip5 = new EasyZip();
zip5.zipFolder('../easy-zip',function(){
    zip5.writeToFile('folderall.zip');
});
Igor S.
  • 3,332
  • 1
  • 25
  • 33
  • 1
    I wouldn't recommend using this because of [this issue](https://github.com/owenchong/easy-zip/issues/2). – Rax Jun 16 '14 at 19:41
  • I've been using `easy-zip` lately and haven't run into the issue that @rax highlighted. – developius Jun 14 '16 at 13:29