Node.js – Display list of contents of archive using unzip module

This tutorial shows how to display the list of contents of 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) ListContentsOfArchive.js Java Script

Following javascript reads the zip file specified (at line 6 in below script) and display the list of contents of the archive file.
Create the below script in your working directory where you installed above npm modules.


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


//File to display the list of contents
var inputFileName = 'C:\kalliphant\input.zip';

fs.createReadStream(inputFileName)
	.pipe(unzip.Parse())
	.on('entry', function (entry) {
		// Display filename
		console.log(entry.type + " " + entry.path)
		entry.autodrain();
	});

3) Run the script

If you run the script, it will read the zip file specified and display list of contents.
Below is the sample output

c:\kalliphant>
c:\kalliphant>node ListContentsOfArchive.js
Directory directory1
Directory directory1/subdirectory/
    :
    :
File directory1/subdirectory/file1
    :
File directory1/subdirectory/filen

c:\kalliphant>