archiverjs / node-archiver

a streaming interface for archive generation
https://www.archiverjs.com
MIT License
2.83k stars 219 forks source link

How to wait till the zipping completed #663

Open ViswanathCelitotech opened 1 year ago

ViswanathCelitotech commented 1 year ago

Hi Team, I am using the below code to perform zipping of files. The zipping is happening fine. I need to wait till zipping completed and so some action. This I want to achieve without using events. Is there any way in achieving the requirement.

`const fs = require('fs'); const archiver = require('archiver');

// Create a new archive instance const archive = archiver('zip', { zlib: { level: 9 } // Sets the compression level (optional) });

// Specify the output file path and create a writable stream for the archive const outputFilePath = '/path/to/output.zip'; const output = fs.createWriteStream(outputFilePath);

// Pipe the writable stream to the archive instance archive.pipe(output);

// Add files to the archive const files = [ { path: '/path/to/file1.txt', name: 'file1.txt' }, { path: '/path/to/file2.txt', name: 'file2.txt' }, // Add more files as needed ];

files.forEach(file => { archive.file(file.path, { name: file.name }); });

// Finalize the archive archive.finalize();`

//Need to do some action after the zipping completes with out using actions.

punkuz commented 1 year ago

Please wrap your code using: const zipAndUpload = new Promise((resolve, reject) => { //your zip code goes here } ); await zipAndUpload; //do what you want after

adevine commented 1 year ago

FWIW I added a suggestion here, https://github.com/archiverjs/node-archiver/issues/476#issuecomment-1792896115, about how to overwrite the finalize method so it waits for streaming to complete.

titanism commented 3 months ago

Wouldn't this suffice @adevine instead?

archive.finalize();
archive.on('warning', (err) => {
  console.warn(err);
});
await new Promise((resolve, reject) => {
  archive.once('error', reject);
  archive.once('end', resolve);
});
adevine commented 3 months ago

Wouldn't this suffice @adevine instead?

Not quite sure what you're asking. The point of my previous comment was that it's a footgun that calling await archive.finalize() doesn't also wait for the streaming output to complete. Sure, the caller can await separately on a promise that waits for the stream to finish, but the issue that needs fixing is most people would expect finalize to do this, and my comment was to show how you can override the finalize to make it wait for everything to be done.

Downchuck commented 2 weeks ago

Wouldn't this suffice @adevine instead?

Not quite sure what you're asking. The point of my previous comment was that it's a footgun that calling await archive.finalize()

Just got hit by this one -- absolute fortune after writing thousands of files that this is the only time it's it me. Could have been a real painful lesson. Note that @titanism does not follow the docs either - which say to set the listeners before running finalize. While the docs are correct -- it's clear from many examples that the interface is error prone for us programmers.