thejoshwolfe / yauzl

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

calculating progress for the whole archive. #129

Closed stukennedy closed 9 months ago

stukennedy commented 4 years ago

being able to display the total progress of an extraction is one of the most common requirements for any archive application.

I just can't figure out how to do it with this library. The unzip example shows how to calculate the progress of each individual file in the archive from entry.uncompressedSize and accumulating the bytes written to the target file, but I can't work out how to get the uncompressed size of the whole archive.

Can anyone help?

vaditya92 commented 4 years ago

@continuata Hey. You can listen to the 'entry' event which has uncompressedSize property for each entry. You can keep incrementing this value for each entry. I had a similar use case and found this workaround.

erikjalevik commented 4 years ago

I just implemented this today by adding a preprocessing step where I add up the sizes before starting extraction. Here's the code snippet (TypeScipt) for what it's worth:

let uncompressedArchiveStats: [number, number] = [0, 0]; // [size, count]

const onOpen = (err?: Error, zipFile?: yauzl.ZipFile) => {
  if (err || !zipFile) {
    // handle error
  }

  zipFile.on("entry", onEntry);
}

const onEntry = (entry: yauzl.Entry) => {
  if (!entry.fileName.endsWith("/")) { // is not directory
    uncompressedArchiveStats = [
      uncompressedArchiveStats[0] + entry.uncompressedSize,
      uncompressedArchiveStats[1] + 1
    ];                
  }
};

yauzl.open(
  archive.path, 
  { autoClose: true, lazyEntries: false },
  onOpen        
);
thejoshwolfe commented 9 months ago

a 2-pass approach with a preprocessing step is the best you can do for this in general. yauzl is a low-level library that doesn't want to incur any overhead of calculating the total size if it's not needed. the intention is that you can calculate that in a high level of code above this library.