Malik Kamran Abid
Malik Kamran Abid

Reputation: 341

Unzip the zip file and exclude the root directory in NodeJS

Is there any library from which I can exclude the root directory while unzipping.

For example, zip file contains - root directory - first file - second file - third file

I want to exclude the root directory.

Upvotes: 0

Views: 1221

Answers (1)

hoangdv
hoangdv

Reputation: 16157

I suggest use unzipper package.

Just extract what you are looking for:

const unzipper = require('unzipper');
const fs = require('fs');

fs.createReadStream('path/to/archive.zip') // Your zip file
  .pipe(unzipper.Parse())
  .on('entry', function (entry) {
    const fileName = entry.path;
    const type = entry.type; // 'Directory' or 'File'
    // file_name1, file_name1 are files which you are looking for
    if (type === 'File' && ['file_name1', 'file_name1'].indexOf(fileName) >= 0) {
      entry.pipe(fs.createWriteStream('output/path/' + fileName)); // Output folder of unzip process
    } else {
      entry.autodrain();
    }
  });

Upvotes: 2

Related Questions