Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when npm `unzip` module has finished unzipping file?

I'm using the unzip module from npm to extract the contents of a zip archive. I need to know when it is done extracting and the file has been completely written to disk.

My code:

fs.createReadStream('master.zip').pipe(unzip.Extract({ path: 'gitdownloads/repo' }));

What I've tried:

My first thought was that I could tap into the stream and listen for the finish event, but unzip only takes input: it doesn't return another stream.

I also looked to see if the unzip module had a "finish" callback. No luck.

like image 624
BonsaiOak Avatar asked Feb 26 '16 14:02

BonsaiOak


People also ask

How do I read a zip file in node JS?

Inside the loop, you convert the object into a string that represents the object using the Node. js toString() method, then log it in the console using the console. log() method. Finally, you invoke the readZipArchive() function with the ZIP archive file path as an argument.

Which node module is used for zip and unzip functionalities?

ADM-ZIP is a pure JavaScript implementation for zip data compression for NodeJS. The library allows you to: decompress zip files directly to disk or in-memory buffers.


1 Answers

From the github README

Extract emits the 'close' event once the zip's contents have been fully extracted to disk.

You'll want to do something like:

fs.createReadStream('master.zip')
  .pipe(unzip.Extract({ path: 'gitdownloads/repo' }))
   .on('close', function () {
     ...
   });
like image 140
tschaible Avatar answered Sep 18 '22 14:09

tschaible