Node.js – Extract archive (zip) using unzip module

This tutorial shows how to extract a given archive (zip/jar/rar etc) using unzip module and node.js.
This uses the following node modules

  • unzip

  • File System

Steps

1) Install unzip module

Unzip module provides simple APIs for parsing and extracting zip files. Refer to unzip documentation for more detail.
To install this module, run the following command at the command prompt in your working directory

c:\kalliphant>
c:\kalliphant>
c:\kalliphant> npm install unzip
     : 
     :
c:\kalliphant>

2) ExtractFileFromArchive.js Java Script

Following javascript reads the zip file specified (at line 5 in below script) and extract the given file from archive file.
Create the below script in your working directory where you installed above npm modules.


var unzip = require('unzip');
var fs = require('fs');


var inputFileName = 'C:/kalliphant/input.zip';
var extractToDirectory = 'C:/kalliphant/outputdir';

fs.createReadStream(inputFileName)
	.pipe(unzip.Extract({
		path: extractToDirectory 
	}));

3) Run the script

If you run the script, it will read the zip file specified and extract the zip file to given output directory.

c:\kalliphant>
c:\kalliphant>node ExtractArchive.js

c:\kalliphant>