Decompress file using zlib module of node.js


zlib module is used to provide compression and decompression functionalities in node. Decompress a file using createUnzip(); method of zlib module in node.js as shown in the snippet below:


//file-name : decompress-file-using-zlib.js
// Including the required modules	
var zlib = require('zlib');
var fs = require('fs');

var unzip = zlib.createUnzip();

var read = fs.createReadStream('assets/new.txt.gz');
var write = fs.createWriteStream('assets/unzip.txt');
//Transform stream which is unzipping the zipped file
console.log("unZipped Successfully");				


  • Now run the snippet using the following command :
    >node decompress-file-using-zlib.js
    unZipped Successfully