Working with zip files in node.js
Large files can be really difficult to share and exchange. Most email service providers do not allow to share heavy files and most of the times we end up zipping them together for easy sharing.
Zipping a file can be of great convenience for you and the end user both.
Based on different use cases, In this blog, we will discuss how we can zip a buffer data and save it to disk. We will be using a good module for zipping files or unzipping files, named ‘adm-zip’.
adm-zip: A Javascript implementation of zip for nodejs. Allows user/s to create or extract zip files both in memory or to/from disk. To know more, you can access its github page.
Use cases:
Zipping a file:
- Zipping a file present in the form of buffer:
Lets assume we have some data in buffer named ‘dataBuffer’
[js]
var zip=require(‘adm-zip’);
var dataBuffer = new Buffer("hello,this is test data",’utf-8′);//console.log(dataBuffer.toString());
var zipper = new zip();
zipper.addFile(‘test.txt’,dataBuffer);
[/js] - Zipping a file present on disk :
Sometimes we have file/s saved on our disk, to make zip of those file we use “addLocalFile”. It will zip the file and add it to the archive .
Example:[js]var zip=require(‘adm-zip’);
var zipper = new zip();
zipper.addLocalFile(‘/home/user/node/test.txt’);
[/js]It will add file “test.txt” that resides in node directory into the archive.
We can also add some local file/s in pre-existing archive by giving it to that archive in second argument.[js]
zipper.addLocalFile(‘/home/user/node/test.txt’,’/home/user/node/test.zip’);
[/js]For adding multiple files
[js]
fileArray.forEach(function(file){
zipper.addLocalFile(file,’/home/user/node/test.zip’);
});
[/js]
Zipping folder from disk:
Suppose we have a directory on disk and we need to zip out the whole directory. In such cases, we will use “addLocalFolder” method which allow us to add whole directory to the archive.
[js]
var zip=require(‘adm-zip’);
var zipper = new zip();
zipper.addLocalFolder(‘/home/user/test/’,’/home/user/test.zip’);
[/js]
Similarly to put group of directories into the archive:
[js]
directoryArray.forEach(function(dirPath){
zipper.addLocalFolder(dirPath,’/home/user/test.zip’);
});
[/js]
After zipping, we will be required to write those files somewhere, be it on disk or in response. Below, we have mentioned some methods to write these files:
- To write the archive file on disk:
[js]
zip.writeZip(/*target file name*/"/home/user/files.zip");
</span>
[/js]
- To write archive in response
[js]
var zipBuffer = zip.toBuffer();// or write everything to disk
</span>[/js]
We can send this buffer data in response to a request and setting filename in headers of response.
Hope this blog was helpful for working with zip files in node.js.
do you know ? why the adm-zip generated zipped file with 0 bytes ?
var zip = new admZip();
// add local file
zip.addLocalFile(“/home/xmazzuce/Downloads/004534111.PDF”); zip.addLocalFile(“/home/xmazzuce/Downloads/004534107.PDF”); zip.addLocalFile(“/home/xmazzuce/Downloads/004534117.PDF”); zip.addLocalFile(“/home/xmazzuce/Downloads/004209531.PDF”);
// get everything as a buffer
//var willSendthis = zip.toBuffer();
// or write everything to disk
zip.writeZip(/*target file name*/”/home/xmazzuce/apps/wells/files.zip”);
thank you 🙂
Hi sahil, Nice explanation.. is there any limit for the size of the file to be archived, if it’s there can we compress a single file into multipile partitions