Why Zlib is used in Node.js ?
Last Updated :
24 Jul, 2024
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:
Similar Reads
What is spawn in Node JS ?
Node JS is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of an internet browser. In this article, we will learn about the Spawn in NodeJs. PrerequisitesNodeJS fundamentalsAsynchronous ProgrammingChild ProcessesSpawn i
3 min read
What is Piping in Node.js ?
Piping in NodeJS is the process by which byte data from one stream is sent to another stream. It is a powerful feature in Node.js that allows data to be transferred from one stream to another seamlessly. Streams are an integral part of Node.js, providing a mechanism for handling data that is too lar
6 min read
What is package.json in Node.js ?
In the world of Node.js development, package.json is a crucial file that serves as the heart of any Node.js project. It acts as a manifest that defines the projectâs metadata, dependencies, scripts, and more. This article will provide an in-depth look at what package.json is, why it's essential, and
4 min read
What is Buffer in Node.js ?
In Node, Buffer is used to store and manage binary data. Pure JavaScript is great with Unicode-encoded strings, but it does not handle binary data very well. It is not problematic when we perform an operation on data at the browser level but at the time of dealing with TCP stream and performing a re
3 min read
What is File System Module in Node.js ?
Node.js is a powerful platform for building server-side applications, and one of its key features is its ability to interact with the file system. The File System (fs) module in Node.js provides an extensive API to work with files and directories, allowing developers to perform operations such as re
3 min read
What is NODE_ENV in Node ?
In Node.js, NODE_ENV is an environment variable used to define the current environment in which the Node.js application is running. It plays a crucial role in configuring the behavior of the application based on different environments such as development, staging, and production. This article delves
4 min read
Node.js urlObject.query API
The urlObject.query is the query string returned without the ASCII question mark (?) or an object returned by the query string module named as parse() method. The url.parse() method is used to check whether the query is a string or an object. Basically, the argument (parseQueryString) that is passed
2 min read
Node.js Web Server
A NodeJS web server is a server built using NodeJS to handle HTTP requests and responses. Unlike traditional web servers like Apache or Nginx, which are primarily designed to give static content, NodeJS web servers can handle both static and dynamic content while supporting real-time communication.
6 min read
Node.js Zlib Module
The zlib module in Node.js is used for compressing and decompressing data. It uses Gzip and Deflate/Inflate algorithms to reduce data size, improving performance and saving bandwidth in Node.js applications.Zlib Module in Node.jsThe zlib module helps developers compress and decompress data streams o
4 min read
Node.js zlib.unzipSync() Method
The zlib.unzipSync() method is an inbuilt application programming interface of the Zlib module which is used to decompress a chunk of data with Unzip. Syntax: zlib.unzipSync( buffer, options ) Parameters: This method accepts two parameters as mentioned above and described below: buffer: It can be of
1 min read