This is a fork of node-unzip which has not been maintained in a while. This fork addresses the following issues:
- finish/close events are not always triggered, particular when the input stream is slower than the receivers
- Any files are buffered into memory before passing on to entry
The stucture of this fork is similar to the original, but uses Promises and inherit guarantees provided by node streams to ensure low memory footprint and guarantee finish/close events at the end of processing. The new
Parser will push any parsed
entries downstream if you pipe from it, while still supporting the legacy
entry event as well.
Breaking changes: The new
Parser will not automatically drain entries if there are no listeners or pipes in place.
Unzipper provides simple APIs similar to node-tar for parsing and extracting zip files. There are no added compiled dependencies - inflation is handled by node.js's built in zlib support.
Please note: Methods that use the Central Directory instead of parsing entire file can be found under
$ npm install unzipper
Extract to a directory
Extract emits the 'close' event once the zip's contents have been fully extracted to disk.
Parse zip file contents
Process each zip file entry or pipe entries to another stream.
Important: If you do not intend to consume an entry stream's raw data, call autodrain() to dispose of the entry's
contents. Otherwise the stream will halt.
.autodrain() returns an empty stream that provides
Additionally you can call
.autodrain().promise() to get the promisified version of success or failure of the autodrain.
// If you want to handle autodrain errors you can either: entry.autodrain().catch(e => handleError); // or entry.autodrain().on('error' => handleError);
Here is a quick example:
Parse zip by piping entries downstream
pipe from unzipper the downstream components will receive each
entry for further processing. This allows for clean pipelines transforming zipfiles into unzipped data.
Example using etl:
Parse a single file and pipe contents
unzipper.parseOne([regex]) is a convenience method that unzips only one file from the archive and pipes the contents down (not the entry itself). If no serch criteria is specified, the first file in the archive will be unzipped. Otherwise, each filename will be compared to the criteria and the first one to match will be unzipped and piped down. If no file matches then the the stream will end without any content.
Buffering the content of an entry into memory
While the recommended strategy of consuming the unzipped contents is using streams, it is sometimes convenient to be able to get the full buffered contents of each file . Each
entry provides a
.buffer function that consumes the entry by buffering the contents into memory and returning a promise to the complete buffer.
Parse.promise() syntax sugar
The parser emits
error events like any other stream. The parser additionally provides a promise wrapper around those two events to allow easy folding into existing Promise based structures.
Previous methods rely on the entire zipfile being received through a pipe. The Open methods load take a different approach: load the central directory first (at the end of the zipfile) and provide the ability to pick and choose which zipfiles to extract, even extracting them in parallel. The open methods return a promise on the contents of the directory, with individual
files listed in an array. Each file element has the following methods:
stream([password])- returns a stream of the unzipped content which can be piped to any destination
buffer([password])- returns a promise on the buffered content of the file) If the file is encrypted you will have to supply a password to decrypt, otherwise you can leave blank.
Unlike adm-zip the Open methods will never read the entire zipfile into buffer.
Returns a Promise to the central directory information with methods to extract individual files.
end options are used to avoid reading the whole file.
Open.url([requestLibrary], [url | options])
This function will return a Promise to the central directory information from a URL point to a zipfile. Range-headers are used to avoid reading the whole file. Unzipper does not ship with a request library so you will have to provide it as the first option.
Live Example: (extracts a tiny xml file from the middle of a 500MB zipfile)
var request = ;var unzipper = ;unzipperOpen;
This function takes a second parameter which can either be a string containing the
url to request, or an
options object to invoke the supplied
request library with. This can be used when other request options are required, such as custom heders or authentication to a third party service.
const request = ;const googleStorageOptions =url: ``qs: alt: 'media'jwt:email: googlestoragecredentialsclient_emailkey: googlestoragecredentialsprivate_keyscopes: '');return unzipperOpen;
This function will return a Promise to the central directory information from a zipfile on S3. Range-headers are used to avoid reading the whole file. Unzipper does not ship with with the aws-sdk so you have to provide an instanciated client as first arguments. The params object requires
Key to fetch the correct file.
var unzipper = ;var AWS = ;var s3Client = AWS;unzipperOpen;
If you already have the zip file in-memory as a buffer, you can open the contents directly.
// never use readFileSync - only used here to simplify the examplevar buffer = fs;unzipperOpen;