Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In Node v18 or higher: unzipper.Parse({forceStream: true}) throws Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close | SOLVED #269

Closed
Mikael-Kolehmainen opened this issue Mar 23, 2023 · 2 comments

Comments

@Mikael-Kolehmainen
Copy link

Mikael-Kolehmainen commented Mar 23, 2023

As the title says, unzipper will throw Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close but still execute successfully. This happens in Node v18 and higher while using the asyinc iterators.
For example (from the npm page) will throw the error:
const zip = fs.createReadStream('path/to/archive.zip').pipe(unzipper.Parse({forceStream: true})); for await (const entry of zip) { const fileName = entry.path; const type = entry.type; // 'Directory' or 'File' const size = entry.vars.uncompressedSize; // There is also compressedSize; if (fileName === "this IS the file I'm looking for") { entry.pipe(fs.createWriteStream('output/path')); } else { entry.autodrain(); } }

I did find a solution for this but couldn't push my branch and make a pull request, it's a one liner so I'll write it here.
In the file lib/parse.js on line 29 before self.emit('close'), I wrote self.emit('end'), which got the code working without the error being thrown.

It would be nice if you could update the repo and the npm package with this change, if this solution doesn't bring any other problems that I'm not aware of.

Please let me know if there's any questions.

@mr-smit
Copy link

mr-smit commented Apr 4, 2023

or you can temporarry ignore error until update using try catch block


try {
 // extract here
}catch(err){}

@ZJONSSON
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants