I need to zip an entire directory using Node.js. I'm currently using node-zip and each time the process runs it generates an invalid ZIP file (as you can see from this Github issue).
Is there another, better, Node.js option that will allow me to ZIP up a directory?
EDIT: I ended up using archiver
writeZip = function(dir,name) {
var zip = new JSZip(),
code = zip.folder(dir),
output = zip.generate(),
filename = ['jsd-',name,'.zip'].join('');
fs.writeFileSync(baseDir + filename, output);
console.log('creating ' + filename);
};
sample value for parameters:
dir = /tmp/jsd-<randomstring>/
name = <randomstring>
UPDATE: For those asking about the implementation I used, here's a link to my downloader:
zip
command includes all parent folder hierarchy of the current working directory in the zipped file. This might be ok for you, it wasn't for me. Also changing the current working directory in child_process somehow doesn't effect the results. 2) To overcome this problem, you have to usepushd
to jump into the folder you will zip andzip -r
, but sincepushd
is built into bash and not /bin/sh you need to use /bin/bash also. In my specific case this wasn't possible. Just a heads up. – Warrenwarrenerchild_process.exec
api lets you specify the cwd from where you want to run the command. Changing the CWD does fix the issue of the parent folder hierarchy. It also fixes the issue of not needingpushd
. I fully recommend child_process. – Gaspard