Simplest way to download and unzip files in Node.js cross-platform?

前端 未结 11 1452
一个人的身影
一个人的身影 2020-11-30 23:44

Just looking for a simple solution to downloading and unzipping .zip or .tar.gz files in Node.js on any operating system.

Not sure if this

相关标签:
11条回答
  • 2020-11-30 23:52

    I tried a few of the nodejs unzip libraries including adm-zip and unzip, then settled on extract-zip which is a wrapper around yauzl. Seemed the simplest to implement.

    https://www.npmjs.com/package/extract-zip

    var extract = require('extract-zip')
    extract(zipfile, { dir: outputPath }, function (err) {
       // handle err
    })
    
    0 讨论(0)
  • 2020-11-30 23:54

    I found success with the following, works with .zip
    (Simplified here for posting: no error checking & just unzips all files to current folder)

    function DownloadAndUnzip(URL){
        var unzip = require('unzip');
        var http = require('http');
        var request = http.get(URL, function(response) {
            response.pipe(unzip.Extract({path:'./'}))
        });
    }
    
    0 讨论(0)
  • 2020-11-30 23:56

    Node has builtin support for gzip and deflate via the zlib module:

    var zlib = require('zlib');
    
    zlib.gunzip(gzipBuffer, function(err, result) {
        if(err) return console.error(err);
    
        console.log(result);
    });
    

    Edit: You can even pipe the data directly through e.g. Gunzip (using request):

    var request = require('request'),
        zlib = require('zlib'),
        fs = require('fs'),
        out = fs.createWriteStream('out');
    
    // Fetch http://example.com/foo.gz, gunzip it and store the results in 'out'
    request('http://example.com/foo.gz').pipe(zlib.createGunzip()).pipe(out);
    

    For tar archives, there is Isaacs' tar module, which is used by npm.

    Edit 2: Updated answer as zlib doesn't support the zip format. This will only work for gzip.

    0 讨论(0)
  • 2020-11-30 23:56

    yauzl is a robust library for unzipping. Design principles:

    • Follow the spec. Don't scan for local file headers. Read the central directory for file metadata.
    • Don't block the JavaScript thread. Use and provide async APIs.
    • Keep memory usage under control. Don't attempt to buffer entire files in RAM at once.
    • Never crash (if used properly). Don't let malformed zip files bring down client applications who are trying to catch errors.
    • Catch unsafe filenames entries. A zip file entry throws an error if its file name starts with "/" or /[A-Za-z]:// or if it contains ".." path segments or "\" (per the spec).

    Currently has 97% test coverage.

    0 讨论(0)
  • 2020-11-30 23:58

    Checkout gunzip-file

    import gunzip from 'gunzip-file';
    
    const unzipAll = async () => {
      try {
        const compFiles = fs.readdirSync('tmp')
        await Promise.all(compFiles.map( async file => {
          if(file.endsWith(".gz")){
            gunzip(`tmp/${file}`, `tmp/${file.slice(0, -3)}`)
          }
        }));
      }
      catch(err) {
        console.log(err)
      }
    }
    
    0 讨论(0)
  • 2020-11-30 23:58

    You can simply extract the existing zip files also by using "unzip". It will work for any size files and you need to add it as a dependency from npm.

    fs.createReadStream(filePath).pipe(unzip.Extract({path:moveIntoFolder})).on('close', function(){
            //To do after unzip
    				callback();
    		});

    0 讨论(0)
提交回复
热议问题