简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM