| Node.js ZLIB
Node.js ZLIB
Main functionalities of Node.js ZLIB module is to provide option of compression and decompression (like we do Zip and Unzip). And this is implemented by using Gzip and deflate/inflate.
We can accessed zlib module by using below commands:-
const zlib = require('zlib');
Compressing and decompressing a file can be done by piping the source stream data into a destination stream through zlib stream.
Node.js ZLIB Example: Compress File
To understand this we will take an example of Node.js ZLIB module to compress file "input.txt" into "input.txt.gz".
File: zlib_example1.js
above is the code text file named "input.txt".
Now we open Node.js command prompt and run the following below code:-
node zlibexample1.js
once the above .js file is run you will get the compressed file named "input.txt.gz" on the desktop as per the below screen.
Node.js ZLIB Example: Decompress File
Now we will do decomress the file by using Node.js ZLIB module and file is "input.txt.gz" into "input2.txt"
File: zlibexample2.js
Now we open Node.js command prompt and run the following below code:-
node zlibexample2.js
Now you will get file once you run above. Now you will see that same code of "input.txt" is available into "input2.txt" file. command
| | |