zlib
module is used to provide compression and decompression functionalities in node.js . GZIP
and deflate/inflate
. zlib
module via requiring it in the following way :
var zlib = require('zlib');
// Including the required modules
var zlib = require('zlib');
var fs = require('fs');
var zip = zlib.createGzip();
var read = fs.createReadStream('newfile.txt');
var write = fs.createWriteStream('newfile.txt.gz');
//Transform stream which is zipping the input file
read.pipe(zip).pipe(write);
console.log("Zipped Successfully");
// Including the required modules
var zlib = require('zlib');
var fs = require('fs');
var unzip = zlib.createUnzip();
var read = fs.createReadStream('newfile.txt.gz');
var write = fs.createWriteStream('unzip.txt');
//Transform stream which is unzipping the zipped file
read.pipe(unzip).pipe(write);
console.log("unZipped Successfully");
createDeflate()
createInflate()
createDeflateRaw()
createInflateRaw()
deflateSync()
inflateSync()
deflateRaw()
inflateRaw()
deflateRawSync()
inflateRawSync()
gzip()
unzip()
gzipSync()
unzipSync()
createGzip()
createGunzip()
In this chapter of 30 days of node tutorial series, we learned how we can compress and decompress files using zlib
module
of node.js. Also we leaned about all the methods available in zlib module.