NodeJS ZLIB: How to Zip and Unzip Files Using NodeJS

Nodejs Zlib Thumbnail

ZLIB is a Node.js module that is used to zip and unzip files. It is not required to install this module to use it inside the application, it is a pre-build module so it can be used directly by requiring it inside the application.

Syntax:

const zlib = require('zlib');  

Compressing or Zipping a File Using NodeJS Zlib

Compression reduces file sizes and can be done in multiple file formats. Here, let’s try to compress a file as a zip.

The compression can be done by using the createGzip() method of the ZLIB module.

Syntax:

zlib.createGzip()

Let’s create a simple file compression application in Node.js using the ZLIB module.

Implementing File Compression using NodeJS Zlib

Let’s get right into an example of how you can compress files using NodeJS.

Setting up the environment

  • Folder Structure

Create a project folder, “File Compressor”, inside this folder create a file app.js where we will write code and, unzip.text contains a “Hello World!” text.

  • Code Editor

Open the project inside the code editor, for this article VS-Code is used, but you can use any editor like the atom, subline, etc.

  • Check Node.js

Open the terminal and type the following command to make sure that Node.js is installed on your system.

node -v

If its returns a version then Node.js is installed, if not then install it from Node.js’ official website.

Importing the required modules

To use the ZLIB module, we first have to require it. We will also use the File System module since we have to interact with files.

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

Create readable and writable streams, the readable stream is used for reading the file so that we can convert it into a zip file whereas the writable stream is used to create a new file as an output for the converted zip file.

const inputFile = fs.createReadStream('unzip.txt');  
const outputFile = fs.createWriteStream('zip.gz');  

Then we have to pipe the input stream to read the file and then pipe the output stream to store the converted file.

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

Complete Code to Compress Files Using NodeJS

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

const inputFile = fs.createReadStream('unzip.txt');  
const outputFile = fs.createWriteStream('zip.gz');  

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

Output

Zlib Zip Example

Decompressing a file using NodeJS zlib

Decompression is used to decompress/unzip a file and convert it into its original form.

The compression can be done by using the createUnzip() method of the ZLIB module.

Syntax:

zlib.createUnzip()

Let’s create a simple file decompression application in Node.js using the ZLIB module.

Creating a NodeJS app to decompress files

The setup and requiring statements are the same as the previous application

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

Create readable and writable streams, the readable stream is used for reading the file so that we want to decompress/unzip whereas the writable stream is used to create a new file as an output for the converted file.

const inputFile = fs.createReadStream('zip.gz');  
const outputFile = fs.createWriteStream('unzip.txt');  

Then we have to pipe the input stream to read the zip file and then pipe the output stream to store the converted unzip file.

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

Complete Code for Decompressing Files Using Zlib

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

const inputFile = fs.createReadStream('zip.gz');  
const outputFile = fs.createWriteStream('unzip.txt');  

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

Output

Zlib Unzip Example

Summary

Sometimes we want to create a zip file and sometimes we want to unzip a file. To compress and uncompress files, we have to use a third-party application. But by using the ZLIB, you can create a zip file and also unzip it by writing a few lines of code.

ZLIB is pre-installed in just Node.js so you don’t even have to install it. Just import it inside a javascript file and use its functions to perform different operations. ZLIB provides us with many methods but the widely used method is createGzip() and createUnzip() method which is used to zip and unzip a file.

We hope this tutorial helped you understand the ZLIB module using Node.js.

Reference

https://nodejs.org/api/zlib.html