Open In App

Why Zlib is used in Node.js ?

Last Updated : 24 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Zlib is a crucial library in Node.js used for data compression and decompression. It's widely employed in web development and various applications to handle data more efficiently. Understanding Zlib's significance in Node.js can greatly enhance your capability to manage data transmission, storage, and overall application performance.

What is Zlib?

Zlib is a compression library that allows you to compress and decompress data streams. It is implemented in C and has been a part of many operating systems and software applications for years. In Node.js, Zlib is wrapped as a core module, making it easily accessible and convenient to use for developers.

Syntax:

const zlib = require('zlib');  

Compression of a file(Zipping)

In this, we are going to use the 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 the createGzip() method.

Steps to Setup the Project

Step 1: Make a folder structure for the project.

mkdir myapp

Step 2: Navigate to the project directory

cd myapp

Step 3: Initialize the NodeJs project inside the myapp folder.

npm init -y

Step 4: Create files app.js and input.txt either manually or by typing the command. 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.

touch app.js input.txt

Step 5: Open the app.js file inside the code editor and Require zlib module.

const zlib = require('zlib'); 

Step 6: Require fs module, this module helps us to create readable streams and writable streams.

const fs = require('fs');  

Step 7: Create a Readable Stream so that our system can read the input file.

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

Step 8: Create a writable stream, so that our system can create an output file.

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

Step 9: 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. 

Node
// app.js

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)

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

Steps to Setup the Project

Step 1: Make a folder structure for the project.

mkdir myapp

Step 2: Navigate to the project directory

cd myapp

Step 3: Initialize the NodeJs project inside the myapp folder.

npm init -y

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

touch app.js 

Step 5: 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 6: Require zlib module and Require fs module, this helps us to create readable streams and writable streams.

const zlib = require('zlib');
const fs = require('fs');

Step 7: Create a Readable Stream

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

Step 8: Create a writable stream, so that our system can read the input file.

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

Step 9: 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.

Node
// app.js

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:


Next Article

Similar Reads