Question

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);
Was it helpful?

Solution 4

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

OTHER TIPS

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:

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

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();
module.exports = class ZlibModule{

listOfImages = ['a.png','b.jpg'];
fs = require('fs')
zlib = require('zlib');

constructor(req,res){
    this.req = req;
    this.res = res;
}

compressOperation(){
    this.listOfImages.forEach((value,index) => {
        const readStream = this.fs.createReadStream(rootDirectory+'/modules/zlibModule/images/'+value);
        const writeStream = this.fs.createWriteStream(rootDirectory+'/modules/zlibModule/'+value+'.gz');
        const compress = this.zlib.createGzip()
        readStream
            .on('error',(error)=>{
                this.res.end(`${error.path} path does't exist`)
            })
            .pipe(compress)
            .pipe(writeStream)
            .on('error',(error)=>{
                console.log(error);
                this.res.end(`Something went to wrong`)
            })
            .on('finish',()=>{
                if(index+1 === this.listOfImages.length) this.res.end("Done");
            })
    });
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top