Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Unzipper - how to know it's finished

I'm trying to use the unzipper node module to extract and process a number of files (exact number is unknown). However, I can't seem to figure out how to know when all the files are processed. So far, my code looks like this:

s3.getObject(params).createReadStream()
.pipe(unzipper.Parse())
.on('entry', async (entry) => {
    var fileName = entry.path;
    if (fileName.match(someRegex)) {
        await processEntry(entry);
        console.log("Uploaded");
    } else {
        entry.autodrain();
        console.log("Drained");
    }
});

I'm trying to figure out how to know that unzipper has gone through all the files (i.e., no more entry events are forthcoming) and all the entry handlers have finished so that I know I've finished processing all the files I care about.

I've tried experimenting with the close and finish events but when I have, they both trigger before console.log("Uploaded"); has printed, so that doesn't seem right.

Help?

like image 748
Swiftheart Avatar asked Oct 12 '25 21:10

Swiftheart


1 Answers

Directly from the docs:

The parser emits finish and error events like any other stream. The parser additionally provides a promise wrapper around those two events to allow easy folding into existing Promise-based structures.

Example:

fs.createReadStream('path/to/archive.zip')
  .pipe(unzipper.Parse())
  .on('entry', entry => entry.autodrain())
  .promise()
  .then( () => console.log('done'), e => console.log('error',e));
like image 98
imjared Avatar answered Oct 14 '25 12:10

imjared