Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
How to Download a File using Express.js ?
Next article icon

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

Last Updated : 01 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Zip files are a common way of 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

Project Structure:

final project

We are going to change this upload_data folder to a 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     const 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 (let 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:

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

Run the app.js file using the following command.

node app.js

app is running

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

changed to zip file

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

https://media.geeksforgeeks.org/wp-content/uploads/20240801101623/Untitled42.mp4


Next Article
How to Download a File using Express.js ?

R

rahulmahajann
Improve
Article Tags :
  • Node.js
  • Web Technologies
  • NodeJS-Questions

Similar Reads

  • How to Download a File Using Node.js?
    Downloading files from the internet is a common task in many Node.js applications, whether it's fetching images, videos, documents, or any other type of file. In this article, we'll explore various methods for downloading files using Node.js, ranging from built-in modules to external libraries. Usin
    3 min read
  • Convert JSON file into CSV file and displaying the data using Node.js
    There are so many ways to store data for better understanding for individual purposes, in few cases, JSON files will be more suitable in few cases, CSV files, and there are also lots of other types as well like XML, etc. In this article, we will convert the JSON file data into CSV file data and also
    3 min read
  • How to Download a File using Express.js ?
    Express.js is a routing and middleware framework for handling the different routing of the webpage and it works between the request and response cycle and works on the top of the node.js server. In this article, we will discuss how to download a file using express.js. ApproachTo download a file usin
    3 min read
  • How to Get Information About a File using Node.js ?
    Node.js is an open-source and cross-platform runtime environment built on Chrome’s V8 JavaScript engine for executing JavaScript code outside of a browser. You need to recollect that NodeJS isn’t a framework, and it’s not a programming language. In this article, we will discuss how to get informatio
    3 min read
  • How to Implement File Download in NextJS using an API Route ?
    In web development, facilitating file downloads is a common requirement, whether it's providing users with documents, images, or other media files. NextJS, with its versatile API routes and server-side capabilities, offers an elegant solution for implementing file downloads. In this article, we'll e
    5 min read
  • How to add authentication in file uploads using Node.js ?
    There are multiple ways to upload files and apply authentications to them. The easiest way to do so is to use a node module called multer. We can add authentication by restricting users on file uploads such as they can upload only pdf and the file size should be less than 1 Mb. There are many module
    3 min read
  • How to Convert Data URI to File then append to FormData?
    A data URI is a base64 encoded string that represents a file (i.e., instead of specifying the URL of the file, you can insert the content of the file in a webpage). When a browser encounters a URI, it decodes and constructs the original file. DataURIs are most commonly used on the web for images. Wh
    3 min read
  • How to Convert PNG to JPG using Node.js ?
    The following approach covers how to convert PNG to JPG in Node.js using Jimp module. Jimp is an image processing library that we can use to do a lot of operations on images. Jimp stands for JavaScript Image Manipulation Program. ApproachTo convert PNG to JPG using Node.js. We will Import Jimp modul
    2 min read
  • How to Add Data in JSON File using Node.js ?
    JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article. Prerequisites:NPM NodeApproachTo add data in JSON file using the node js
    4 min read
  • How to convert JPG to PNG using Node.js ?
    Converting images between different formats is a common requirement in web development. Node.js, with its powerful libraries and ecosystem, makes it easy to perform such tasks. This article will show you how to convert a JPG image to PNG using Node.js, covering both the basic and more advanced techn
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences