htaidirt htaidirt - 2 years ago 920
Node.js Question

Node.js - Zip/Unzip a folder

I'm diving into Zlib of node.js. I was able to compress and uncompress files using the provided examples ( but I didn't be able to find more about doing the same for folders?

One possibility (but that I consider as tinkering) is to use

module and adding all the files of the folder one by one. But I'll face a problem when uncompressing (I will lose the folders in this case).

Any idea how to compress (and then uncompress) a whole folder (respecting the sub-solders hierarchy) using Node.js?


Answer Source

I've finally got it, with the help of @generalhenry (see comments on the question) and

as mentioned in the comments, we need to compress the folder in two steps:

  1. Convert the folder into a .tar file

  2. Compress the .tar file

In order to perform the first step, I needed two node.js modules:

npm install tar
npm install fstream

The first one allows us to create .tar files. You can have access to the source code here

The second node module will help us to read a folder and write a file. Concerning the basic fs node.js module, I don't know if it is possible to read a directory (I'm not talking about getting all the files in an array, using fs.readdir, but handling all the files and their organization in folders).

Then, when I convert the folder to .tar file, I can compress it using Gzip() of Zlib.

Here is the final code:

var fstream = require('fstream'),
    tar = require('tar'),
    zlib = require('zlib');

fstream.Reader({ 'path': 'path/to/my/dir/', 'type': 'Directory' }) /* Read the source directory */
.pipe(tar.Pack()) /* Convert the directory to a .tar file */
.pipe(zlib.Gzip()) /* Compress the .tar file */
.pipe(fstream.Writer({ 'path': 'compressed_folder.tar.gz' })); /* Give the output file name */

This helped me to compress an entire folder using node.js

2 more things:

  • As you can see, there is a lack of documentation on tar module. I hope this will be improved soon since the two examples that was provided talk about how to extract content from the .tar file.

  • I used the fstream module to help me handle the source directory. Can this be bypassed using fs? I don't know (please, comment if you have an idea).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download