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