Open ViswanathCelitotech opened 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
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.
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);
});
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.
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.
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.