Node.js ZLIB
Node.js ZLIB
The Node.js Zlib module is a powerful built-in utility that enables compression and decompression (zip and unzip) functionalities. It’s implemented using Gzip and Deflate/Inflate algorithms, allowing developers to efficiently reduce file sizes and manage data transfer more effectively.
Introduction to Applied AI:–Click Here
You can access the Zlib module in Node.js using:
const zlib = require('zlib');
With Zlib, compressing and decompressing files becomes simple — you can pipe data streams through Zlib’s transformation streams to achieve both operations seamlessly.
Node.js ZLIB Example: Compress a File
Let’s look at a practical example of using the Node.js Zlib module to compress a file named input.txt
into a new compressed file input.txt.gz
.
Data Science Tutorial:-Click Here
File: zlib_example1.js
const zlib = require('zlib');
const gzip = zlib.createGzip();
const fs = require('fs');
const inp = fs.createReadStream('input.txt');
const out = fs.createWriteStream('input.txt.gz');
inp.pipe(gzip).pipe(out);
Make sure you have a text file named input.txt
available (for example, on your desktop).
Download New Real Time Projects :–Click here
Now, open your Node.js command prompt and run:
node zlib_example1.js
This will create a compressed file named input.txt.gz
in the same directory.
Node.js ZLIB Example: Decompress a File
Next, let’s see how to decompress the previously generated input.txt.gz
file back into a readable text file input2.txt
.
File: zlib_example2.js
const zlib = require('zlib');
const unzip = zlib.createUnzip();
const fs = require('fs');
const inp = fs.createReadStream('input.txt.gz');
const out = fs.createWriteStream('input2.txt');
inp.pipe(unzip).pipe(out);
Execute the script using:
Machine Learning Tutorial:–Click Here
node zlib_example2.js
After running this, you’ll find a new file named input2.txt
in your directory. It will contain the same data as the original input.txt
file.
Complete Advance AI topics:- CLICK HERE
To better understand how effective compression is, try creating an input.txt
file with a large amount of data (for example, 40 KB). Once compressed using Zlib, the resulting input.txt.gz
file might be as small as 1 KB. When you decompress it back, you’ll get your original 40 KB of data perfectly restored.
Deep Learning Tutorial:– Click Here
Complete Python Course with Advance topics:-Click Here
SQL Tutorial :–Click Here
zlib npm zlib unzip nodejs zlib gunzipsync zlib gzip nodejs zlib js zlib nodejs example zlib gunzip example zlib inflate node js zlib download node js zlib w3schools node js zlib example
Post Comment