Working with zip files in node.js

29 / Jan / 2014 by Sahil Chitkara 3 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();
  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 .

    var zip=require('adm-zip');
    var zipper = new zip();

    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.


    For adding multiple files


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();

Similarly to put group of directories into the archive:


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/");
  • To write archive in response
var zipBuffer = zip.toBuffer();// or write everything to disk

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.


comments (3)

  1. Eduardo Mazzucchelli

    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/”);

    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


Leave a comment -