Compressing multiple files using zlib

2019-02-16 09:29发布

问题:

The below code will compress one file. How can I compress multiple files

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);

回答1:

Something that you could use:

var listOfFiles = ['firstFile.txt', 'secondFile.txt', 'thirdFile.txt'];

function compressFile(filename, callback) {
    var compress = zlib.createGzip(),
        input = fs.createReadStream(filename),
        output = fs.createWriteStream(filename + '.gz');

    input.pipe(compress).pipe(output);

    if (callback) {
        output.on('end', callback);
    }
}

#1 Method:

// Dummy, just compress the files, no control at all;
listOfFiles.forEach(compressFile);

#2 Method:

// Compress the files in waterfall and run a function in the end if it exists
function getNext(callback) {
    if (listOfFiles.length) {
        compressFile(listOfFiles.shift(), function () {
            getNext(callback);
        });
    } else if (callback) {
        callback();
    }
}

getNext(function () {
    console.log('File compression ended');
});


回答2:

Gzip is an algorithm that compresses a string of data. It knows nothing about files or folders and so can't do what you want by itself. What you can do is use an archiver tool to build a single archive file, and then use gzip to compress the data that makes up the archive:

  • https://github.com/mafintosh/tar-stream

Also see this answer for more information: Node.js - Zip/Unzip a folder



回答3:

The best package for this (and the only one still maintained and properly documented) seems to be archiver

var fs = require('fs');
var archiver = require('archiver');
var output = fs.createWriteStream('./example.tar.gz');
var archive = archiver('tar', {
    gzip: true,
    zlib: { level: 9 } // Sets the compression level.
});

archive.on('error', function(err) {
  throw err;
});

// pipe archive data to the output file
archive.pipe(output);

// append files
archive.file('/path/to/file0.txt', {name: 'file0-or-change-this-whatever.txt'});
archive.file('/path/to/README.md', {name: 'foobar.md'});

// Wait for streams to complete
archive.finalize();