Open In App

Why Zlib is used in Node.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

Zlip is a module used in Node.js to compress or decompress a file. Compression means zip the file and decompression means unzip the file. It is a pre-built Node.js module so don’t require to install it as a dependency.

Syntax:

const zlib = require('zlib');  

Compression of a file(Zipping):

Approach: We are going to use createGzip() method of the Zlib module to create a zip file or compressed file from the existing file. Then we are going to pipe the method with the file we want to compress while creating an output file as a result of createGzip() method.

Project Setup:

Step 1: Create and Open a project folder inside a code editor.

Step 2: Locate the project folder inside the terminal.

Step 3: Create files app.js and input.txt either manually or by typing the command

touch app.js input.txt

inside input.txt make sure that you have written some text so that when we compressed it and then decompressed it we will verify the output.

Step 4: Open the app.js file inside the code editor.

Code Writing:

Step 6: Require zlib module

const zlib = require('zlib'); 

Step 7: Require fs module

const fs = require('fs');  

this module helps us to create readable streams and writable streams.

Step 8: Create a Readable Stream

const inputFile = fs.createReadStream('input.txt'); 

so that our system can read the input file.

Step 9: Create a writable stream

const outputFile = fs.createWriteStream('input.txt.gz');  

so that our system can create an output file.

Step 10: Pipe the createGzip() method with the file we want to compress while creating an output file as a result of createGzip() method.

inputFile.pipe(zlib.createGzip()).pipe(outputFile);  

Example: We are going to compress a file using Zlip module. 

Javascript




const zlib = require('zlib');  
const fs = require('fs');  
  
const inputFile = fs.createReadStream('input.txt');  
const outputFile = fs.createWriteStream('input.txt.gz');  
  
inputFile.pipe(zlib.createGzip()).pipe(outputFile);


Steps to run the application: Inside the terminal type the command to run your script.

node app.js

Output:

 

Decompression of a file(Unzipping):

Approach: Now, the file which we create in Example 1, let’s unzip or decompressed it using the createUnzip() method of Zlib module.

Project Setup:

Step 1: Create and Open a project folder inside a code editor.

Step 2: Locate the project folder inside the terminal.

Step 3: Create files app.js either manually or by typing the command

touch app.js 

Step 4: Make sure that you want a decompressed file inside your current working directory which you want to unzip. In our case, we consider the file we zip in Example 1 so that we can verify the output result.

Step 5: Open the app.js file inside the code editor.

Code Writing:

Step 6: Require zlib module

const zlib = require('zlib');

Step 7: Require fs module

const fs = require('fs');  

this module helps us to create readable streams and writable streams.

Step 8: Create a Readable Stream

const inputFile = fs.createReadStream('input.txt.gz');

so that our system can read the input file.

Step 9: Create a writable stream

const outputFile = fs.createWriteStream('input2.txt');  

so that our system can create an output file.

Step 10: Pipe the createUnzip() method with the file we want to decompress while creating an output file as a result of the createUnzip() method.

inputFile.pipe(zlib.createUnzip()).pipe(outputFile);  

Example: We are going to decompress a file using Zlip module.

Javascript




const zlib = require('zlib');  
const fs = require('fs');  
  
const inputFile = fs.createReadStream('input.txt.gz');  
const outputFile = fs.createWriteStream('input2.txt');  
  
inputFile.pipe(zlib.createUnzip()).pipe(outputFile);


Steps to run the application: Inside the terminal type the command to run your script.

node app.js

Output:

 



Last Updated : 13 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads