Zip archives in node.js

前端 未结 10 1967
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 05:09

I want to create a zip archive and unzip it in node.js.

I can\'t find any node implementation.

相关标签:
10条回答
  • 2020-12-01 05:33

    You can use archiver module, it was very helpful 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();
    });
    
    0 讨论(0)
  • 2020-12-01 05:33

    If you don't want to use/learn a library, you could use node to control the zip commandline tools by executing child processes

    Though I'd recommend learning a library like the one mentioned by Emmerman

    0 讨论(0)
  • 2020-12-01 05:36

    node-core has built in zip features: 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);
    
    0 讨论(0)
  • 2020-12-01 05:36

    I have used 'archiver' for zipping files. Here is one of the Stackoverflow link which shows how to use it, Stackoverflow link for zipping files with archiver

    0 讨论(0)
  • 2020-12-01 05:38

    You can try node-zip npm module.

    It ports JSZip to node, to compress/uncompress zip files.

    0 讨论(0)
  • 2020-12-01 05:45

    I've found it easiest to roll my own wrapper around 7-zip, but you could just as easily use zip or whatever command line zip tool is available in your runtime environment. This particular module just does one thing: zip a 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 like this, assuming you've saved the above code into zipdir.js. The third log param is optional. Use it if you have a custom logger. Or delete my obnoxious log statements entirely.

    const zipdir = require('./zipdir');
    
    (async () => {
      await zipdir('/path/to/my/directory', '/path/to/file.zip');
    })();
    
    0 讨论(0)
提交回复
热议问题