File compression helps in reducing the file size, as beginner developers, we always face challenges building a new feature in node.
Today, in this step-by-step tutorial, we will explain how to compress a file using Gzip format in node js. In order to compress a file, we will use the zlib module in the node js environment.
The Node. js Zlib package is compelling and offers compression and decompression (zip and unzip) services on the go. It is implemented using Gzip and deflate/inflate.
Earlier zlib module was available through the npm registry; nonetheless, this module has been deprecated since the functionality was incorporated into the node js crux.
The zlib is a super simple module and provides synchronous deflate/inflate for node.js in conjunction with streams.
A stream is a notional interface that helps in dealing with streaming data in Node.js. The stream module offers an API for incorporating the stream interface.
How to Compress a File with Zlib Module in Node Js
- Step 1: Build Blank Folder
- Step 2: Generate Package Json
- Step 3: Configure Main App File
- Step 4: Build File Compression
- Step 5: Test Feature
Build Blank Folder
We need a folder where we can put our functionality related code.
So, you may create the folder using command or you may go with the manual process.
mkdir node-world
When the folder is ready, make sure to get into it.
cd node-world
Generate Package Json
In this step, we require a package.json file, this file contains the scripts, project meta information.
It is easy to create and can be generated using the given below command.
npm init
Configure Main App File
Now, we need a specific file that allows us to write the code for building the feature.
Therefore, you have to create app.js file inside the node folder.
In this step, we have to associate the file with command that requires us to register the command into the package.json file.
...
...
"scripts": {
"start": "node app.js"
},
...
...
Build File Compression
In this step, we need to write the logic for making the feature. Therefore, open the app.js file and place the entire code into the file.
const { createReadStream, createWriteStream } = require('fs')
const { createGzip } = require('zlib')
const compressFile = (path) => {
const handleStream = createReadStream(path)
handleStream
.pipe(createGzip())
.pipe(createWriteStream(`${path}.gz`))
.on('finish', () => {
console.log(`Compression process done: ${path}`)
})
}
compressFile('./samurai.jpg')
Test Feature
To test the feature, you have to run the given command. Make sure to type the following command and press enter.
node app.js
We kept the samurai.jpg file at the root of project directory, however you can keep any file but make sure to pass the same name into the app file.
Once the script is executed, you can see the compressed file with .gz extension in project directory.
Compression process done: ./samurai.jpg
Conclusion
In this quick post, we have learned how to compress files with Gzip extension.
Ideally, files with GZ extension are compressed archives that are built by the standard GNU zip (gzip) compression algorithm.
In this node gzip example, we have gone through the file compression process through the zlib stream.
We saw how to use pipe method for gzip compression in the node that helps in streaming through a zlib Transform stream into a destination stream.