thejoshwolfe / yauzl

yet another unzip library for node
MIT License
736 stars 80 forks source link

Unable to unzip hidden files #119

Closed xinkule closed 4 years ago

xinkule commented 4 years ago

Hello there, I have a zip file on osx, somehow it may include some hidden files like .DS_Store, and in the unzip process the program will throw ENOENT: no such file or directory, open xxx error. Wondering how to resolve the issue or could I just skip this kind of file ? Thank you. Following is my code snippet.

function unzip(path) {
  return new Promise((resolve, reject) => {
    yauzl.open(`${path}.zip`, { lazyEntries: true }, (err, zipfile) => {
      if (err) return reject(err);

      zipfile.on('close', () => {
        resolve();
      });

      zipfile.readEntry();

      zipfile.on('entry', entry => {
        const backslashCount = (entry.fileName.match(/\//g) || []).length;
        const firstBackslashIndex = entry.fileName.indexOf('/');
        const realFileName = entry.fileName.substring(firstBackslashIndex + 1);

        // create directory for file
        if (/\/$/.test(entry.fileName)) {
          fs.ensureDir(
            `${path}/${backslashCount > 1 ? realFileName : ''}`,
            err => {
              if (err) return reject(err);
              zipfile.readEntry();
            }
          );
        } else {
          zipfile.openReadStream(entry, (err, readStream) => {
            // error occurs here
            if (err) return reject(err);

            readStream.on('end', () => {
              zipfile.readEntry();
            });

            const out = fs.createWriteStream(`${path}/${realFileName}`);
            out.on('error', err => {
              reject(err);
            });

            readStream.pipe(out);
          });
        }
      });
    });
  });
}
thejoshwolfe commented 4 years ago

yauzl does not treat .DS_Store or any other files as "hidden"; that concept does not exist in zip files. If you'd like to skip the files, you can check for it in your code.

If you're getting an ENOENT, it's definitely not a bug in yauzl; i would check the stacktrace and find what call is causing that.