Zip archives in node.js

I want to create a zip archive and unzip it in node.js. I can not find the node implementation. Please, help.

+45
Apr 22 2018-11-11T00:
source share
10 answers

node -core has built-in zip functions: http://nodejs.org/api/zlib.html

Use them:

var zlib = require('zlib'); var gzip = zlib.createGzip(); var fs = require('fs'); var inp = fs.createReadStream('input.txt'); var out = fs.createWriteStream('input.txt.gz'); inp.pipe(gzip).pipe(out); 
+28
Oct 24 '13 at 14:45
source share

I ended up like this (I use Express). I am creating a ZIP that contains all the files in this directory (SCRIPTS_PATH).

I only tested this on Mac OS X Lion, but I think it will work fine on Linux and Windows with Cygwin installed.

  var spawn = require('child_process').spawn; app.get('/scripts/archive', function(req, res) { // Options -r recursive -j ignore directory info - redirect to stdout var zip = spawn('zip', ['-rj', '-', SCRIPTS_PATH]); res.contentType('zip'); // Keep writing stdout to res zip.stdout.on('data', function (data) { res.write(data); }); zip.stderr.on('data', function (data) { // Uncomment to see the files being added //console.log('zip stderr: ' + data); }); // End the response on zip exit zip.on('exit', function (code) { if(code !== 0) { res.statusCode = 500; console.log('zip process exited with code ' + code); res.end(); } else { res.end(); } }); }); 
+26
Jul 26 '11 at 22:55
source share

You can try the node-zip npm module.

It migrates JSZip to node to compress / decompress zip files.

+9
May 10 '12 at 6:58 a.m.
source share

You can use the archiver module, it was very useful for me, here is an example:

 var Archiver = require('archiver'), fs = require('fs'); app.get('download-zip-file', function(req, res){ var archive = Archiver('zip'); archive.on('error', function(err) { res.status(500).send({error: err.message}); }); //on stream closed we can end the request res.on('close', function() { console.log('Archive wrote %d bytes', archive.pointer()); return res.status(200).send('OK').end(); }); //set the archive name res.attachment('file-txt.zip'); //this is the streaming magic archive.pipe(res); archive.append(fs.createReadStream('mydir/file.txt'), {name:'file.txt'}); //you can add a directory using directory function //archive.directory(dirPath, false); archive.finalize(); }); 
+6
Apr 6 '15 at 19:47
source share

If you only need unpacking, node-zipfile looks less heavy than node-archive . He definitely has a lesser learning curve.

+1
Sep 22 '11 at 20:55
source share

I used the archiver to archive files. Here is one of the Stackoverflow links that shows how to use it, stack overflow

+1
Oct 17 '14 at 11:07
source share

If you do not want to use / study the library, you can use node to manage zip command line tools by executing child processes

Although I would recommend exploring a library like the one mentioned by Emmerman

0
Apr 23 2018-11-11T00:
source share

adm-zip

This is a javascript-only library for reading, creating and modifying zip archives in memory.

It looks good, but it is a bit buggy. I had problems unpacking a text file.

0
Feb 10 '13 at 9:15
source share

I found it easiest to wrap my own wrapper around 7-zip, but you can also easily use zip or any other zip command-line tool available in your runtime. This particular module just does one thing: archive the directory.

 const { spawn } = require('child_process'); const path = require('path'); module.exports = (directory, zipfile, log) => { return new Promise((resolve, reject) => { if (!log) log = console; try { const zipArgs = ['a', zipfile, path.join(directory, '*')]; log.info('zip args', zipArgs); const zipProcess = spawn('7z', zipArgs); zipProcess.stdout.on('data', message => { // received a message sent from the 7z process log.info(message.toString()); }); // end the input stream and allow the process to exit zipProcess.on('error', (err) => { log.error('err contains: ' + err); throw err; }); zipProcess.on('close', (code) => { log.info('The 7z exit code was: ' + code); if (code != 0) throw '7zip exited with an error'; // throw and let the handler below log it else { log.info('7zip complete'); return resolve(); } }); } catch(err) { return reject(err); } }); } 

Use it this way if you saved the above code in zipdir.js . The third parameter to log is optional. Use it if you have your own registrar. Or completely delete my nasty log entries.

 const zipdir = require('./zipdir'); (async () => { await zipdir('/path/to/my/directory', '/path/to/file.zip'); })(); 
0
May 02 '19 at 11:00
source share

You can use the edge.js module, which supports the interaction between node.js and the .NET in-process, and then calls the .NET framework ZipFile class, which allows you to manipulate ZIP archives. Here is a complete example of creating a ZIP package using edge.js. Also check unzip the example using edge.js.

-four
Apr 19 '13 at 23:35
source share



All Articles