Zip archives in node.js

前端 未结 10 1968
佛祖请我去吃肉
佛祖请我去吃肉 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:45

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

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

    If you only need unzip, node-zipfile looks to be less heavy-weight than node-archive. It definitely has a smaller learning curve.

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

    adm-zip

    It's a javascript-only library for reading, creating and modifying zip archives in memory.

    It looks nice, but it is a little buggy. I had some trouble unzipping a text file.

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

    I ended up doing it like this (I'm using Express). I'm creating a ZIP that contains all the files on a given directory (SCRIPTS_PATH).

    I've only tested this on Mac OS X Lion, but I guess it'll work just 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();
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题