Skip to content
Related Articles

Related Articles

How to convert a file to zip file and download it using NodeJS ?
  • Last Updated : 05 Feb, 2021

The Zip files are a common way for storing compressed files and folders. In this article, I’ll demonstrate how to convert the file to zip-format by using the adm-zip module (NPM PACKAGE).

Uses of ADM-ZIP

  • compress the original file and change them to zip format.
  • update/delete the existing files(.zip format).

Installation of ADM-ZIP:

Step 1: Install the module using the below command in the terminal.

npm install adm-zip

Step 2: Check the version of the installed module by using the below command.

npm vresion adm-zip

We are going to change this upload_data folder to zip file using the adm-zip module!

upload_data FOLDER

Code for conversion and downloading zip file:


// express is a node framework that is helps in creating
// 2 or more web-pages application
const express = require('express')
// filesystem is a node module that allows us to work with
// the files that are stored on our pc
const file_system = require('fs')
// it is an npm package.this is to be required in our JS 
// file for the conversion of data to a zip file!
const admz = require('adm-zip')
// stores the express module into the app variable!
const app = express()
// this is the name of specific folder which is to be 
// changed into zip file1
var to_zip = file_system.readdirSync(__dirname+'/'+'upload_data')
// this is used to request the specific file and then print 
// the data in it!
    // zp is created as an object of class admz() which 
    // contains functionalities
    var zp = new admz();
    // this is the main part of our work!
    // here for loop check counts and passes each and every 
    // file of our folder "upload_data"
    // and convert each of them to a zip!
    for(var k=0 ; k<to_zip.length ; k++){
    // here we assigned the name to our downloaded file!
    const file_after_download = '';
    // toBuffer() is used to read the data and save it
    // for downloading process!
    const data = zp.toBuffer();
    // this is the code for downloading!
    // here we have to specify 3 things:
        // 1. type of content that we are downloading
        // 2. name of file to be downloaded
        // 3. length or size of the downloaded file!
    res.set('Content-Disposition',`attachment; filename=${file_after_download}`);
// this is used to listen a specific port!
    console.log('port is active at 7777');

Steps to run the program:

  1. Our project looks like:

    final project

  2. Open the terminal at the desired local and make sure that u have downloaded the adm-zip package using the following command.

    npm install adm-zip
  3. Run app.js file using following command.

    node app.js

    app is running

  4. Open the browser and open localhost:7777 and then the upload_data folder get converted to zip file and gets downloaded!

    changed to zip file

Output: Representing the whole procedure for converting file to zip file with the help of the following gif, so in this way you can change your folder to a zip file and then download it!

file to zip file

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :