In Node.js, ZLIB module provides compression and decompression functionalities using various compression algorithms.
It allows us to compress and decompress data using gzip, deflate, and other compression formats.
Here are some often used methods of zlib module in Node.js:
The "zlib.gzip()" method is used to compress a buffer using the "gzip" algorithm.
const zlib = require('zlib'); const inputBuffer = Buffer.from('Hello World, Node.js!'); zlib.gzip(inputBuffer, (err, compressedBuffer) => { if (err) { console.error(`Error while Compression: ${err.message}`); return; } console.log('Compressed data result:', compressedBuffer.toString('base64')); });
Compressed data result: H4sIAAAAAAAAE/NIzcnJVwjPL8pJ0VHwy09J1csqVgQAbcyChxUAAAA=
The "zlib.gunzip()" method is used to decompress a buffer that was compressed with gzip.
const zlib = require('zlib'); // de-compression of above step Output const compressedBuffer = Buffer.from('H4sIAAAAAAAAE/NIzcnJVwjPL8pJ0VHwy09J1csqVgQAbcyChxUAAAA=', 'base64'); zlib.gunzip(compressedBuffer, (err, decompressedBuffer) => { if (err) { console.error(`Error while Decompression: ${err.message}`); return; } console.log('Decompressed data result:', decompressedBuffer.toString()); });
// This output we receive after we have compressed `text` string. // then de-compressed `base64` string to get original result. Decompressed data result: Hello World, Node.js!
The "zlib.deflate()" method is used to compress a buffer using the deflate algorithm.
const zlib = require('zlib'); const inputBuffer = Buffer.from('Hello World, Node.js!'); zlib.deflate(inputBuffer, (err, compressedBuffer) => { if (err) { console.error(`Error while Compressing Buffer: ${err.message}`); return; } console.log('Compressed data result:', compressedBuffer.toString('base64')); });
// After running above code Output is: > Compressed data result: eJzzSM3JyVcIzy/KSdFR8MtPSdXLKlYEAFDMBxs=
The "zlib.inflate()" method is used to decompress a buffer that was compressed with deflate.
const zlib = require('zlib'); const compressedBuffer = Buffer.from('eJzzSM3JyVcIzy/KSdFR8MtPSdXLKlYEAFDMBxs=', 'base64'); zlib.inflate(compressedBuffer, (err, decompressedBuffer) => { if (err) { console.error(`Error while Decompression: ${err.message}`); return; } console.log('Decompressed data result:', decompressedBuffer.toString()); });
// decompressing with `compressed with deflate step Output : eJzzSM3JyVcIzy/KSdFR8MtPSdXLKlYEAFDMBxs=` Decompressed data result: Hello World, Node.js!
The "zlib.createGzip()" method creates a writable stream that compresses data on-the-fly.
const zlib = require('zlib'); const fs = require('fs'); const inputStream = fs.createReadStream('input.txt'); const gzipStream = zlib.createGzip(); const outputStream = fs.createWriteStream('output.gz'); inputStream.pipe(gzipStream).pipe(outputStream);
// This will create output.gz in your current directory. // in Next Step, while decompressing we get same output in file named `output.txt`
The "zlib.createGunzip()" method creates a readable stream that decompresses data on the fly.
const zlib = require('zlib'); const fs = require('fs'); const inputStream = fs.createReadStream('output.gz'); const gunzipStream = zlib.createGunzip(); const outputStream = fs.createWriteStream('output.txt'); inputStream.pipe(gunzipStream).pipe(outputStream);
// outputStream will create new file with named `output.txt` and it will contain same content that was present in input.txt file while Stream Compression. Output: > Hello World, Node.js