yauzl (yet another unzip library) is a Node.js package designed for reading and extracting `.zip` archives. It strictly adheres to the ZIP specification, differentiating itself by reading the central directory for file metadata rather than scanning for local file headers, which are prone to being out of sync with the central directory. The library emphasizes non-blocking, asynchronous APIs and efficient memory usage, particularly through its `lazyEntries` option, which prevents buffering entire files or all entries into RAM simultaneously. Version 3.3.0 is the current stable release, with the project demonstrating a healthy and positive release cadence, with updates typically occurring every few months. Key differentiators include robust error handling, built-in validation to guard against unsafe file names and potential zip bomb attacks, and support for non-conformant zip files created by certain Microsoft tools. The API is callback-based, making it suitable for event-driven Node.js applications.
npm install yauzlVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to open a zip file, process entries one by one using `lazyEntries`, handle both files and directories, and stream content to disk.
Ensure your Node.js environment is version 12 or newer. If implementing a custom `RandomAccessReader`, rename the `destroy` method to `_destroy` and update its signature to `_destroy(err, callback)`.
Upgrade `yauzl` to version 3.3.0 or higher immediately. This fix addresses the vulnerability and prevents application crashes from malformed zip files.
Always set `lazyEntries: true` in the `options` object when calling `yauzl.open()`, and manually call `zipfile.readEntry()` after processing each entry to control memory usage.
Keep `validateEntrySizes` set to `true` (the default). If you must disable it, implement custom heuristics for size validation and error handling to mitigate risks from malicious or malformed zip files.
Consider the origin of your zip files. If interoperability with Microsoft-created zip files is necessary, leave `strictFileNames: false`. If strict adherence to the ZIP spec regarding filenames is critical, set `strictFileNames: true` and handle potential errors for invalid filenames.
Unless you have a specific need to handle raw filename buffers (e.g., custom encoding), keep `decodeStrings: true` for proper string decoding and automatic filename validation.
Set `lazyEntries: true` when opening the zip file and manually call `zipfile.readEntry()` after each entry's data stream has finished processing.
Verify that the input file is indeed a valid and uncorrupted `.zip` archive. Check file integrity or source.
Inspect `entry.fileName`. If the name is genuinely unsafe, reject the entry. If backslashes are the issue and expected from non-conformant zips, ensure `strictFileNames: false` (the default) and `decodeStrings: true`.
This indicates a corruption within the zip file's contents for a specific entry. The zip file itself might be damaged. There is no code fix for a corrupted source file; you may need to acquire an uncorrupted archive.
Handle the error gracefully. This is a security feature. The zip file might be intentionally malicious or simply corrupted. Ensure `yauzl` is updated to the latest stable version to benefit from all fixes and validations.