Working with zip files in node.js

29 / Jan / 2014 by Sahil Chitkara 2 comments

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:

  1. Zipping a file present in the form of buffer:
    Lets assume we have some data in buffer named ‘dataBuffer’

    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);
    
  2. 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:

    var zip=require('adm-zip');
    var zipper = new zip();
    zipper.addLocalFile('/home/user/node/test.txt');
    

    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.

    zipper.addLocalFile('/home/user/node/test.txt','/home/user/node/test.zip');
    

    For adding multiple files

    fileArray.forEach(function(file){
    zipper.addLocalFile(file,'/home/user/node/test.zip');
    });
    

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.

var zip=require('adm-zip');
var zipper = new zip();
zipper.addLocalFolder('/home/user/test/','/home/user/test.zip');

Similarly to put group of directories into the archive:

directoryArray.forEach(function(dirPath){
zipper.addLocalFolder(dirPath,'/home/user/test.zip');
});

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:
zip.writeZip(/*target file name*/"/home/user/files.zip");
</span>
  • To write archive in response
var zipBuffer = zip.toBuffer();// or write everything to disk
</span>

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.

FOUND THIS USEFUL? SHARE IT

comments (2)

    1. Sai

      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

      Reply

Leave a comment -