Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to convert a file to zip file and download it using Node.js ?

  • Last Updated : 07 Oct, 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 version 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:

 

Javascript




// 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!
app.get('/',function(req,res){
    res.sendFile(__dirname+'/'+'index.html')
  
  
    // 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++){
        zp.addLocalFile(__dirname+'/'+'upload_data'+'/'+to_zip[k])
    }
  
  
    // here we assigned the name to our downloaded file!
    const file_after_download = 'downloaded_file.zip';
  
    // 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-Type','application/octet-stream');
    res.set('Content-Disposition',`attachment; filename=${file_after_download}`);
    res.set('Content-Length',data.length);
    res.send(data);
  
})
  
// this is used to listen a specific port!
app.listen(7777,function(){
    console.log('port is active at 7777');
})

Steps to run the program:

  1. Our project looks like:

final project

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

  1.  
npm install adm-zip

Run app.js file using following command.

  1.  
node app.js
  1.  

app is running

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

  1.  

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!