Decompress file using zlib module of node.js


Overview

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:

Code


													
//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
read.pipe(unzip).pipe(write);	
console.log("unZipped Successfully");				
													
												

Run

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