Need a zip whole directory using Node.js

I need to zip the whole directory using Node.js. I am currently using node-zip, and every time the process starts, it generates an invalid ZIP file (as you can see from this Github problem ).

Is there any other, better version of Node.js that will allow me a ZIP 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 is a link to my bootloader :

+77
Mar 26 '13 at 15:41
source share
10 answers

I ended up using archiver lib. It works great.

Example

 var file_system = require('fs'); var archiver = require('archiver'); var output = file_system.createWriteStream('target.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: 'source', src: ['**'], dest: 'source'} ]); archive.finalize(); 
+96
Sep 12 '13 at 22:06 on
source share

I do not pretend to show anything new, I just want to generalize the above solutions for those who like to use Promise functions in their code (for example, I).

 const archiver = require('archiver'); /** * @param {String} source * @param {String} out * @returns {Promise} */ function zipDirectory(source, out) { const archive = archiver('zip', { zlib: { level: 9 }}); const stream = fs.createWriteStream(out); return new Promise((resolve, reject) => { archive .directory(source, false) .on('error', err => reject(err)) .pipe(stream) ; stream.on('close', () => resolve()); archive.finalize(); }); } 

Hope this helps someone;)

+22
Jul 25 '18 at 11:46
source share

To include all files and directories:

 archive.bulk([ { expand: true, cwd: "temp/freewheel-bvi-120", src: ["**/*"], dot: true } ]); 

It uses node-glob ( https://github.com/isaacs/node-glob ), so any expression compatible with it will work.

+10
Jan 14 '15 at 3:19
source share

Archive.bulk now deprecated, a new method to be used for this, glob :

 var fileName = 'zipOutput.zip' var fileOutput = fs.createWriteStream(fileName); fileOutput.on('close', function () { console.log(archive.pointer() + ' total bytes'); console.log('archiver has been finalized and the output file descriptor has closed.'); }); archive.pipe(fileOutput); archive.glob("../dist/**/*"); //some glob pattern here archive.glob("../dist/.htaccess"); //another glob pattern // add as many as you like archive.on('error', function(err){ throw err; }); archive.finalize(); 
+10
Nov 22 '16 at 7:40
source share

This is another library that packs a folder in one line: zip-local

 var zipper = require('zip-local'); zipper.sync.zip("./hello/world/").compress().save("pack.zip"); 
+6
Jul 30 '18 at 11:27
source share

Use the Node native child_process api for this.

No need for third-party libraries. Two lines of code.

 const child_process = require("child_process"); child_process.execSync('zip -r DESIRED_NAME_OF_ZIP_FILE_HERE *', { cwd: PATH_TO_FOLDER_YOU_WANT_ZIPPED_HERE }); 

I am using the synchronous API. You can use child_process.exec(path, options, callback) if you need asynchrony. There are many more options than just specifying CWD to further customize your queries. See exec / execSync docs.




Please note: this example assumes that zip is installed on your system (at least it comes with OSX). Some operating systems may not have the utility installed (i.e. the AWS Lambda runtime does not). In this case, you can easily get the zip utility binary here and pack it together with the source code of your application (for AWS Lambda you can also pack it in Lambda Layer), or you will have to either use a third-party module (there are many of them on NPM). I prefer the first approach because the ZIP utility has been tested and tested for decades.

+4
Apr 22 '18 at 20:20
source share

Adm-zip has problems simply compressing the existing https://github.com/cthackers/adm-zip/issues/64 archive, as well as corruption with binary compression.

I also ran into compression problems with node-zip https://github.com/daraosn/node-zip/issues/4

node-archiver is the only one that seems to work well for compression, but it has no functions without compression.

+3
Sep 11 '13 at 21:41
source share

To pass the result to the response object (scripts in which you need to load the zip and not store it locally)

  archive.pipe(res); 

The washed out hints for accessing the contents of the directory worked for me.

 src: ["**/*"] 
+3
Aug 28 '15 at 13:38
source share

I found this little library that contains what you need.

 npm install zip-a-folder const zip-a-folder = require('zip-a-folder'); await zip-a-folder.zip('/path/to/the/folder', '/path/to/archive.zip'); 

https://www.npmjs.com/package/zip-a-folder

0
Oct 01 '18 at 21:04
source share

You can try in a simple way:

Install zip-dir :

 npm install zip-dir 

and use it

 var zipdir = require('zip-dir'); let foldername = src_path.split('/').pop() zipdir(<<src_path>>, { saveTo: 'demo.zip' }, function (err, buffer) { }); 
0
Nov 12 '18 at 10:46
source share



All Articles