How to convert a file to zip file and download it using Node.js ?
Last Updated :
01 Aug, 2024
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 projectWe are going to change this upload_data folder to a zip file using the adm-zip module!
upload_data FOLDERCode 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 runningOpen the browser and open localhost:7777 and then the upload_data folder gets converted to a zip file and gets downloaded!
changed to zip fileOutput: 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!
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.Using
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 using
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