pax_global_header00006660000000000000000000000064133165722120014514gustar00rootroot0000000000000052 comment=51010ce4e8c7e6345efe195e1b4150518f37b393 yauzl-2.10.0/000077500000000000000000000000001331657221200127405ustar00rootroot00000000000000yauzl-2.10.0/.gitignore000066400000000000000000000000301331657221200147210ustar00rootroot00000000000000/coverage /node_modules yauzl-2.10.0/.travis.yml000066400000000000000000000003121331657221200150450ustar00rootroot00000000000000language: node_js node_js: - 10 - 8 - 6 - 4 - "0.10" script: - "npm run test-travis" after_script: - "npm install coveralls@2 && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls" yauzl-2.10.0/LICENSE000066400000000000000000000020651331657221200137500ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014 Josh Wolfe Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. yauzl-2.10.0/README.md000066400000000000000000000747111331657221200142310ustar00rootroot00000000000000# yauzl [![Build Status](https://travis-ci.org/thejoshwolfe/yauzl.svg?branch=master)](https://travis-ci.org/thejoshwolfe/yauzl) [![Coverage Status](https://img.shields.io/coveralls/thejoshwolfe/yauzl.svg)](https://coveralls.io/r/thejoshwolfe/yauzl) yet another unzip library for node. For zipping, see [yazl](https://github.com/thejoshwolfe/yazl). Design principles: * Follow the spec. Don't scan for local file headers. Read the central directory for file metadata. (see [No Streaming Unzip API](#no-streaming-unzip-api)). * Don't block the JavaScript thread. Use and provide async APIs. * Keep memory usage under control. Don't attempt to buffer entire files in RAM at once. * Never crash (if used properly). Don't let malformed zip files bring down client applications who are trying to catch errors. * Catch unsafe file names. See `validateFileName()`. ## Usage ```js var yauzl = require("yauzl"); yauzl.open("path/to/file.zip", {lazyEntries: true}, function(err, zipfile) { if (err) throw err; zipfile.readEntry(); zipfile.on("entry", function(entry) { if (/\/$/.test(entry.fileName)) { // Directory file names end with '/'. // Note that entires for directories themselves are optional. // An entry's fileName implicitly requires its parent directories to exist. zipfile.readEntry(); } else { // file entry zipfile.openReadStream(entry, function(err, readStream) { if (err) throw err; readStream.on("end", function() { zipfile.readEntry(); }); readStream.pipe(somewhere); }); } }); }); ``` See also `examples/` for more usage examples. ## API The default for every optional `callback` parameter is: ```js function defaultCallback(err) { if (err) throw err; } ``` ### open(path, [options], [callback]) Calls `fs.open(path, "r")` and reads the `fd` effectively the same as `fromFd()` would. `options` may be omitted or `null`. The defaults are `{autoClose: true, lazyEntries: false, decodeStrings: true, validateEntrySizes: true, strictFileNames: false}`. `autoClose` is effectively equivalent to: ```js zipfile.once("end", function() { zipfile.close(); }); ``` `lazyEntries` indicates that entries should be read only when `readEntry()` is called. If `lazyEntries` is `false`, `entry` events will be emitted as fast as possible to allow `pipe()`ing file data from all entries in parallel. This is not recommended, as it can lead to out of control memory usage for zip files with many entries. See [issue #22](https://github.com/thejoshwolfe/yauzl/issues/22). If `lazyEntries` is `true`, an `entry` or `end` event will be emitted in response to each call to `readEntry()`. This allows processing of one entry at a time, and will keep memory usage under control for zip files with many entries. `decodeStrings` is the default and causes yauzl to decode strings with `CP437` or `UTF-8` as required by the spec. The exact effects of turning this option off are: * `zipfile.comment`, `entry.fileName`, and `entry.fileComment` will be `Buffer` objects instead of `String`s. * Any Info-ZIP Unicode Path Extra Field will be ignored. See `extraFields`. * Automatic file name validation will not be performed. See `validateFileName()`. `validateEntrySizes` is the default and ensures that an entry's reported uncompressed size matches its actual uncompressed size. This check happens as early as possible, which is either before emitting each `"entry"` event (for entries with no compression), or during the `readStream` piping after calling `openReadStream()`. See `openReadStream()` for more information on defending against zip bomb attacks. When `strictFileNames` is `false` (the default) and `decodeStrings` is `true`, all backslash (`\`) characters in each `entry.fileName` are replaced with forward slashes (`/`). The spec forbids file names with backslashes, but Microsoft's `System.IO.Compression.ZipFile` class in .NET versions 4.5.0 until 4.6.1 creates non-conformant zipfiles with backslashes in file names. `strictFileNames` is `false` by default so that clients can read these non-conformant zipfiles without knowing about this Microsoft-specific bug. When `strictFileNames` is `true` and `decodeStrings` is `true`, entries with backslashes in their file names will result in an error. See `validateFileName()`. When `decodeStrings` is `false`, `strictFileNames` has no effect. The `callback` is given the arguments `(err, zipfile)`. An `err` is provided if the End of Central Directory Record cannot be found, or if its metadata appears malformed. This kind of error usually indicates that this is not a zip file. Otherwise, `zipfile` is an instance of `ZipFile`. ### fromFd(fd, [options], [callback]) Reads from the fd, which is presumed to be an open .zip file. Note that random access is required by the zip file specification, so the fd cannot be an open socket or any other fd that does not support random access. `options` may be omitted or `null`. The defaults are `{autoClose: false, lazyEntries: false, decodeStrings: true, validateEntrySizes: true, strictFileNames: false}`. See `open()` for the meaning of the options and callback. ### fromBuffer(buffer, [options], [callback]) Like `fromFd()`, but reads from a RAM buffer instead of an open file. `buffer` is a `Buffer`. If a `ZipFile` is acquired from this method, it will never emit the `close` event, and calling `close()` is not necessary. `options` may be omitted or `null`. The defaults are `{lazyEntries: false, decodeStrings: true, validateEntrySizes: true, strictFileNames: false}`. See `open()` for the meaning of the options and callback. The `autoClose` option is ignored for this method. ### fromRandomAccessReader(reader, totalSize, [options], [callback]) This method of reading a zip file allows clients to implement their own back-end file system. For example, a client might translate read calls into network requests. The `reader` parameter must be of a type that is a subclass of [RandomAccessReader](#class-randomaccessreader) that implements the required methods. The `totalSize` is a Number and indicates the total file size of the zip file. `options` may be omitted or `null`. The defaults are `{autoClose: true, lazyEntries: false, decodeStrings: true, validateEntrySizes: true, strictFileNames: false}`. See `open()` for the meaning of the options and callback. ### dosDateTimeToDate(date, time) Converts MS-DOS `date` and `time` data into a JavaScript `Date` object. Each parameter is a `Number` treated as an unsigned 16-bit integer. Note that this format does not support timezones, so the returned object will use the local timezone. ### validateFileName(fileName) Returns `null` or a `String` error message depending on the validity of `fileName`. If `fileName` starts with `"/"` or `/[A-Za-z]:\//` or if it contains `".."` path segments or `"\\"`, this function returns an error message appropriate for use like this: ```js var errorMessage = yauzl.validateFileName(fileName); if (errorMessage != null) throw new Error(errorMessage); ``` This function is automatically run for each entry, as long as `decodeStrings` is `true`. See `open()`, `strictFileNames`, and `Event: "entry"` for more information. ### Class: ZipFile The constructor for the class is not part of the public API. Use `open()`, `fromFd()`, `fromBuffer()`, or `fromRandomAccessReader()` instead. #### Event: "entry" Callback gets `(entry)`, which is an `Entry`. See `open()` and `readEntry()` for when this event is emitted. If `decodeStrings` is `true`, entries emitted via this event have already passed file name validation. See `validateFileName()` and `open()` for more information. If `validateEntrySizes` is `true` and this entry's `compressionMethod` is `0` (stored without compression), this entry has already passed entry size validation. See `open()` for more information. #### Event: "end" Emitted after the last `entry` event has been emitted. See `open()` and `readEntry()` for more info on when this event is emitted. #### Event: "close" Emitted after the fd is actually closed. This is after calling `close()` (or after the `end` event when `autoClose` is `true`), and after all stream pipelines created from `openReadStream()` have finished reading data from the fd. If this `ZipFile` was acquired from `fromRandomAccessReader()`, the "fd" in the previous paragraph refers to the `RandomAccessReader` implemented by the client. If this `ZipFile` was acquired from `fromBuffer()`, this event is never emitted. #### Event: "error" Emitted in the case of errors with reading the zip file. (Note that other errors can be emitted from the streams created from `openReadStream()` as well.) After this event has been emitted, no further `entry`, `end`, or `error` events will be emitted, but the `close` event may still be emitted. #### readEntry() Causes this `ZipFile` to emit an `entry` or `end` event (or an `error` event). This method must only be called when this `ZipFile` was created with the `lazyEntries` option set to `true` (see `open()`). When this `ZipFile` was created with the `lazyEntries` option set to `true`, `entry` and `end` events are only ever emitted in response to this method call. The event that is emitted in response to this method will not be emitted until after this method has returned, so it is safe to call this method before attaching event listeners. After calling this method, calling this method again before the response event has been emitted will cause undefined behavior. Calling this method after the `end` event has been emitted will cause undefined behavior. Calling this method after calling `close()` will cause undefined behavior. #### openReadStream(entry, [options], callback) `entry` must be an `Entry` object from this `ZipFile`. `callback` gets `(err, readStream)`, where `readStream` is a `Readable Stream` that provides the file data for this entry. If this zipfile is already closed (see `close()`), the `callback` will receive an `err`. `options` may be omitted or `null`, and has the following defaults: ```js { decompress: entry.isCompressed() ? true : null, decrypt: null, start: 0, // actually the default is null, see below end: entry.compressedSize, // actually the default is null, see below } ``` If the entry is compressed (with a supported compression method), and the `decompress` option is `true` (or omitted), the read stream provides the decompressed data. Omitting the `decompress` option is what most clients should do. The `decompress` option must be `null` (or omitted) when the entry is not compressed (see `isCompressed()`), and either `true` (or omitted) or `false` when the entry is compressed. Specifying `decompress: false` for a compressed entry causes the read stream to provide the raw compressed file data without going through a zlib inflate transform. If the entry is encrypted (see `isEncrypted()`), clients may want to avoid calling `openReadStream()` on the entry entirely. Alternatively, clients may call `openReadStream()` for encrypted entries and specify `decrypt: false`. If the entry is also compressed, clients must *also* specify `decompress: false`. Specifying `decrypt: false` for an encrypted entry causes the read stream to provide the raw, still-encrypted file data. (This data includes the 12-byte header described in the spec.) The `decrypt` option must be `null` (or omitted) for non-encrypted entries, and `false` for encrypted entries. Omitting the `decrypt` option (or specifying it as `null`) for an encrypted entry will result in the `callback` receiving an `err`. This default behavior is so that clients not accounting for encrypted files aren't surprised by bogus file data. The `start` (inclusive) and `end` (exclusive) options are byte offsets into this entry's file data, and can be used to obtain part of an entry's file data rather than the whole thing. If either of these options are specified and non-`null`, then the above options must be used to obain the file's raw data. Speficying `{start: 0, end: entry.compressedSize}` will result in the complete file, which is effectively the default values for these options, but note that unlike omitting the options, when you specify `start` or `end` as any non-`null` value, the above requirement is still enforced that you must also pass the appropriate options to get the file's raw data. It's possible for the `readStream` provided to the `callback` to emit errors for several reasons. For example, if zlib cannot decompress the data, the zlib error will be emitted from the `readStream`. Two more error cases (when `validateEntrySizes` is `true`) are if the decompressed data has too many or too few actual bytes compared to the reported byte count from the entry's `uncompressedSize` field. yauzl notices this false information and emits an error from the `readStream` after some number of bytes have already been piped through the stream. This check allows clients to trust the `uncompressedSize` field in `Entry` objects. Guarding against [zip bomb](http://en.wikipedia.org/wiki/Zip_bomb) attacks can be accomplished by doing some heuristic checks on the size metadata and then watching out for the above errors. Such heuristics are outside the scope of this library, but enforcing the `uncompressedSize` is implemented here as a security feature. It is possible to destroy the `readStream` before it has piped all of its data. To do this, call `readStream.destroy()`. You must `unpipe()` the `readStream` from any destination before calling `readStream.destroy()`. If this zipfile was created using `fromRandomAccessReader()`, the `RandomAccessReader` implementation must provide readable streams that implement a `.destroy()` method (see `randomAccessReader._readStreamForRange()`) in order for calls to `readStream.destroy()` to work in this context. #### close() Causes all future calls to `openReadStream()` to fail, and closes the fd, if any, after all streams created by `openReadStream()` have emitted their `end` events. If the `autoClose` option is set to `true` (see `open()`), this function will be called automatically effectively in response to this object's `end` event. If the `lazyEntries` option is set to `false` (see `open()`) and this object's `end` event has not been emitted yet, this function causes undefined behavior. If the `lazyEntries` option is set to `true`, you can call this function instead of calling `readEntry()` to abort reading the entries of a zipfile. It is safe to call this function multiple times; after the first call, successive calls have no effect. This includes situations where the `autoClose` option effectively calls this function for you. If `close()` is never called, then the zipfile is "kept open". For zipfiles created with `fromFd()`, this will leave the `fd` open, which may be desirable. For zipfiles created with `open()`, this will leave the underlying `fd` open, thereby "leaking" it, which is probably undesirable. For zipfiles created with `fromRandomAccessReader()`, the reader's `close()` method will never be called. For zipfiles created with `fromBuffer()`, the `close()` function has no effect whether called or not. Regardless of how this `ZipFile` was created, there are no resources other than those listed above that require cleanup from this function. This means it may be desirable to never call `close()` in some usecases. #### isOpen `Boolean`. `true` until `close()` is called; then it's `false`. #### entryCount `Number`. Total number of central directory records. #### comment `String`. Always decoded with `CP437` per the spec. If `decodeStrings` is `false` (see `open()`), this field is the undecoded `Buffer` instead of a decoded `String`. ### Class: Entry Objects of this class represent Central Directory Records. Refer to the zipfile specification for more details about these fields. These fields are of type `Number`: * `versionMadeBy` * `versionNeededToExtract` * `generalPurposeBitFlag` * `compressionMethod` * `lastModFileTime` (MS-DOS format, see `getLastModDateTime`) * `lastModFileDate` (MS-DOS format, see `getLastModDateTime`) * `crc32` * `compressedSize` * `uncompressedSize` * `fileNameLength` (bytes) * `extraFieldLength` (bytes) * `fileCommentLength` (bytes) * `internalFileAttributes` * `externalFileAttributes` * `relativeOffsetOfLocalHeader` #### fileName `String`. Following the spec, the bytes for the file name are decoded with `UTF-8` if `generalPurposeBitFlag & 0x800`, otherwise with `CP437`. Alternatively, this field may be populated from the Info-ZIP Unicode Path Extra Field (see `extraFields`). This field is automatically validated by `validateFileName()` before yauzl emits an "entry" event. If this field would contain unsafe characters, yauzl emits an error instead of an entry. If `decodeStrings` is `false` (see `open()`), this field is the undecoded `Buffer` instead of a decoded `String`. Therefore, `generalPurposeBitFlag` and any Info-ZIP Unicode Path Extra Field are ignored. Furthermore, no automatic file name validation is performed for this file name. #### extraFields `Array` with each entry in the form `{id: id, data: data}`, where `id` is a `Number` and `data` is a `Buffer`. This library looks for and reads the ZIP64 Extended Information Extra Field (0x0001) in order to support ZIP64 format zip files. This library also looks for and reads the Info-ZIP Unicode Path Extra Field (0x7075) in order to support some zipfiles that use it instead of General Purpose Bit 11 to convey `UTF-8` file names. When the field is identified and verified to be reliable (see the zipfile spec), the the file name in this field is stored in the `fileName` property, and the file name in the central directory record for this entry is ignored. Note that when `decodeStrings` is false, all Info-ZIP Unicode Path Extra Fields are ignored. None of the other fields are considered significant by this library. Fields that this library reads are left unalterned in the `extraFields` array. #### fileComment `String` decoded with the charset indicated by `generalPurposeBitFlag & 0x800` as with the `fileName`. (The Info-ZIP Unicode Path Extra Field has no effect on the charset used for this field.) If `decodeStrings` is `false` (see `open()`), this field is the undecoded `Buffer` instead of a decoded `String`. Prior to yauzl version 2.7.0, this field was erroneously documented as `comment` instead of `fileComment`. For compatibility with any code that uses the field name `comment`, yauzl creates an alias field named `comment` which is identical to `fileComment`. #### getLastModDate() Effectively implemented as: ```js return dosDateTimeToDate(this.lastModFileDate, this.lastModFileTime); ``` #### isEncrypted() Returns is this entry encrypted with "Traditional Encryption". Effectively implemented as: ```js return (this.generalPurposeBitFlag & 0x1) !== 0; ``` See `openReadStream()` for the implications of this value. Note that "Strong Encryption" is not supported, and will result in an `"error"` event emitted from the `ZipFile`. #### isCompressed() Effectively implemented as: ```js return this.compressionMethod === 8; ``` See `openReadStream()` for the implications of this value. ### Class: RandomAccessReader This class is meant to be subclassed by clients and instantiated for the `fromRandomAccessReader()` function. An example implementation can be found in `test/test.js`. #### randomAccessReader._readStreamForRange(start, end) Subclasses *must* implement this method. `start` and `end` are Numbers and indicate byte offsets from the start of the file. `end` is exclusive, so `_readStreamForRange(0x1000, 0x2000)` would indicate to read `0x1000` bytes. `end - start` will always be at least `1`. This method should return a readable stream which will be `pipe()`ed into another stream. It is expected that the readable stream will provide data in several chunks if necessary. If the readable stream provides too many or too few bytes, an error will be emitted. (Note that `validateEntrySizes` has no effect on this check, because this is a low-level API that should behave correctly regardless of the contents of the file.) Any errors emitted on the readable stream will be handled and re-emitted on the client-visible stream (returned from `zipfile.openReadStream()`) or provided as the `err` argument to the appropriate callback (for example, for `fromRandomAccessReader()`). The returned stream *must* implement a method `.destroy()` if you call `readStream.destroy()` on streams you get from `openReadStream()`. If you never call `readStream.destroy()`, then streams returned from this method do not need to implement a method `.destroy()`. `.destroy()` should abort any streaming that is in progress and clean up any associated resources. `.destroy()` will only be called after the stream has been `unpipe()`d from its destination. Note that the stream returned from this method might not be the same object that is provided by `openReadStream()`. The stream returned from this method might be `pipe()`d through one or more filter streams (for example, a zlib inflate stream). #### randomAccessReader.read(buffer, offset, length, position, callback) Subclasses may implement this method. The default implementation uses `createReadStream()` to fill the `buffer`. This method should behave like `fs.read()`. #### randomAccessReader.close(callback) Subclasses may implement this method. The default implementation is effectively `setImmediate(callback);`. `callback` takes parameters `(err)`. This method is called once the all streams returned from `_readStreamForRange()` have ended, and no more `_readStreamForRange()` or `read()` requests will be issued to this object. ## How to Avoid Crashing When a malformed zipfile is encountered, the default behavior is to crash (throw an exception). If you want to handle errors more gracefully than this, be sure to do the following: * Provide `callback` parameters where they are allowed, and check the `err` parameter. * Attach a listener for the `error` event on any `ZipFile` object you get from `open()`, `fromFd()`, `fromBuffer()`, or `fromRandomAccessReader()`. * Attach a listener for the `error` event on any stream you get from `openReadStream()`. Minor version updates to yauzl will not add any additional requirements to this list. ## Limitations ### No Streaming Unzip API Due to the design of the .zip file format, it's impossible to interpret a .zip file from start to finish (such as from a readable stream) without sacrificing correctness. The Central Directory, which is the authority on the contents of the .zip file, is at the end of a .zip file, not the beginning. A streaming API would need to either buffer the entire .zip file to get to the Central Directory before interpreting anything (defeating the purpose of a streaming interface), or rely on the Local File Headers which are interspersed through the .zip file. However, the Local File Headers are explicitly denounced in the spec as being unreliable copies of the Central Directory, so trusting them would be a violation of the spec. Any library that offers a streaming unzip API must make one of the above two compromises, which makes the library either dishonest or nonconformant (usually the latter). This library insists on correctness and adherence to the spec, and so does not offer a streaming API. Here is a way to create a spec-conformant .zip file using the `zip` command line program (Info-ZIP) available in most unix-like environments, that is (nearly) impossible to parse correctly with a streaming parser: ``` $ echo -ne '\x50\x4b\x07\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' > file.txt $ zip -q0 - file.txt | cat > out.zip ``` This .zip file contains a single file entry that uses General Purpose Bit 3, which means the Local File Header doesn't know the size of the file. Any streaming parser that encounters this situation will either immediately fail, or attempt to search for the Data Descriptor after the file's contents. The file's contents is a sequence of 16-bytes crafted to exactly mimic a valid Data Descriptor for an empty file, which will fool any parser that gets this far into thinking that the file is empty rather than containing 16-bytes. What follows the file's real contents is the file's real Data Descriptor, which will likely cause some kind of signature mismatch error for a streaming parser (if one hasn't occurred already). By using General Purpose Bit 3 (and compression method 0), it's possible to create arbitrarily ambiguous .zip files that distract parsers with file contents that contain apparently valid .zip file metadata. ### Limitted ZIP64 Support For ZIP64, only zip files smaller than `8PiB` are supported, not the full `16EiB` range that a 64-bit integer should be able to index. This is due to the JavaScript Number type being an IEEE 754 double precision float. The Node.js `fs` module probably has this same limitation. ### ZIP64 Extensible Data Sector Is Ignored The spec does not allow zip file creators to put arbitrary data here, but rather reserves its use for PKWARE and mentions something about Z390. This doesn't seem useful to expose in this library, so it is ignored. ### No Multi-Disk Archive Support This library does not support multi-disk zip files. The multi-disk fields in the zipfile spec were intended for a zip file to span multiple floppy disks, which probably never happens now. If the "number of this disk" field in the End of Central Directory Record is not `0`, the `open()`, `fromFd()`, `fromBuffer()`, or `fromRandomAccessReader()` `callback` will receive an `err`. By extension the following zip file fields are ignored by this library and not provided to clients: * Disk where central directory starts * Number of central directory records on this disk * Disk number where file starts ### Limited Encryption Handling You can detect when a file entry is encrypted with "Traditional Encryption" via `isEncrypted()`, but yauzl will not help you decrypt it. See `openReadStream()`. If a zip file contains file entries encrypted with "Strong Encryption", yauzl emits an error. If the central directory is encrypted or compressed, yauzl emits an error. ### Local File Headers Are Ignored Many unzip libraries mistakenly read the Local File Header data in zip files. This data is officially defined to be redundant with the Central Directory information, and is not to be trusted. Aside from checking the signature, yauzl ignores the content of the Local File Header. ### No CRC-32 Checking This library provides the `crc32` field of `Entry` objects read from the Central Directory. However, this field is not used for anything in this library. ### versionNeededToExtract Is Ignored The field `versionNeededToExtract` is ignored, because this library doesn't support the complete zip file spec at any version, ### No Support For Obscure Compression Methods Regarding the `compressionMethod` field of `Entry` objects, only method `0` (stored with no compression) and method `8` (deflated) are supported. Any of the other 15 official methods will cause the `openReadStream()` `callback` to receive an `err`. ### Data Descriptors Are Ignored There may or may not be Data Descriptor sections in a zip file. This library provides no support for finding or interpreting them. ### Archive Extra Data Record Is Ignored There may or may not be an Archive Extra Data Record section in a zip file. This library provides no support for finding or interpreting it. ### No Language Encoding Flag Support Zip files officially support charset encodings other than CP437 and UTF-8, but the zip file spec does not specify how it works. This library makes no attempt to interpret the Language Encoding Flag. ## Change History * 2.10.0 * Added support for non-conformant zipfiles created by Microsoft, and added option `strictFileNames` to disable the workaround. [issue #66](https://github.com/thejoshwolfe/yauzl/issues/66), [issue #88](https://github.com/thejoshwolfe/yauzl/issues/88) * 2.9.2 * Removed `tools/hexdump-zip.js` and `tools/hex2bin.js`. Those tools are now located here: [thejoshwolfe/hexdump-zip](https://github.com/thejoshwolfe/hexdump-zip) and [thejoshwolfe/hex2bin](https://github.com/thejoshwolfe/hex2bin) * Worked around performance problem with zlib when using `fromBuffer()` and `readStream.destroy()` for large compressed files. [issue #87](https://github.com/thejoshwolfe/yauzl/issues/87) * 2.9.1 * Removed `console.log()` accidentally introduced in 2.9.0. [issue #64](https://github.com/thejoshwolfe/yauzl/issues/64) * 2.9.0 * Throw an exception if `readEntry()` is called without `lazyEntries:true`. Previously this caused undefined behavior. [issue #63](https://github.com/thejoshwolfe/yauzl/issues/63) * 2.8.0 * Added option `validateEntrySizes`. [issue #53](https://github.com/thejoshwolfe/yauzl/issues/53) * Added `examples/promises.js` * Added ability to read raw file data via `decompress` and `decrypt` options. [issue #11](https://github.com/thejoshwolfe/yauzl/issues/11), [issue #38](https://github.com/thejoshwolfe/yauzl/issues/38), [pull #39](https://github.com/thejoshwolfe/yauzl/pull/39) * Added `start` and `end` options to `openReadStream()`. [issue #38](https://github.com/thejoshwolfe/yauzl/issues/38) * 2.7.0 * Added option `decodeStrings`. [issue #42](https://github.com/thejoshwolfe/yauzl/issues/42) * Fixed documentation for `entry.fileComment` and added compatibility alias. [issue #47](https://github.com/thejoshwolfe/yauzl/issues/47) * 2.6.0 * Support Info-ZIP Unicode Path Extra Field, used by WinRAR for Chinese file names. [issue #33](https://github.com/thejoshwolfe/yauzl/issues/33) * 2.5.0 * Ignore malformed Extra Field that is common in Android .apk files. [issue #31](https://github.com/thejoshwolfe/yauzl/issues/31) * 2.4.3 * Fix crash when parsing malformed Extra Field buffers. [issue #31](https://github.com/thejoshwolfe/yauzl/issues/31) * 2.4.2 * Remove .npmignore and .travis.yml from npm package. * 2.4.1 * Fix error handling. * 2.4.0 * Add ZIP64 support. [issue #6](https://github.com/thejoshwolfe/yauzl/issues/6) * Add `lazyEntries` option. [issue #22](https://github.com/thejoshwolfe/yauzl/issues/22) * Add `readStream.destroy()` method. [issue #26](https://github.com/thejoshwolfe/yauzl/issues/26) * Add `fromRandomAccessReader()`. [issue #14](https://github.com/thejoshwolfe/yauzl/issues/14) * Add `examples/unzip.js`. * 2.3.1 * Documentation updates. * 2.3.0 * Check that `uncompressedSize` is correct, or else emit an error. [issue #13](https://github.com/thejoshwolfe/yauzl/issues/13) * 2.2.1 * Update dependencies. * 2.2.0 * Update dependencies. * 2.1.0 * Remove dependency on `iconv`. * 2.0.3 * Fix crash when trying to read a 0-byte file. * 2.0.2 * Fix event behavior after errors. * 2.0.1 * Fix bug with using `iconv`. * 2.0.0 * Initial release. yauzl-2.10.0/examples/000077500000000000000000000000001331657221200145565ustar00rootroot00000000000000yauzl-2.10.0/examples/dump.js000066400000000000000000000013041331657221200160570ustar00rootroot00000000000000 var yauzl = require("../"); var paths = []; var dumpContents = true; process.argv.slice(2).forEach(function(arg) { if (arg === "--no-contents") { dumpContents = false; } else { paths.push(arg); } }); paths.forEach(function(path) { yauzl.open(path, function(err, zipfile) { if (err) throw err; zipfile.on("error", function(err) { throw err; }); zipfile.on("entry", function(entry) { console.log(entry); console.log(entry.getLastModDate()); if (!dumpContents || /\/$/.exec(entry)) return; zipfile.openReadStream(entry, function(err, readStream) { if (err) throw err; readStream.pipe(process.stdout); }); }); }); }); yauzl-2.10.0/examples/promises.js000066400000000000000000000032501331657221200167550ustar00rootroot00000000000000// yauzl does not provide a Promise API, // but yauzl's API follows the Node.js convention of // using (err, result) callbacks as the final parameter. // This lends itself cleanly to "promisifying" the API // as shown in this example. // // This example requires V8 version 5.5+ (Node version 7.6+). // While async/await is still experimental, you also need // to run this example with --harmony-async-await let yauzl = require("../"); let simpleZipBuffer = new Buffer([ 80,75,3,4,20,0,8,8,0,0,134,96,146,74,0,0, 0,0,0,0,0,0,0,0,0,0,5,0,0,0,97,46,116,120, 116,104,101,108,108,111,10,80,75,7,8,32, 48,58,54,6,0,0,0,6,0,0,0,80,75,1,2,63,3, 20,0,8,8,0,0,134,96,146,74,32,48,58,54,6, 0,0,0,6,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,180, 129,0,0,0,0,97,46,116,120,116,80,75,5,6,0, 0,0,0,1,0,1,0,51,0,0,0,57,0,0,0,0,0 ]); function promisify(api) { return function(...args) { return new Promise(function(resolve, reject) { api(...args, function(err, response) { if (err) return reject(err); resolve(response); }); }); }; } let yauzlFromBuffer = promisify(yauzl.fromBuffer); (async () => { let zipfile = await yauzlFromBuffer(simpleZipBuffer, {lazyEntries: true}); console.log("number of entries:", zipfile.entryCount); let openReadStream = promisify(zipfile.openReadStream.bind(zipfile)); zipfile.readEntry(); zipfile.on("entry", async (entry) => { console.log("found entry:", entry.fileName); let stream = await openReadStream(entry); stream.on("end", () => { console.log(""); zipfile.readEntry(); }); stream.pipe(process.stdout); }); zipfile.on("end", () => { console.log("end of entries"); }); })(); yauzl-2.10.0/examples/unzip.js000066400000000000000000000154041331657221200162650ustar00rootroot00000000000000 var yauzl = require("../"); var path = require("path"); var fs = require("fs"); var util = require("util"); var Transform = require("stream").Transform; var zipFilePath; var offsetArg; var lenArg; var endArg; var args = process.argv.slice(2); for (var i = 0; i < args.length; i++) { var arg = args[i]; if (arg === "--offset") { i += 1; offsetArg = parseInt(args[i]); if (isNaN(offsetArg)) throw new Error("--offset argument not parsable as an int"); } else if (arg === "--len") { i += 1; lenArg = parseInt(args[i]); if (isNaN(lenArg)) throw new Error("--len argument not parsable as an int"); } else if (arg === "--end") { i += 1; endArg = parseInt(args[i]); if (isNaN(endArg)) throw new Error("--end argument not parsable as an int"); } else if (["-h", "--help"].indexOf(arg) !== -1) { // print help zipFilePath = null; break; } else if (/^--/.test(arg)) { throw new Error("unrecognized option: " + arg); } else { if (zipFilePath != null) throw new Error("too many arguments"); zipFilePath = arg; } } if (zipFilePath == null || /^-/.test(zipFilePath) || (lenArg != null && endArg != null)) { console.log( "usage: node unzip.js [options] path/to/file.zip\n" + "\n" + "unzips the specified zip file into the current directory\n" + "\n" + "options:\n" + " --offset START\n" + " --len LEN\n" + " --end END\n" + " interprets the middle of the specified file as a zipfile.\n" + " starting START number of bytes in from the beginning (default 0).\n" + " end with length of LEN (default is all the way to the end of the file).\n" + " or end at byte offset END (exclusive) (default is the end of the file).\n" + " end can be negative to count backwards from the end of the file\n" + " (example, `--end -1` excludes the last byte of the file).\n" + ""); process.exit(1); } function mkdirp(dir, cb) { if (dir === ".") return cb(); fs.stat(dir, function(err) { if (err == null) return cb(); // already exists var parent = path.dirname(dir); mkdirp(parent, function() { process.stdout.write(dir.replace(/\/$/, "") + "/\n"); fs.mkdir(dir, cb); }); }); } if (offsetArg != null || lenArg != null || endArg != null) { openMiddleOfFile(zipFilePath, {lazyEntries: true}, offsetArg, lenArg, endArg, handleZipFile); } else { yauzl.open(zipFilePath, {lazyEntries: true}, handleZipFile); } function openMiddleOfFile(zipFilePath, options, offsetArg, lenArg, endArg, handleZipFile) { fs.open(zipFilePath, "r", function(err, fd) { if (err != null) throw err; fs.fstat(fd, function(err, stats) { // resolve optional parameters if (offsetArg == null) offsetArg = 0; if (lenArg == null && endArg == null) endArg = stats.size; if (endArg == null) endArg = lenArg + offsetArg; else if (endArg < 0) endArg = stats.size + endArg; // validate parameters if (offsetArg < 0) throw new Error("--offset < 0"); if (lenArg < 0) throw new Error("--len < 0"); if (offsetArg > endArg) throw new Error("--offset > --end"); if (endArg > stats.size) throw new Error("--end/--len goes past EOF"); function adjustOffset(n) { return n + offsetArg; } // extend RandomAccessReader function MiddleOfFileReader() { yauzl.RandomAccessReader.call(this); } util.inherits(MiddleOfFileReader, yauzl.RandomAccessReader); // implement required and option methods MiddleOfFileReader.prototype._readStreamForRange = function(start, end) { return fs.createReadStream(null, { fd: fd, // shift the start and end offsets start: start + offsetArg, end: end + offsetArg - 1, // the -1 is because fs.createReadStream()'s end option is inclusive autoClose: false, }); }; MiddleOfFileReader.prototype.read = function(buffer, offset, length, position, callback) { // shift the position fs.read(fd, buffer, offset, length, position + offsetArg, callback); }; MiddleOfFileReader.prototype.close = function(callback) { fs.close(fd, callback); }; yauzl.fromRandomAccessReader(new MiddleOfFileReader(), endArg - offsetArg, options, handleZipFile); }); }); } function handleZipFile(err, zipfile) { if (err) throw err; // track when we've closed all our file handles var handleCount = 0; function incrementHandleCount() { handleCount++; } function decrementHandleCount() { handleCount--; if (handleCount === 0) { console.log("all input and output handles closed"); } } incrementHandleCount(); zipfile.on("close", function() { console.log("closed input file"); decrementHandleCount(); }); zipfile.readEntry(); zipfile.on("entry", function(entry) { if (/\/$/.test(entry.fileName)) { // directory file names end with '/' mkdirp(entry.fileName, function() { if (err) throw err; zipfile.readEntry(); }); } else { // ensure parent directory exists mkdirp(path.dirname(entry.fileName), function() { zipfile.openReadStream(entry, function(err, readStream) { if (err) throw err; // report progress through large files var byteCount = 0; var totalBytes = entry.uncompressedSize; var lastReportedString = byteCount + "/" + totalBytes + " 0%"; process.stdout.write(entry.fileName + "..." + lastReportedString); function reportString(msg) { var clearString = ""; for (var i = 0; i < lastReportedString.length; i++) { clearString += "\b"; if (i >= msg.length) { clearString += " \b"; } } process.stdout.write(clearString + msg); lastReportedString = msg; } // report progress at 60Hz var progressInterval = setInterval(function() { reportString(byteCount + "/" + totalBytes + " " + ((byteCount / totalBytes * 100) | 0) + "%"); }, 1000 / 60); var filter = new Transform(); filter._transform = function(chunk, encoding, cb) { byteCount += chunk.length; cb(null, chunk); }; filter._flush = function(cb) { clearInterval(progressInterval); reportString(""); // delete the "..." process.stdout.write("\b \b\b \b\b \b\n"); cb(); zipfile.readEntry(); }; // pump file contents var writeStream = fs.createWriteStream(entry.fileName); incrementHandleCount(); writeStream.on("close", decrementHandleCount); readStream.pipe(filter).pipe(writeStream); }); }); } }); } yauzl-2.10.0/index.js000066400000000000000000001004551331657221200144120ustar00rootroot00000000000000var fs = require("fs"); var zlib = require("zlib"); var fd_slicer = require("fd-slicer"); var crc32 = require("buffer-crc32"); var util = require("util"); var EventEmitter = require("events").EventEmitter; var Transform = require("stream").Transform; var PassThrough = require("stream").PassThrough; var Writable = require("stream").Writable; exports.open = open; exports.fromFd = fromFd; exports.fromBuffer = fromBuffer; exports.fromRandomAccessReader = fromRandomAccessReader; exports.dosDateTimeToDate = dosDateTimeToDate; exports.validateFileName = validateFileName; exports.ZipFile = ZipFile; exports.Entry = Entry; exports.RandomAccessReader = RandomAccessReader; function open(path, options, callback) { if (typeof options === "function") { callback = options; options = null; } if (options == null) options = {}; if (options.autoClose == null) options.autoClose = true; if (options.lazyEntries == null) options.lazyEntries = false; if (options.decodeStrings == null) options.decodeStrings = true; if (options.validateEntrySizes == null) options.validateEntrySizes = true; if (options.strictFileNames == null) options.strictFileNames = false; if (callback == null) callback = defaultCallback; fs.open(path, "r", function(err, fd) { if (err) return callback(err); fromFd(fd, options, function(err, zipfile) { if (err) fs.close(fd, defaultCallback); callback(err, zipfile); }); }); } function fromFd(fd, options, callback) { if (typeof options === "function") { callback = options; options = null; } if (options == null) options = {}; if (options.autoClose == null) options.autoClose = false; if (options.lazyEntries == null) options.lazyEntries = false; if (options.decodeStrings == null) options.decodeStrings = true; if (options.validateEntrySizes == null) options.validateEntrySizes = true; if (options.strictFileNames == null) options.strictFileNames = false; if (callback == null) callback = defaultCallback; fs.fstat(fd, function(err, stats) { if (err) return callback(err); var reader = fd_slicer.createFromFd(fd, {autoClose: true}); fromRandomAccessReader(reader, stats.size, options, callback); }); } function fromBuffer(buffer, options, callback) { if (typeof options === "function") { callback = options; options = null; } if (options == null) options = {}; options.autoClose = false; if (options.lazyEntries == null) options.lazyEntries = false; if (options.decodeStrings == null) options.decodeStrings = true; if (options.validateEntrySizes == null) options.validateEntrySizes = true; if (options.strictFileNames == null) options.strictFileNames = false; // limit the max chunk size. see https://github.com/thejoshwolfe/yauzl/issues/87 var reader = fd_slicer.createFromBuffer(buffer, {maxChunkSize: 0x10000}); fromRandomAccessReader(reader, buffer.length, options, callback); } function fromRandomAccessReader(reader, totalSize, options, callback) { if (typeof options === "function") { callback = options; options = null; } if (options == null) options = {}; if (options.autoClose == null) options.autoClose = true; if (options.lazyEntries == null) options.lazyEntries = false; if (options.decodeStrings == null) options.decodeStrings = true; var decodeStrings = !!options.decodeStrings; if (options.validateEntrySizes == null) options.validateEntrySizes = true; if (options.strictFileNames == null) options.strictFileNames = false; if (callback == null) callback = defaultCallback; if (typeof totalSize !== "number") throw new Error("expected totalSize parameter to be a number"); if (totalSize > Number.MAX_SAFE_INTEGER) { throw new Error("zip file too large. only file sizes up to 2^52 are supported due to JavaScript's Number type being an IEEE 754 double."); } // the matching unref() call is in zipfile.close() reader.ref(); // eocdr means End of Central Directory Record. // search backwards for the eocdr signature. // the last field of the eocdr is a variable-length comment. // the comment size is encoded in a 2-byte field in the eocdr, which we can't find without trudging backwards through the comment to find it. // as a consequence of this design decision, it's possible to have ambiguous zip file metadata if a coherent eocdr was in the comment. // we search backwards for a eocdr signature, and hope that whoever made the zip file was smart enough to forbid the eocdr signature in the comment. var eocdrWithoutCommentSize = 22; var maxCommentSize = 0xffff; // 2-byte size var bufferSize = Math.min(eocdrWithoutCommentSize + maxCommentSize, totalSize); var buffer = newBuffer(bufferSize); var bufferReadStart = totalSize - buffer.length; readAndAssertNoEof(reader, buffer, 0, bufferSize, bufferReadStart, function(err) { if (err) return callback(err); for (var i = bufferSize - eocdrWithoutCommentSize; i >= 0; i -= 1) { if (buffer.readUInt32LE(i) !== 0x06054b50) continue; // found eocdr var eocdrBuffer = buffer.slice(i); // 0 - End of central directory signature = 0x06054b50 // 4 - Number of this disk var diskNumber = eocdrBuffer.readUInt16LE(4); if (diskNumber !== 0) { return callback(new Error("multi-disk zip files are not supported: found disk number: " + diskNumber)); } // 6 - Disk where central directory starts // 8 - Number of central directory records on this disk // 10 - Total number of central directory records var entryCount = eocdrBuffer.readUInt16LE(10); // 12 - Size of central directory (bytes) // 16 - Offset of start of central directory, relative to start of archive var centralDirectoryOffset = eocdrBuffer.readUInt32LE(16); // 20 - Comment length var commentLength = eocdrBuffer.readUInt16LE(20); var expectedCommentLength = eocdrBuffer.length - eocdrWithoutCommentSize; if (commentLength !== expectedCommentLength) { return callback(new Error("invalid comment length. expected: " + expectedCommentLength + ". found: " + commentLength)); } // 22 - Comment // the encoding is always cp437. var comment = decodeStrings ? decodeBuffer(eocdrBuffer, 22, eocdrBuffer.length, false) : eocdrBuffer.slice(22); if (!(entryCount === 0xffff || centralDirectoryOffset === 0xffffffff)) { return callback(null, new ZipFile(reader, centralDirectoryOffset, totalSize, entryCount, comment, options.autoClose, options.lazyEntries, decodeStrings, options.validateEntrySizes, options.strictFileNames)); } // ZIP64 format // ZIP64 Zip64 end of central directory locator var zip64EocdlBuffer = newBuffer(20); var zip64EocdlOffset = bufferReadStart + i - zip64EocdlBuffer.length; readAndAssertNoEof(reader, zip64EocdlBuffer, 0, zip64EocdlBuffer.length, zip64EocdlOffset, function(err) { if (err) return callback(err); // 0 - zip64 end of central dir locator signature = 0x07064b50 if (zip64EocdlBuffer.readUInt32LE(0) !== 0x07064b50) { return callback(new Error("invalid zip64 end of central directory locator signature")); } // 4 - number of the disk with the start of the zip64 end of central directory // 8 - relative offset of the zip64 end of central directory record var zip64EocdrOffset = readUInt64LE(zip64EocdlBuffer, 8); // 16 - total number of disks // ZIP64 end of central directory record var zip64EocdrBuffer = newBuffer(56); readAndAssertNoEof(reader, zip64EocdrBuffer, 0, zip64EocdrBuffer.length, zip64EocdrOffset, function(err) { if (err) return callback(err); // 0 - zip64 end of central dir signature 4 bytes (0x06064b50) if (zip64EocdrBuffer.readUInt32LE(0) !== 0x06064b50) { return callback(new Error("invalid zip64 end of central directory record signature")); } // 4 - size of zip64 end of central directory record 8 bytes // 12 - version made by 2 bytes // 14 - version needed to extract 2 bytes // 16 - number of this disk 4 bytes // 20 - number of the disk with the start of the central directory 4 bytes // 24 - total number of entries in the central directory on this disk 8 bytes // 32 - total number of entries in the central directory 8 bytes entryCount = readUInt64LE(zip64EocdrBuffer, 32); // 40 - size of the central directory 8 bytes // 48 - offset of start of central directory with respect to the starting disk number 8 bytes centralDirectoryOffset = readUInt64LE(zip64EocdrBuffer, 48); // 56 - zip64 extensible data sector (variable size) return callback(null, new ZipFile(reader, centralDirectoryOffset, totalSize, entryCount, comment, options.autoClose, options.lazyEntries, decodeStrings, options.validateEntrySizes, options.strictFileNames)); }); }); return; } callback(new Error("end of central directory record signature not found")); }); } util.inherits(ZipFile, EventEmitter); function ZipFile(reader, centralDirectoryOffset, fileSize, entryCount, comment, autoClose, lazyEntries, decodeStrings, validateEntrySizes, strictFileNames) { var self = this; EventEmitter.call(self); self.reader = reader; // forward close events self.reader.on("error", function(err) { // error closing the fd emitError(self, err); }); self.reader.once("close", function() { self.emit("close"); }); self.readEntryCursor = centralDirectoryOffset; self.fileSize = fileSize; self.entryCount = entryCount; self.comment = comment; self.entriesRead = 0; self.autoClose = !!autoClose; self.lazyEntries = !!lazyEntries; self.decodeStrings = !!decodeStrings; self.validateEntrySizes = !!validateEntrySizes; self.strictFileNames = !!strictFileNames; self.isOpen = true; self.emittedError = false; if (!self.lazyEntries) self._readEntry(); } ZipFile.prototype.close = function() { if (!this.isOpen) return; this.isOpen = false; this.reader.unref(); }; function emitErrorAndAutoClose(self, err) { if (self.autoClose) self.close(); emitError(self, err); } function emitError(self, err) { if (self.emittedError) return; self.emittedError = true; self.emit("error", err); } ZipFile.prototype.readEntry = function() { if (!this.lazyEntries) throw new Error("readEntry() called without lazyEntries:true"); this._readEntry(); }; ZipFile.prototype._readEntry = function() { var self = this; if (self.entryCount === self.entriesRead) { // done with metadata setImmediate(function() { if (self.autoClose) self.close(); if (self.emittedError) return; self.emit("end"); }); return; } if (self.emittedError) return; var buffer = newBuffer(46); readAndAssertNoEof(self.reader, buffer, 0, buffer.length, self.readEntryCursor, function(err) { if (err) return emitErrorAndAutoClose(self, err); if (self.emittedError) return; var entry = new Entry(); // 0 - Central directory file header signature var signature = buffer.readUInt32LE(0); if (signature !== 0x02014b50) return emitErrorAndAutoClose(self, new Error("invalid central directory file header signature: 0x" + signature.toString(16))); // 4 - Version made by entry.versionMadeBy = buffer.readUInt16LE(4); // 6 - Version needed to extract (minimum) entry.versionNeededToExtract = buffer.readUInt16LE(6); // 8 - General purpose bit flag entry.generalPurposeBitFlag = buffer.readUInt16LE(8); // 10 - Compression method entry.compressionMethod = buffer.readUInt16LE(10); // 12 - File last modification time entry.lastModFileTime = buffer.readUInt16LE(12); // 14 - File last modification date entry.lastModFileDate = buffer.readUInt16LE(14); // 16 - CRC-32 entry.crc32 = buffer.readUInt32LE(16); // 20 - Compressed size entry.compressedSize = buffer.readUInt32LE(20); // 24 - Uncompressed size entry.uncompressedSize = buffer.readUInt32LE(24); // 28 - File name length (n) entry.fileNameLength = buffer.readUInt16LE(28); // 30 - Extra field length (m) entry.extraFieldLength = buffer.readUInt16LE(30); // 32 - File comment length (k) entry.fileCommentLength = buffer.readUInt16LE(32); // 34 - Disk number where file starts // 36 - Internal file attributes entry.internalFileAttributes = buffer.readUInt16LE(36); // 38 - External file attributes entry.externalFileAttributes = buffer.readUInt32LE(38); // 42 - Relative offset of local file header entry.relativeOffsetOfLocalHeader = buffer.readUInt32LE(42); if (entry.generalPurposeBitFlag & 0x40) return emitErrorAndAutoClose(self, new Error("strong encryption is not supported")); self.readEntryCursor += 46; buffer = newBuffer(entry.fileNameLength + entry.extraFieldLength + entry.fileCommentLength); readAndAssertNoEof(self.reader, buffer, 0, buffer.length, self.readEntryCursor, function(err) { if (err) return emitErrorAndAutoClose(self, err); if (self.emittedError) return; // 46 - File name var isUtf8 = (entry.generalPurposeBitFlag & 0x800) !== 0; entry.fileName = self.decodeStrings ? decodeBuffer(buffer, 0, entry.fileNameLength, isUtf8) : buffer.slice(0, entry.fileNameLength); // 46+n - Extra field var fileCommentStart = entry.fileNameLength + entry.extraFieldLength; var extraFieldBuffer = buffer.slice(entry.fileNameLength, fileCommentStart); entry.extraFields = []; var i = 0; while (i < extraFieldBuffer.length - 3) { var headerId = extraFieldBuffer.readUInt16LE(i + 0); var dataSize = extraFieldBuffer.readUInt16LE(i + 2); var dataStart = i + 4; var dataEnd = dataStart + dataSize; if (dataEnd > extraFieldBuffer.length) return emitErrorAndAutoClose(self, new Error("extra field length exceeds extra field buffer size")); var dataBuffer = newBuffer(dataSize); extraFieldBuffer.copy(dataBuffer, 0, dataStart, dataEnd); entry.extraFields.push({ id: headerId, data: dataBuffer, }); i = dataEnd; } // 46+n+m - File comment entry.fileComment = self.decodeStrings ? decodeBuffer(buffer, fileCommentStart, fileCommentStart + entry.fileCommentLength, isUtf8) : buffer.slice(fileCommentStart, fileCommentStart + entry.fileCommentLength); // compatibility hack for https://github.com/thejoshwolfe/yauzl/issues/47 entry.comment = entry.fileComment; self.readEntryCursor += buffer.length; self.entriesRead += 1; if (entry.uncompressedSize === 0xffffffff || entry.compressedSize === 0xffffffff || entry.relativeOffsetOfLocalHeader === 0xffffffff) { // ZIP64 format // find the Zip64 Extended Information Extra Field var zip64EiefBuffer = null; for (var i = 0; i < entry.extraFields.length; i++) { var extraField = entry.extraFields[i]; if (extraField.id === 0x0001) { zip64EiefBuffer = extraField.data; break; } } if (zip64EiefBuffer == null) { return emitErrorAndAutoClose(self, new Error("expected zip64 extended information extra field")); } var index = 0; // 0 - Original Size 8 bytes if (entry.uncompressedSize === 0xffffffff) { if (index + 8 > zip64EiefBuffer.length) { return emitErrorAndAutoClose(self, new Error("zip64 extended information extra field does not include uncompressed size")); } entry.uncompressedSize = readUInt64LE(zip64EiefBuffer, index); index += 8; } // 8 - Compressed Size 8 bytes if (entry.compressedSize === 0xffffffff) { if (index + 8 > zip64EiefBuffer.length) { return emitErrorAndAutoClose(self, new Error("zip64 extended information extra field does not include compressed size")); } entry.compressedSize = readUInt64LE(zip64EiefBuffer, index); index += 8; } // 16 - Relative Header Offset 8 bytes if (entry.relativeOffsetOfLocalHeader === 0xffffffff) { if (index + 8 > zip64EiefBuffer.length) { return emitErrorAndAutoClose(self, new Error("zip64 extended information extra field does not include relative header offset")); } entry.relativeOffsetOfLocalHeader = readUInt64LE(zip64EiefBuffer, index); index += 8; } // 24 - Disk Start Number 4 bytes } // check for Info-ZIP Unicode Path Extra Field (0x7075) // see https://github.com/thejoshwolfe/yauzl/issues/33 if (self.decodeStrings) { for (var i = 0; i < entry.extraFields.length; i++) { var extraField = entry.extraFields[i]; if (extraField.id === 0x7075) { if (extraField.data.length < 6) { // too short to be meaningful continue; } // Version 1 byte version of this extra field, currently 1 if (extraField.data.readUInt8(0) !== 1) { // > Changes may not be backward compatible so this extra // > field should not be used if the version is not recognized. continue; } // NameCRC32 4 bytes File Name Field CRC32 Checksum var oldNameCrc32 = extraField.data.readUInt32LE(1); if (crc32.unsigned(buffer.slice(0, entry.fileNameLength)) !== oldNameCrc32) { // > If the CRC check fails, this UTF-8 Path Extra Field should be // > ignored and the File Name field in the header should be used instead. continue; } // UnicodeName Variable UTF-8 version of the entry File Name entry.fileName = decodeBuffer(extraField.data, 5, extraField.data.length, true); break; } } } // validate file size if (self.validateEntrySizes && entry.compressionMethod === 0) { var expectedCompressedSize = entry.uncompressedSize; if (entry.isEncrypted()) { // traditional encryption prefixes the file data with a header expectedCompressedSize += 12; } if (entry.compressedSize !== expectedCompressedSize) { var msg = "compressed/uncompressed size mismatch for stored file: " + entry.compressedSize + " != " + entry.uncompressedSize; return emitErrorAndAutoClose(self, new Error(msg)); } } if (self.decodeStrings) { if (!self.strictFileNames) { // allow backslash entry.fileName = entry.fileName.replace(/\\/g, "/"); } var errorMessage = validateFileName(entry.fileName, self.validateFileNameOptions); if (errorMessage != null) return emitErrorAndAutoClose(self, new Error(errorMessage)); } self.emit("entry", entry); if (!self.lazyEntries) self._readEntry(); }); }); }; ZipFile.prototype.openReadStream = function(entry, options, callback) { var self = this; // parameter validation var relativeStart = 0; var relativeEnd = entry.compressedSize; if (callback == null) { callback = options; options = {}; } else { // validate options that the caller has no excuse to get wrong if (options.decrypt != null) { if (!entry.isEncrypted()) { throw new Error("options.decrypt can only be specified for encrypted entries"); } if (options.decrypt !== false) throw new Error("invalid options.decrypt value: " + options.decrypt); if (entry.isCompressed()) { if (options.decompress !== false) throw new Error("entry is encrypted and compressed, and options.decompress !== false"); } } if (options.decompress != null) { if (!entry.isCompressed()) { throw new Error("options.decompress can only be specified for compressed entries"); } if (!(options.decompress === false || options.decompress === true)) { throw new Error("invalid options.decompress value: " + options.decompress); } } if (options.start != null || options.end != null) { if (entry.isCompressed() && options.decompress !== false) { throw new Error("start/end range not allowed for compressed entry without options.decompress === false"); } if (entry.isEncrypted() && options.decrypt !== false) { throw new Error("start/end range not allowed for encrypted entry without options.decrypt === false"); } } if (options.start != null) { relativeStart = options.start; if (relativeStart < 0) throw new Error("options.start < 0"); if (relativeStart > entry.compressedSize) throw new Error("options.start > entry.compressedSize"); } if (options.end != null) { relativeEnd = options.end; if (relativeEnd < 0) throw new Error("options.end < 0"); if (relativeEnd > entry.compressedSize) throw new Error("options.end > entry.compressedSize"); if (relativeEnd < relativeStart) throw new Error("options.end < options.start"); } } // any further errors can either be caused by the zipfile, // or were introduced in a minor version of yauzl, // so should be passed to the client rather than thrown. if (!self.isOpen) return callback(new Error("closed")); if (entry.isEncrypted()) { if (options.decrypt !== false) return callback(new Error("entry is encrypted, and options.decrypt !== false")); } // make sure we don't lose the fd before we open the actual read stream self.reader.ref(); var buffer = newBuffer(30); readAndAssertNoEof(self.reader, buffer, 0, buffer.length, entry.relativeOffsetOfLocalHeader, function(err) { try { if (err) return callback(err); // 0 - Local file header signature = 0x04034b50 var signature = buffer.readUInt32LE(0); if (signature !== 0x04034b50) { return callback(new Error("invalid local file header signature: 0x" + signature.toString(16))); } // all this should be redundant // 4 - Version needed to extract (minimum) // 6 - General purpose bit flag // 8 - Compression method // 10 - File last modification time // 12 - File last modification date // 14 - CRC-32 // 18 - Compressed size // 22 - Uncompressed size // 26 - File name length (n) var fileNameLength = buffer.readUInt16LE(26); // 28 - Extra field length (m) var extraFieldLength = buffer.readUInt16LE(28); // 30 - File name // 30+n - Extra field var localFileHeaderEnd = entry.relativeOffsetOfLocalHeader + buffer.length + fileNameLength + extraFieldLength; var decompress; if (entry.compressionMethod === 0) { // 0 - The file is stored (no compression) decompress = false; } else if (entry.compressionMethod === 8) { // 8 - The file is Deflated decompress = options.decompress != null ? options.decompress : true; } else { return callback(new Error("unsupported compression method: " + entry.compressionMethod)); } var fileDataStart = localFileHeaderEnd; var fileDataEnd = fileDataStart + entry.compressedSize; if (entry.compressedSize !== 0) { // bounds check now, because the read streams will probably not complain loud enough. // since we're dealing with an unsigned offset plus an unsigned size, // we only have 1 thing to check for. if (fileDataEnd > self.fileSize) { return callback(new Error("file data overflows file bounds: " + fileDataStart + " + " + entry.compressedSize + " > " + self.fileSize)); } } var readStream = self.reader.createReadStream({ start: fileDataStart + relativeStart, end: fileDataStart + relativeEnd, }); var endpointStream = readStream; if (decompress) { var destroyed = false; var inflateFilter = zlib.createInflateRaw(); readStream.on("error", function(err) { // setImmediate here because errors can be emitted during the first call to pipe() setImmediate(function() { if (!destroyed) inflateFilter.emit("error", err); }); }); readStream.pipe(inflateFilter); if (self.validateEntrySizes) { endpointStream = new AssertByteCountStream(entry.uncompressedSize); inflateFilter.on("error", function(err) { // forward zlib errors to the client-visible stream setImmediate(function() { if (!destroyed) endpointStream.emit("error", err); }); }); inflateFilter.pipe(endpointStream); } else { // the zlib filter is the client-visible stream endpointStream = inflateFilter; } // this is part of yauzl's API, so implement this function on the client-visible stream endpointStream.destroy = function() { destroyed = true; if (inflateFilter !== endpointStream) inflateFilter.unpipe(endpointStream); readStream.unpipe(inflateFilter); // TODO: the inflateFilter may cause a memory leak. see Issue #27. readStream.destroy(); }; } callback(null, endpointStream); } finally { self.reader.unref(); } }); }; function Entry() { } Entry.prototype.getLastModDate = function() { return dosDateTimeToDate(this.lastModFileDate, this.lastModFileTime); }; Entry.prototype.isEncrypted = function() { return (this.generalPurposeBitFlag & 0x1) !== 0; }; Entry.prototype.isCompressed = function() { return this.compressionMethod === 8; }; function dosDateTimeToDate(date, time) { var day = date & 0x1f; // 1-31 var month = (date >> 5 & 0xf) - 1; // 1-12, 0-11 var year = (date >> 9 & 0x7f) + 1980; // 0-128, 1980-2108 var millisecond = 0; var second = (time & 0x1f) * 2; // 0-29, 0-58 (even numbers) var minute = time >> 5 & 0x3f; // 0-59 var hour = time >> 11 & 0x1f; // 0-23 return new Date(year, month, day, hour, minute, second, millisecond); } function validateFileName(fileName) { if (fileName.indexOf("\\") !== -1) { return "invalid characters in fileName: " + fileName; } if (/^[a-zA-Z]:/.test(fileName) || /^\//.test(fileName)) { return "absolute path: " + fileName; } if (fileName.split("/").indexOf("..") !== -1) { return "invalid relative path: " + fileName; } // all good return null; } function readAndAssertNoEof(reader, buffer, offset, length, position, callback) { if (length === 0) { // fs.read will throw an out-of-bounds error if you try to read 0 bytes from a 0 byte file return setImmediate(function() { callback(null, newBuffer(0)); }); } reader.read(buffer, offset, length, position, function(err, bytesRead) { if (err) return callback(err); if (bytesRead < length) { return callback(new Error("unexpected EOF")); } callback(); }); } util.inherits(AssertByteCountStream, Transform); function AssertByteCountStream(byteCount) { Transform.call(this); this.actualByteCount = 0; this.expectedByteCount = byteCount; } AssertByteCountStream.prototype._transform = function(chunk, encoding, cb) { this.actualByteCount += chunk.length; if (this.actualByteCount > this.expectedByteCount) { var msg = "too many bytes in the stream. expected " + this.expectedByteCount + ". got at least " + this.actualByteCount; return cb(new Error(msg)); } cb(null, chunk); }; AssertByteCountStream.prototype._flush = function(cb) { if (this.actualByteCount < this.expectedByteCount) { var msg = "not enough bytes in the stream. expected " + this.expectedByteCount + ". got only " + this.actualByteCount; return cb(new Error(msg)); } cb(); }; util.inherits(RandomAccessReader, EventEmitter); function RandomAccessReader() { EventEmitter.call(this); this.refCount = 0; } RandomAccessReader.prototype.ref = function() { this.refCount += 1; }; RandomAccessReader.prototype.unref = function() { var self = this; self.refCount -= 1; if (self.refCount > 0) return; if (self.refCount < 0) throw new Error("invalid unref"); self.close(onCloseDone); function onCloseDone(err) { if (err) return self.emit('error', err); self.emit('close'); } }; RandomAccessReader.prototype.createReadStream = function(options) { var start = options.start; var end = options.end; if (start === end) { var emptyStream = new PassThrough(); setImmediate(function() { emptyStream.end(); }); return emptyStream; } var stream = this._readStreamForRange(start, end); var destroyed = false; var refUnrefFilter = new RefUnrefFilter(this); stream.on("error", function(err) { setImmediate(function() { if (!destroyed) refUnrefFilter.emit("error", err); }); }); refUnrefFilter.destroy = function() { stream.unpipe(refUnrefFilter); refUnrefFilter.unref(); stream.destroy(); }; var byteCounter = new AssertByteCountStream(end - start); refUnrefFilter.on("error", function(err) { setImmediate(function() { if (!destroyed) byteCounter.emit("error", err); }); }); byteCounter.destroy = function() { destroyed = true; refUnrefFilter.unpipe(byteCounter); refUnrefFilter.destroy(); }; return stream.pipe(refUnrefFilter).pipe(byteCounter); }; RandomAccessReader.prototype._readStreamForRange = function(start, end) { throw new Error("not implemented"); }; RandomAccessReader.prototype.read = function(buffer, offset, length, position, callback) { var readStream = this.createReadStream({start: position, end: position + length}); var writeStream = new Writable(); var written = 0; writeStream._write = function(chunk, encoding, cb) { chunk.copy(buffer, offset + written, 0, chunk.length); written += chunk.length; cb(); }; writeStream.on("finish", callback); readStream.on("error", function(error) { callback(error); }); readStream.pipe(writeStream); }; RandomAccessReader.prototype.close = function(callback) { setImmediate(callback); }; util.inherits(RefUnrefFilter, PassThrough); function RefUnrefFilter(context) { PassThrough.call(this); this.context = context; this.context.ref(); this.unreffedYet = false; } RefUnrefFilter.prototype._flush = function(cb) { this.unref(); cb(); }; RefUnrefFilter.prototype.unref = function(cb) { if (this.unreffedYet) return; this.unreffedYet = true; this.context.unref(); }; var cp437 = '\u0000☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ '; function decodeBuffer(buffer, start, end, isUtf8) { if (isUtf8) { return buffer.toString("utf8", start, end); } else { var result = ""; for (var i = start; i < end; i++) { result += cp437[buffer[i]]; } return result; } } function readUInt64LE(buffer, offset) { // there is no native function for this, because we can't actually store 64-bit integers precisely. // after 53 bits, JavaScript's Number type (IEEE 754 double) can't store individual integers anymore. // but since 53 bits is a whole lot more than 32 bits, we do our best anyway. var lower32 = buffer.readUInt32LE(offset); var upper32 = buffer.readUInt32LE(offset + 4); // we can't use bitshifting here, because JavaScript bitshifting only works on 32-bit integers. return upper32 * 0x100000000 + lower32; // as long as we're bounds checking the result of this function against the total file size, // we'll catch any overflow errors, because we already made sure the total file size was within reason. } // Node 10 deprecated new Buffer(). var newBuffer; if (typeof Buffer.allocUnsafe === "function") { newBuffer = function(len) { return Buffer.allocUnsafe(len); }; } else { newBuffer = function(len) { return new Buffer(len); }; } function defaultCallback(err) { if (err) throw err; } yauzl-2.10.0/package.json000066400000000000000000000015611331657221200152310ustar00rootroot00000000000000{ "name": "yauzl", "version": "2.10.0", "description": "yet another unzip library for node", "main": "index.js", "scripts": { "test": "node test/test.js", "test-cov": "istanbul cover test/test.js", "test-travis": "istanbul cover --report lcovonly test/test.js" }, "repository": { "type": "git", "url": "https://github.com/thejoshwolfe/yauzl.git" }, "keywords": [ "unzip", "zip", "stream", "archive", "file" ], "author": "Josh Wolfe ", "license": "MIT", "bugs": { "url": "https://github.com/thejoshwolfe/yauzl/issues" }, "homepage": "https://github.com/thejoshwolfe/yauzl", "dependencies": { "fd-slicer": "~1.1.0", "buffer-crc32": "~0.2.3" }, "devDependencies": { "bl": "~1.0.0", "istanbul": "~0.3.4", "pend": "~1.2.0" }, "files": [ "index.js" ] } yauzl-2.10.0/test/000077500000000000000000000000001331657221200137175ustar00rootroot00000000000000yauzl-2.10.0/test/big-compression.zip000066400000000000000000000022131331657221200175410ustar00rootroot00000000000000PKlTG0x1000001 Om_>PK8 PKlTG8 0x100000PK6?yauzl-2.10.0/test/failure/000077500000000000000000000000001331657221200153465ustar00rootroot00000000000000yauzl-2.10.0/test/failure/absolute path C xt.zip000066400000000000000000000004641331657221200214100ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQVC:/xtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/failure/absolute path atxt.zip000066400000000000000000000004641331657221200215720ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQV/atxtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/failure/compressed uncompressed size mismatch for stored file 2147483647 5.zip000066400000000000000000000004641331657221200316060ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQVa.txtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/failure/end of central directory record signature not found.zip000066400000000000000000000004641331657221200277250ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQVa.txtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/failure/end of central directory record signature not found_1.zip000066400000000000000000000000001331657221200301270ustar00rootroot00000000000000yauzl-2.10.0/test/failure/expected zip64 extended information extra field.zip000066400000000000000000000003641331657221200270720ustar00rootroot00000000000000PK-fH empty.txtPKPK?-fH empty.txtPK,?-S?PKPKyauzl-2.10.0/test/failure/extra field length exceeds extra field buffer size.zip000066400000000000000000000004641331657221200274650ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQVa.txtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/failure/file data overflows file bounds 63 2147483647 308.zip000066400000000000000000000004641331657221200256530ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQVa.txtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/failure/invalid central directory file header signature 0x1014b50.zip000066400000000000000000000004641331657221200303420ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQVa.txtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/failure/invalid characters in fileName a txt.zip000066400000000000000000000004641331657221200247150ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQVa\txtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/failure/invalid code lengths set.zip000066400000000000000000000056611331657221200226240ustar00rootroot00000000000000PK E )index.jsUT WSWSux [o~C#ۺUov\IZ" $Ų69s 9dj 9sΙ܇z 5+p>%,MJ..H YU A%򑆼rIsEg4`˶}4HH<Z"K ۪.ء t;Cd MzE@LQOK%Q¬72I%֎ir: ҲU(d~ ,Q=漨J5#~ ?ڍI$&4OoI2"8V=&RAq8 ,64CyX>aK*63ْf~SXlC*< {4(8 A}PD%\ RFJCj/$\M"11 =+I@ ͱ`IBmIS@=SEG4",PQc߱pG ^ A{ zsDV\ncF 4PVDY xaӫ,YJtö5Kt^CRZQP`>iб"Aۃxu6TpPi$bVUN8{4eDe"qda^ J OhX U< ~xHentaS1-;HӔ;o P11Eu)0螼 ABV<-wa~y,SlldfƱ'i0^0D]hg5(srC헬Z.وqv9q +DIŲ6ɤ{qx&0ϰ9aC{km"@(Zkkno_uA&Y([5ZYg@]?؍HcCtpBgbqHuŅFD*2[bȲ]ziƇ\9twg, tq2J>J5}Ê aei ^%]6f" [V*)^ pjUcd(y8l߉-7Iyb"V,{$x#$ +}U?(zJsˮx;Y.l pe&{ J [rNeaf*{X5sFK1fK8 op(㥅xR~i+ܞe-?ZP9$cHN,h *H1&GK HP߿>U,Y^IN  B?Y6(%LEڿ뗸ZR Y]*_y~\W(nvة,< ױL'(sW+U|5 0e݇k$O\ŕ7u3KBt8LZ脅<{*MHl 37WXXXԷ/rY_;8ݼ-3@6CFQDVe嵳I ߾*޼mVp77ṑإ} mMmI)(ıV6 &]`sdvmi۬=Y:3m&)וV}Dͭu'Gӝٗt= }sgscFN=Z2@ޝW8ނ .:|G?YySvmX͖-w\a (: Фfz=ݜCN+fУ5࣫_(w\<ֈrx lGp.w{[Qp( CFV]w{+iU\mj:}؊X%b#uflG~mc;,w]wh%ζH9%6{ ; ;9f^ o}C] 0k[]@[*]̕~5V}/^p/ӹIRė2gukC%@<5ԝQ7G ]2-*loOk]6EPK E )index.jsUTWSux PKNM yauzl-2.10.0/test/failure/invalid comment length expected 1 found 0.zip000066400000000000000000000004651331657221200255310ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQVa.txtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/failure/invalid local file header signature 0x3034b50.zip000066400000000000000000000004641331657221200260230ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQVa.txtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/failure/invalid relative path xt.zip000066400000000000000000000004641331657221200226510ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQV../xtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/failure/invalid zip64 end of central directory locator signature.zip000066400000000000000000000001421331657221200305720ustar00rootroot00000000000000PK,?-PKPKyauzl-2.10.0/test/failure/invalid zip64 end of central directory record signature.zip000066400000000000000000000001421331657221200304050ustar00rootroot00000000000000PK,?-PKPKyauzl-2.10.0/test/failure/multi-disk zip files are not supported found disk number 1.zip000066400000000000000000000004641331657221200307460ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQVa.txtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/failure/not enough bytes in the stream expected 2048576 got only 1000000.zip000066400000000000000000000022011331657221200305450ustar00rootroot00000000000000PKGFy@B zeros.binUT e!@U\!@Uux  nH@PKGFy@B zeros.binUTe!@Uux PKOyauzl-2.10.0/test/failure/strong encryption is not supported.zip000066400000000000000000000003021331657221200247370ustar00rootroot00000000000000PK J 0:6a.txtUT :XEXux (=0X&]b\PK 0:6PK IJ 0:6a.txtUT:Xux PKKayauzl-2.10.0/test/failure/too many bytes in the stream expected 82496 got at least 98304.zip000066400000000000000000000022011331657221200304650ustar00rootroot00000000000000PKGFy@B zeros.binUT e!@U\!@Uux  nH@PKGFy@B zeros.binUTe!@Uux PKOyauzl-2.10.0/test/failure/too many length or distance symbols.zip000066400000000000000000000056611331657221200247170ustar00rootroot00000000000000PK E )index.jsUT WSWSux Z[o~C#ۺUov\IZ" $Ų69s 9dj 9sΙ܇z 5+p>%,MJ..H YU A%򑆼rIsEg4`˶}4HH<Z"K ۪.ء t;Cd MzE@LQOK%Q¬72I%֎ir: ҲU(d~ ,Q=漨J5#~ ?ڍI$&4OoI2"8V=&RAq8 ,64CyX>aK*63ْf~SXlC*< {4(8 A}PD%\ RFJCj/$\M"11 =+I@ ͱ`IBmIS@=SEG4",PQc߱pG ^ A{ zsDV\ncF 4PVDY xaӫ,YJtö5Kt^CRZQP`>iб"Aۃxu6TpPi$bVUN8{4eDe"qda^ J OhX U< ~xHentaS1-;HӔ;o P11Eu)0螼 ABV<-wa~y,SlldfƱ'i0^0D]hg5(srC헬Z.وqv9q +DIŲ6ɤ{qx&0ϰ9aC{km"@(Zkkno_uA&Y([5ZYg@]?؍HcCtpBgbqHuŅFD*2[bȲ]ziƇ\9twg, tq2J>J5}Ê aei ^%]6f" [V*)^ pjUcd(y8l߉-7Iyb"V,{$x#$ +}U?(zJsˮx;Y.l pe&{ J [rNeaf*{X5sFK1fK8 op(㥅xR~i+ܞe-?ZP9$cHN,h *H1&GK HP߿>U,Y^IN  B?Y6(%LEڿ뗸ZR Y]*_y~\W(nvة,< ױL'(sW+U|5 0e݇k$O\ŕ7u3KBt8LZ脅<{*MHl 37WXXXԷ/rY_;8ݼ-3@6CFQDVe嵳I ߾*޼mVp77ṑإ} mMmI)(ıV6 &]`sdvmi۬=Y:3m&)וV}Dͭu'Gӝٗt= }sgscFN=Z2@ޝW8ނ .:|G?YySvmX͖-w\a (: Фfz=ݜCN+fУ5࣫_(w\<ֈrx lGp.w{[Qp( CFV]w{+iU\mj:}؊X%b#uflG~mc;,w]wh%ζH9%6{ ; ;9f^ o}C] 0k[]@[*]̕~5V}/^p/ӹIRė2gukC%@<5ԝQ7G ]2-*loOk]6EPK E )index.jsUTWSux PKNM yauzl-2.10.0/test/failure/unsupported compression method 1.zip000066400000000000000000000004641331657221200243720ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQVa.txtUT>*Sux PK KEDb.txtUT>*Sux PKzip64 extended information extra field does not include compressed size.zip000066400000000000000000000003641331657221200334110ustar00rootroot00000000000000yauzl-2.10.0/test/failurePK-c H empty.txtPKPK?-c H empty.txtPK,?-S?PKPKzip64 extended information extra field does not include relative header offset.zip000066400000000000000000000003641331657221200346050ustar00rootroot00000000000000yauzl-2.10.0/test/failurePK-c H empty.txtPKPK?-c H empty.txtPK,?-S?PKPKzip64 extended information extra field does not include uncompressed size.zip000066400000000000000000000003641331657221200337540ustar00rootroot00000000000000yauzl-2.10.0/test/failurePK-c H empty.txtPKPK?-c H empty.txtPK,?-S?PKPKyauzl-2.10.0/test/range-test.js000066400000000000000000000145271331657221200163370ustar00rootroot00000000000000var yauzl = require("../"); var PassThrough = require("stream").PassThrough; var util = require("util"); var Pend = require("pend"); var BufferList = require("bl"); exports.runTest = runTest; // zipfile obtained via: // $ echo -n 'aaabaaabaaabaaab' > stored.txt // $ cp stored.txt compressed.txt // $ cp stored.txt encrypted.txt // $ cp stored.txt encrypted-and-compressed.txt // $ rm -f out.zip // $ zip out.zip -0 stored.txt // $ zip out.zip compressed.txt // $ zip out.zip -e0 encrypted.txt // $ zip out.zip -e encrypted-and-compressed.txt var zipfileBuffer = hexToBuffer("" + "504b03040a00000000006a54954ab413389510000000100000000a001c007374" + "6f7265642e7478745554090003d842fa5842c5f75875780b000104e803000004" + "e803000061616162616161626161616261616162504b03041400000008007554" + "954ab413389508000000100000000e001c00636f6d707265737365642e747874" + "5554090003ed42fa58ed42fa5875780b000104e803000004e80300004b4c4c4c" + "4a44c200504b03040a00090000008454954ab41338951c000000100000000d00" + "1c00656e637279707465642e74787455540900030743fa580743fa5875780b00" + "0104e803000004e8030000f72e7bb915142131c934f01b163fcadb2a8db7cdaf" + "d0a6f4dd1694c0504b0708b41338951c00000010000000504b03041400090008" + "008a54954ab413389514000000100000001c001c00656e637279707465642d61" + "6e642d636f6d707265737365642e74787455540900031343fa581343fa587578" + "0b000104e803000004e80300007c4d3ea0d9754b470d3eb32ada5741bfc848f4" + "19504b0708b41338951400000010000000504b01021e030a00000000006a5495" + "4ab413389510000000100000000a0018000000000000000000b4810000000073" + "746f7265642e7478745554050003d842fa5875780b000104e803000004e80300" + "00504b01021e031400000008007554954ab413389508000000100000000e0018" + "000000000001000000b48154000000636f6d707265737365642e747874555405" + "0003ed42fa5875780b000104e803000004e8030000504b01021e030a00090000" + "008454954ab41338951c000000100000000d0018000000000000000000b481a4" + "000000656e637279707465642e74787455540500030743fa5875780b000104e8" + "03000004e8030000504b01021e031400090008008a54954ab413389514000000" + "100000001c0018000000000001000000b48117010000656e637279707465642d" + "616e642d636f6d707265737365642e74787455540500031343fa5875780b0001" + "04e803000004e8030000504b0506000000000400040059010000910100000000" + ""); // the same file in all 4 supported forms: // [0b00]: stored // [0b01]: compressed // [0b10]: encrypted // [0b11]: encrypted and compressed function shouldBeCompressed(index) { return (index & 1) !== 0; } function shouldBeEncrypted (index) { return (index & 2) !== 0; } var expectedFileDatas = [ hexToBuffer("61616162616161626161616261616162"), hexToBuffer("4b4c4c4c4a44c200"), hexToBuffer("f72e7bb915142131c934f01b163fcadb2a8db7cdafd0a6f4dd1694c0"), hexToBuffer("7c4d3ea0d9754b470d3eb32ada5741bfc848f419"), ]; function runTest(cb) { util.inherits(StingyRandomAccessReader, yauzl.RandomAccessReader); function StingyRandomAccessReader(buffer) { yauzl.RandomAccessReader.call(this); this.buffer = buffer; this.upcomingByteCounts = []; } StingyRandomAccessReader.prototype._readStreamForRange = function(start, end) { if (this.upcomingByteCounts.length > 0) { var expectedByteCount = this.upcomingByteCounts.shift(); if (expectedByteCount != null) { if (expectedByteCount !== end - start) { throw new Error("expected " + expectedByteCount + " got " + (end - start) + " bytes"); } } } var result = new PassThrough(); result.write(this.buffer.slice(start, end)); result.end(); return result; }; var zipfileReader = new StingyRandomAccessReader(zipfileBuffer); var options = {lazyEntries: true, autoClose: false}; yauzl.fromRandomAccessReader(zipfileReader, zipfileBuffer.length, options, function(err, zipfile) { var entries = []; zipfile.readEntry(); zipfile.on("entry", function(entry) { var index = entries.length; // asser the structure of the zipfile is what we expect if (entry.isCompressed() !== shouldBeCompressed(index)) throw new Error("assertion failure"); if (entry.isEncrypted() !== shouldBeEncrypted(index)) throw new Error("assertion failure"); entries.push(entry); zipfile.readEntry(); }); zipfile.on("end", function() { // now we get to the testing var pend = new Pend(); // 1 thing at a time for better determinism/reproducibility pend.max = 1; [null, 0, 2].forEach(function(start) { [null, 3, 5].forEach(function(end) { entries.forEach(function(entry, index) { var expectedFileData = expectedFileDatas[index]; pend.go(function(cb) { var effectiveStart = start != null ? start : 0; var effectiveEnd = end != null ? end : expectedFileData.length; var expectedSlice = expectedFileData.slice(effectiveStart, effectiveEnd); // the next read will be to check the local file header. // then we assert that yauzl is asking for just the bytes we asked for. zipfileReader.upcomingByteCounts = [null, expectedSlice.length]; var options = {}; if (start != null) options.start = start; if (end != null) options.end = end; if (entry.isCompressed()) options.decompress = false; if (entry.isEncrypted()) options.decrypt = false; zipfile.openReadStream(entry, options, function(err, readStream) { if (err) throw err; readStream.pipe(BufferList(function(err, data) { var prefix = "openReadStream with range(" + start + "," + end + "," + index + "): "; if (!buffersEqual(data, expectedSlice)) { throw new Error(prefix + "contents mismatch"); } console.log(prefix + "pass"); cb(); })); }); }); }); }); }); pend.wait(cb); }); }); } function hexToBuffer(hexString) { var buffer = new Buffer(hexString.length / 2); for (var i = 0; i < buffer.length; i++) { buffer[i] = parseInt(hexString.substr(i * 2, 2), 16); } return buffer; } function buffersEqual(a, b) { if (a.length !== b.length) return false; for (var i = 0; i < a.length; i++) { if (a[i] !== b[i]) return false; } return true; } if (require.main === module) runTest(function() {}); yauzl-2.10.0/test/success/000077500000000000000000000000001331657221200153675ustar00rootroot00000000000000yauzl-2.10.0/test/success/cygwin-info-zip.zip000066400000000000000000000004641331657221200211500ustar00rootroot00000000000000PK KEQVa.txtUT =*S=*Sux +z+asdf PK KEb.txtUT =*S=*Sux +z+bsdf PK KEQVa.txtUT=*Sux +z+PK KEDb.txtUT=*Sux +z+PKyauzl-2.10.0/test/success/cygwin-info-zip/000077500000000000000000000000001331657221200204205ustar00rootroot00000000000000yauzl-2.10.0/test/success/cygwin-info-zip/a.txt000066400000000000000000000000051331657221200213740ustar00rootroot00000000000000asdf yauzl-2.10.0/test/success/cygwin-info-zip/b.txt000066400000000000000000000000051331657221200213750ustar00rootroot00000000000000bsdf yauzl-2.10.0/test/success/deflate.zip000066400000000000000000000056611331657221200175270ustar00rootroot00000000000000PK E )index.jsUT WSWSux Z[o~C#ۺUov\IZ" $Ų69s 9dj 9sΙ܇z 5+p>%,MJ..H YU A%򑆼rIsEg4`˶}4HH<Z"K ۪.ء t;Cd MzE@LQOK%Q¬72I%֎ir: ҲU(d~ ,Q=漨J5#~ ?ڍI$&4OoI2"8V=&RAq8 ,64CyX>aK*63ْf~SXlC*< {4(8 A}PD%\ RFJCj/$\M"11 =+I@ ͱ`IBmIS@=SEG4",PQc߱pG ^ A{ zsDV\ncF 4PVDY xaӫ,YJtö5Kt^CRZQP`>iб"Aۃxu6TpPi$bVUN8{4eDe"qda^ J OhX U< ~xHentaS1-;HӔ;o P11Eu)0螼 ABV<-wa~y,SlldfƱ'i0^0D]hg5(srC헬Z.وqv9q +DIŲ6ɤ{qx&0ϰ9aC{km"@(Zkkno_uA&Y([5ZYg@]?؍HcCtpBgbqHuŅFD*2[bȲ]ziƇ\9twg, tq2J>J5}Ê aei ^%]6f" [V*)^ pjUcd(y8l߉-7Iyb"V,{$x#$ +}U?(zJsˮx;Y.l pe&{ J [rNeaf*{X5sFK1fK8 op(㥅xR~i+ܞe-?ZP9$cHN,h *H1&GK HP߿>U,Y^IN  B?Y6(%LEڿ뗸ZR Y]*_y~\W(nvة,< ױL'(sW+U|5 0e݇k$O\ŕ7u3KBt8LZ脅<{*MHl 37WXXXԷ/rY_;8ݼ-3@6CFQDVe嵳I ߾*޼mVp77ṑإ} mMmI)(ıV6 &]`sdvmi۬=Y:3m&)וV}Dͭu'Gӝٗt= }sgscFN=Z2@ޝW8ނ .:|G?YySvmX͖-w\a (: Фfz=ݜCN+fУ5࣫_(w\<ֈrx lGp.w{[Qp( CFV]w{+iU\mj:}؊X%b#uflG~mc;,w]wh%ζH9%6{ ; ;9f^ o}C] 0k[]@[*]̕~5V}/^p/ӹIRė2gukC%@<5ԝQ7G ]2-*loOk]6EPK E )index.jsUTWSux PKNM yauzl-2.10.0/test/success/deflate/000077500000000000000000000000001331657221200167735ustar00rootroot00000000000000yauzl-2.10.0/test/success/deflate/index.js000066400000000000000000000247011331657221200204440ustar00rootroot00000000000000var fs = require("fs"); var FdSlicer = require("fd-slicer"); // cd - Central Directory // cdr - Central Directory Record // eocdr - End of Central Directory Record open("test/cygwin-info-zip.zip", function(err, zipfile) { if (err) throw err; console.log("entries:", zipfile.entriesRemaining()); keepReading(); function keepReading() { if (zipfile.entriesRemaining() === 0) return; zipfile.readEntry(function(err, entry) { if (err) throw err; console.log(entry); zipfile.openReadStream(entry, function(err, readStream) { if (err) throw err; readStream.pipe(process.stdout); }); keepReading(); }); } }); module.exports.open = open; function open(path, callback) { if (callback == null) callback = defaultCallback; fs.open(path, "r", function(err, fd) { if (err) return callback(err); fopen(fd, function(err, zipfile) { if (err) fs.close(fd, defaultCallback); callback(err, zipfile); }); }); } module.exports.fopen = fopen; function fopen(fd, callback) { if (callback == null) callback = defaultCallback; fs.fstat(fd, function(err, stats) { if (err) return callback(err); verbose("searching backwards for the eocdr signature"); // the last field of the eocdr is a variable-length comment. // the comment size is encoded in a 2-byte field in the eocdr, which we can't find without trudging backwards through the comment to find it. // as a consequence of this design decision, it's possible to have ambiguous zip file metadata if, for example, a coherent eocdr was in the comment. // we search backwards for the first eocdr signature, and hope that whoever made the zip file was smart enough to forbid the eocdr signature in the comment. var eocdrWithoutCommentSize = 22; var maxCommentSize = 0x10000; // 2-byte size var bufferSize = Math.min(eocdrWithoutCommentSize + maxCommentSize, stats.size); var buffer = new Buffer(bufferSize); var bufferReadStart = stats.size - buffer.length; readNoEof(fd, buffer, 0, bufferSize, bufferReadStart, function(err) { if (err) return callback(err); for (var i = bufferSize - eocdrWithoutCommentSize; i >= 0; i -= 1) { if (buffer.readUInt32LE(i) !== 0x06054b50) continue; verbose("found eocdr at offset: " + (bufferReadStart + i)); var eocdrBuffer = buffer.slice(i); // 0 - End of central directory signature = 0x06054b50 // 4 - Number of this disk var diskNumber = eocdrBuffer.readUInt16LE(4); if (diskNumber !== 0) return callback(new Error("multi-disk zip files are not supported: found disk number: " + diskNumber)); // 6 - Disk where central directory starts // 8 - Number of central directory records on this disk // 10 - Total number of central directory records var entryCount = eocdrBuffer.readUInt16LE(10); // 12 - Size of central directory (bytes) // 16 - Offset of start of central directory, relative to start of archive var cdOffset = eocdrBuffer.readUInt32LE(16); // 20 - Comment length var commentLength = eocdrBuffer.readUInt16LE(20); var expectedCommentLength = eocdrBuffer.length - eocdrWithoutCommentSize; if (commentLength !== expectedCommentLength) { return callback(new Error("invalid comment length. expected: " + expectedCommentLength + ". found: " + commentLength)); } // 22 - Comment var comment = new Buffer(commentLength); // the comment length is typcially 0. // copy from the original buffer to make sure we're not pinning it from being GC'ed. eocdrBuffer.copy(comment, 0, 22, eocdrBuffer.length); return callback(null, new ZipFile(fd, cdOffset, entryCount, comment)); } callback(new Error("end of central directory record signature not found")); }); }); } function ZipFile(fd, cdOffset, entryCount, comment) { this.fdSlicer = new FdSlicer(fd); this.readEntryCursor = cdOffset; this.entryCount = entryCount; this.comment = comment; this.entriesRead = 0; this.isReadingEntry = false; } ZipFile.prototype.close = function(callback) { if (callback == null) callback = defaultCallback; fs.close(this.fdSlicer.fd, callback); }; ZipFile.prototype.readEntries = function(callback) { var self = this; if (callback == null) callback = defaultCallback; var entries = []; keepReading(); function keepReading() { self.readEntry(function(err, entry) { if (err) return callback(err); entries.push(entry); if (self.entriesRemaining() > 0) { keepReading(); } else { callback(null, entries); } }); } }; ZipFile.prototype.entriesRemaining = function() { return this.entryCount - this.entriesRead; }; ZipFile.prototype.readEntry = function(callback) { var self = this; if (self.isReadingEntry) throw new Error("readEntry already in progress"); self.isReadingEntry = true; if (callback == null) callback = defaultCallback; var buffer = new Buffer(46); readFdSlicerNoEof(this.fdSlicer, buffer, 0, buffer.length, this.readEntryCursor, function(err) { if (err) return callback(err); var entry = {}; // 0 - Central directory file header signature var signature = buffer.readUInt32LE(0); if (signature !== 0x02014b50) return callback(new Error("invalid central directory file header signature: 0x" + signature.toString(16))); // 4 - Version made by entry.versionMadeBy = buffer.readUInt16LE(4); // 6 - Version needed to extract (minimum) entry.versionNeededToExtract = buffer.readUInt16LE(6); // 8 - General purpose bit flag entry.generalPurposeBitFlag = buffer.readUInt16LE(8); // 10 - Compression method entry.compressionMethod = buffer.readUInt16LE(10); // 12 - File last modification time entry.lastModFileTime = buffer.readUInt16LE(12); // 14 - File last modification date entry.lastModFileDate = buffer.readUInt16LE(14); // 16 - CRC-32 entry.crc32 = buffer.readUInt32LE(16); // 20 - Compressed size entry.compressedSize = buffer.readUInt32LE(20); // 24 - Uncompressed size entry.uncompressedSize = buffer.readUInt32LE(24); // 28 - File name length (n) entry.fileNameLength = buffer.readUInt16LE(28); // 30 - Extra field length (m) entry.extraFieldLength = buffer.readUInt16LE(30); // 32 - File comment length (k) entry.fileCommentLength = buffer.readUInt16LE(32); // 34 - Disk number where file starts // 36 - Internal file attributes entry.internalFileAttributes = buffer.readUInt16LE(36); // 38 - External file attributes entry.externalFileAttributes = buffer.readUInt32LE(38); // 42 - Relative offset of local file header entry.relativeOffsetOfLocalHeader = buffer.readUInt32LE(42); self.readEntryCursor += 46; buffer = new Buffer(entry.fileNameLength + entry.extraFieldLength + entry.fileCommentLength); readFdSlicerNoEof(self.fdSlicer, buffer, 0, buffer.length, self.readEntryCursor, function(err) { if (err) return callback(err); // 46 - File name var encoding = entry.generalPurposeBitFlag & 0x800 ? "utf8" : "ascii"; // TODO: replace ascii with CP437 using https://github.com/bnoordhuis/node-iconv entry.fileName = buffer.toString(encoding, 0, entry.fileNameLength); // 46+n - Extra field var fileCommentStart = entry.fileNameLength + entry.extraFieldLength; var extraFieldBuffer = buffer.slice(entry.fileNameLength, fileCommentStart); entry.extraFields = []; var i = 0; while (i < extraFieldBuffer.length) { var headerId = extraFieldBuffer.readUInt16LE(i + 0); var dataSize = extraFieldBuffer.readUInt16LE(i + 2); var dataStart = i + 4; var dataEnd = dataStart + dataSize; var dataBuffer = new Buffer(dataSize); extraFieldBuffer.copy(dataBuffer, 0, dataStart, dataEnd); entry.extraFields.push({ id: headerId, data: dataBuffer, }); i = dataEnd; } // 46+n+m - File comment entry.fileComment = buffer.toString(encoding, fileCommentStart, fileCommentStart + entry.fileCommentLength); self.readEntryCursor += buffer.length; self.entriesRead += 1; self.isReadingEntry = false; callback(null, entry); }); }); }; ZipFile.prototype.openReadStream = function(entry, callback) { var self = this; var buffer = new Buffer(30); readFdSlicerNoEof(self.fdSlicer, buffer, 0, buffer.length, entry.relativeOffsetOfLocalHeader, function(err) { if (err) return callback(err); // 0 - Local file header signature = 0x04034b50 var signature = buffer.readUInt32LE(0); if (signature !== 0x04034b50) callback(new Error("invalid local file header signature: 0x" + signature.toString(16))); // all this should be redundant // 4 - Version needed to extract (minimum) // 6 - General purpose bit flag // 8 - Compression method // 10 - File last modification time // 12 - File last modification date // 14 - CRC-32 // 18 - Compressed size // 22 - Uncompressed size // 26 - File name length (n) var fileNameLength = buffer.readUInt16LE(26); // 28 - Extra field length (m) var extraFieldLength = buffer.readUInt16LE(28); // 30 - File name // 30+n - Extra field var localFileHeaderEnd = entry.relativeOffsetOfLocalHeader + buffer.length + fileNameLength + extraFieldLength; if (entry.compressionMethod === 0) { // 0 - The file is stored (no compression) } else { return callback(new Error("unsupported compression method: " + entry.compressionMethod)); } var fileDataStart = localFileHeaderEnd; var fileDataEnd = fileDataStart + entry.compressedSize; var readStream = self.fdSlicer.createReadStream({start: fileDataStart, end: fileDataEnd}); callback(null, readStream); }); }; function verbose(message) { console.log(message); } function readNoEof(fd, buffer, offset, length, position, callback) { fs.read(fd, buffer, offset, length, position, function(err, bytesRead) { if (err) return callback(err); if (bytesRead < length) return callback(new Error("unexpected EOF")); callback(null, buffer); }); } function readFdSlicerNoEof(fdSlicer, buffer, offset, length, position, callback) { fdSlicer.read(buffer, offset, length, position, function(err, bytesRead) { if (err) return callback(err); if (bytesRead < length) return callback(new Error("unexpected EOF")); callback(null, buffer); }); } function defaultCallback(err) { if (err) throw err; } yauzl-2.10.0/test/success/directories.zip000066400000000000000000000006541331657221200204340ustar00rootroot00000000000000PK Ea/UT SSux PK Ea/a.txtUT SSux PK Eb/UT 3S3Sux PK EAa/UTSux PK E<a/a.txtUTSux PK EA}b/UT3Sux PKyauzl-2.10.0/test/success/directories/000077500000000000000000000000001331657221200177035ustar00rootroot00000000000000yauzl-2.10.0/test/success/directories/a/000077500000000000000000000000001331657221200201235ustar00rootroot00000000000000yauzl-2.10.0/test/success/directories/a/a.txt000066400000000000000000000000001331657221200210720ustar00rootroot00000000000000yauzl-2.10.0/test/success/directories/b/000077500000000000000000000000001331657221200201245ustar00rootroot00000000000000yauzl-2.10.0/test/success/directories/b/.git_please_make_this_directory000066400000000000000000000000001331657221200263370ustar00rootroot00000000000000yauzl-2.10.0/test/success/empty.zip000066400000000000000000000000261331657221200172470ustar00rootroot00000000000000PKyauzl-2.10.0/test/success/empty/000077500000000000000000000000001331657221200165255ustar00rootroot00000000000000yauzl-2.10.0/test/success/empty/.git_please_make_this_directory000066400000000000000000000000001331657221200247400ustar00rootroot00000000000000yauzl-2.10.0/test/success/linux-info-zip.zip000066400000000000000000000004641331657221200210070ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQVa.txtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/success/linux-info-zip/000077500000000000000000000000001331657221200202575ustar00rootroot00000000000000yauzl-2.10.0/test/success/linux-info-zip/a.txt000066400000000000000000000000051331657221200212330ustar00rootroot00000000000000asdf yauzl-2.10.0/test/success/linux-info-zip/b.txt000066400000000000000000000000051331657221200212340ustar00rootroot00000000000000bsdf yauzl-2.10.0/test/success/sloppy-filenames.zip000066400000000000000000000004641331657221200214060ustar00rootroot00000000000000PK KEQVa.txtUT >*S>*Sux asdf PK KEb.txtUT >*S>*Sux bsdf PK KEQVa\txtUT>*Sux PK KEDb.txtUT>*Sux PKyauzl-2.10.0/test/success/sloppy-filenames/000077500000000000000000000000001331657221200206565ustar00rootroot00000000000000yauzl-2.10.0/test/success/sloppy-filenames/a/000077500000000000000000000000001331657221200210765ustar00rootroot00000000000000yauzl-2.10.0/test/success/sloppy-filenames/a/.dont_expect_an_empty_dir_entry_for_this_dir000066400000000000000000000000001331657221200321070ustar00rootroot00000000000000yauzl-2.10.0/test/success/sloppy-filenames/a/txt000066400000000000000000000000051331657221200216330ustar00rootroot00000000000000asdf yauzl-2.10.0/test/success/sloppy-filenames/b.txt000066400000000000000000000000051331657221200216330ustar00rootroot00000000000000bsdf yauzl-2.10.0/test/success/traditional-encryption-and-compression.zip000066400000000000000000000003071331657221200257140ustar00rootroot00000000000000PK J a.binUT iXiXux c.,"s IX}Y`PK PK J a.binUTiXux PKKfyauzl-2.10.0/test/success/traditional-encryption-and-compression/000077500000000000000000000000001331657221200251705ustar00rootroot00000000000000yauzl-2.10.0/test/success/traditional-encryption-and-compression/a.bin000066400000000000000000000000271331657221200261010ustar00rootroot00000000000000c.,"s IX}Y`yauzl-2.10.0/test/success/traditional-encryption.zip000066400000000000000000000003021331657221200226100ustar00rootroot00000000000000PK J 0:6a.txtUT :XEXux (=0X&]b\PK 0:6PK J 0:6a.txtUT:Xux PKKayauzl-2.10.0/test/success/traditional-encryption/000077500000000000000000000000001331657221200220715ustar00rootroot00000000000000yauzl-2.10.0/test/success/traditional-encryption/a.txt000066400000000000000000000000221331657221200230440ustar00rootroot00000000000000(=0X&]b\yauzl-2.10.0/test/success/unicode-path-extra-field.zip000066400000000000000000000003411331657221200226730ustar00rootroot00000000000000PK-%H up七个房间.txtPKPK?-%H6 upupupup七个房间.txtPKtWyauzl-2.10.0/test/success/unicode-path-extra-field/000077500000000000000000000000001331657221200221515ustar00rootroot00000000000000yauzl-2.10.0/test/success/unicode-path-extra-field/qi ge fangjian.txt000066400000000000000000000000001331657221200254230ustar00rootroot00000000000000yauzl-2.10.0/test/success/unicode.zip000066400000000000000000000017641331657221200175510ustar00rootroot00000000000000PK z ETurmion Kätilöt/UT SSux PK E ETurmion Kätilöt/Hoitovirhe/UT SSux PK  E+Turmion Kätilöt/Hoitovirhe/Rautaketju.mp3UT S8FSux PK  ETurmion Kätilöt/Pirun nyrkki/UT 0S1Sux PK  E6Turmion Kätilöt/Pirun nyrkki/Mistä veri pakenee.mp3UT S:FSux PK z EATurmion Kätilöt/UTSux PK E EALTurmion Kätilöt/Hoitovirhe/UTSux PK  E+Turmion Kätilöt/Hoitovirhe/Rautaketju.mp3UTSux PK  EATurmion Kätilöt/Pirun nyrkki/UT0Sux PK  E6aTurmion Kätilöt/Pirun nyrkki/Mistä veri pakenee.mp3UTSux PK yauzl-2.10.0/test/success/unicode/000077500000000000000000000000001331657221200170155ustar00rootroot00000000000000yauzl-2.10.0/test/success/unicode/Turmion Katilot/000077500000000000000000000000001331657221200220425ustar00rootroot00000000000000yauzl-2.10.0/test/success/unicode/Turmion Katilot/Hoitovirhe/000077500000000000000000000000001331657221200241625ustar00rootroot00000000000000yauzl-2.10.0/test/success/unicode/Turmion Katilot/Hoitovirhe/Rautaketju.mp3000066400000000000000000000000001331657221200267100ustar00rootroot00000000000000yauzl-2.10.0/test/success/unicode/Turmion Katilot/Pirun nyrkki/000077500000000000000000000000001331657221200244275ustar00rootroot00000000000000yauzl-2.10.0/test/success/unicode/Turmion Katilot/Pirun nyrkki/Mista veri pakenee.mp3000066400000000000000000000000001331657221200304320ustar00rootroot00000000000000yauzl-2.10.0/test/success/windows-7-zip.zip000077500000000000000000000004241331657221200205540ustar00rootroot00000000000000PK KEQVa.txtasdf PK KEb.txtbsdf PK? KEQV$ a.txt >l>l>lPK? KE$ (b.txt mmmPKPyauzl-2.10.0/test/success/windows-7-zip/000077500000000000000000000000001331657221200200255ustar00rootroot00000000000000yauzl-2.10.0/test/success/windows-7-zip/a.txt000066400000000000000000000000051331657221200210010ustar00rootroot00000000000000asdf yauzl-2.10.0/test/success/windows-7-zip/b.txt000066400000000000000000000000051331657221200210020ustar00rootroot00000000000000bsdf yauzl-2.10.0/test/success/windows-compressed-folder.zip000077500000000000000000000003141331657221200232210ustar00rootroot00000000000000PKKEQVa.txtasdf PKKEb.txtbsdf PKKEQV a.txtPKKE (b.txtPKfPyauzl-2.10.0/test/success/windows-compressed-folder/000077500000000000000000000000001331657221200224745ustar00rootroot00000000000000yauzl-2.10.0/test/success/windows-compressed-folder/a.txt000066400000000000000000000000051331657221200234500ustar00rootroot00000000000000asdf yauzl-2.10.0/test/success/windows-compressed-folder/b.txt000066400000000000000000000000051331657221200234510ustar00rootroot00000000000000bsdf yauzl-2.10.0/test/success/zip64.zip000066400000000000000000000006201331657221200170650ustar00rootroot00000000000000PK-} I test1.txtKPK!esPK-} I test2.txtKJ,PKvPK--} I!es  test1.txtPK--} Iv  Dtest2.txtDPK,--PK.PKyauzl-2.10.0/test/success/zip64/000077500000000000000000000000001331657221200163435ustar00rootroot00000000000000yauzl-2.10.0/test/success/zip64/test1.txt000066400000000000000000000000031331657221200201350ustar00rootroot00000000000000fooyauzl-2.10.0/test/success/zip64/test2.txt000066400000000000000000000000031331657221200201360ustar00rootroot00000000000000baryauzl-2.10.0/test/test.js000066400000000000000000000360061331657221200152410ustar00rootroot00000000000000var yauzl = require("../"); var zip64 = require("./zip64"); var rangeTest = require("./range-test"); var fs = require("fs"); var path = require("path"); var Pend = require("pend"); var util = require("util"); var Readable = require("stream").Readable; var Writable = require("stream").Writable; // this is the date i made the example zip files and their content files, // so this timestamp will be earlier than all the ones stored in these test zip files // (and probably all future zip files). // no timezone awareness, because that's how MS-DOS rolls. var earliestTimestamp = new Date(2014, 7, 18, 0, 0, 0, 0); var pend = new Pend(); // 1 thing at a time for better determinism/reproducibility pend.max = 1; var args = process.argv.slice(2); function shouldDoTest(testPath) { if (args.length === 0) return true; return args.indexOf(testPath) !== -1; } // success tests listZipFiles([path.join(__dirname, "success"), path.join(__dirname, "wrong-entry-sizes")]).forEach(function(zipfilePath) { if (!shouldDoTest(zipfilePath)) return; var optionConfigurations = [ // you can find more options coverage in the failure tests. {lazyEntries: true}, {lazyEntries: true, decodeStrings: false}, ]; if (/\/wrong-entry-sizes\//.test(zipfilePath)) { optionConfigurations.forEach(function(options) { options.validateEntrySizes = false; }); } var openFunctions = [ function(testId, options, callback) { yauzl.open(zipfilePath, options, callback); }, function(testId, options, callback) { yauzl.fromBuffer(fs.readFileSync(zipfilePath), options, callback); }, function(testId, options, callback) { openWithRandomAccess(zipfilePath, options, true, testId, callback); }, function(testId, options, callback) { openWithRandomAccess(zipfilePath, options, false, testId, callback); }, ]; openFunctions.forEach(function(openFunction, i) { optionConfigurations.forEach(function(options, j) { var testId = zipfilePath + "(" + ["fd", "buffer", "randomAccess", "minimalRandomAccess"][i] + "," + j + "): "; var expectedPathPrefix = zipfilePath.replace(/\.zip$/, ""); var expectedArchiveContents = {}; var DIRECTORY = 1; // not a string recursiveRead("."); function recursiveRead(name) { // windows support? whatever. var name = name.replace(/\\/g, "/"); var key = addUnicodeSupport(name); var realPath = path.join(expectedPathPrefix, name); if (fs.statSync(realPath).isFile()) { switch (path.basename(name)) { case ".git_please_make_this_directory": // ignore break; case ".dont_expect_an_empty_dir_entry_for_this_dir": delete expectedArchiveContents[path.dirname(name)]; break; default: // normal file expectedArchiveContents[key] = fs.readFileSync(realPath); break; } } else { if (name !== ".") expectedArchiveContents[key] = DIRECTORY; fs.readdirSync(realPath).forEach(function(child) { recursiveRead(path.join(name, child)); }); } } pend.go(function(zipfileCallback) { openFunction(testId, options, function(err, zipfile) { if (err) throw err; zipfile.readEntry(); zipfile.on("entry", function(entry) { var fileName = entry.fileName; var fileComment = entry.fileComment; if (options.decodeStrings === false) { if (fileName.constructor !== Buffer) throw new Error(testId + "expected fileName to be a Buffer"); fileName = manuallyDecodeFileName(fileName); fileComment = manuallyDecodeFileName(fileComment); } if (fileComment !== "") throw new Error(testId + "expected empty fileComment"); var messagePrefix = testId + fileName + ": "; var timestamp = entry.getLastModDate(); if (timestamp < earliestTimestamp) throw new Error(messagePrefix + "timestamp too early: " + timestamp); if (timestamp > new Date()) throw new Error(messagePrefix + "timestamp in the future: " + timestamp); var fileNameKey = fileName.replace(/\/$/, ""); var expectedContents = expectedArchiveContents[fileNameKey]; if (expectedContents == null) { throw new Error(messagePrefix + "not supposed to exist"); } delete expectedArchiveContents[fileNameKey]; if (fileName !== fileNameKey) { // directory console.log(messagePrefix + "pass"); zipfile.readEntry(); } else { var isEncrypted = entry.isEncrypted(); var isCompressed = entry.isCompressed(); if (/traditional-encryption/.test(zipfilePath) !== isEncrypted) { throw new Error("expected traditional encryption in the traditional encryption test cases"); if (/traditional-encryption-and-compression/.test(zipfilePath) !== isCompressed) { throw new Error("expected traditional encryption and compression in the traditional encryption and compression test case"); } } if (isEncrypted) { zipfile.openReadStream(entry, { decrypt: false, decompress: isCompressed ? false : null, }, onReadStream); } else { zipfile.openReadStream(entry, onReadStream); } function onReadStream(err, readStream) { if (err) throw err; var buffers = []; readStream.on("data", function(data) { buffers.push(data); }); readStream.on("end", function() { var actualContents = Buffer.concat(buffers); // uh. there's no buffer equality check? var equal = actualContents.toString("binary") === expectedContents.toString("binary"); if (!equal) { throw new Error(messagePrefix + "wrong contents"); } console.log(messagePrefix + "pass"); zipfile.readEntry(); }); readStream.on("error", function(err) { throw err; }); } } }); zipfile.on("end", function() { for (var fileName in expectedArchiveContents) { throw new Error(testId + fileName + ": missing file"); } console.log(testId + "pass"); zipfileCallback(); }); zipfile.on("close", function() { console.log(testId + "closed"); }); }); }); }); }); }); // failure tests listZipFiles([path.join(__dirname, "failure")]).forEach(function(zipfilePath) { if (!shouldDoTest(zipfilePath)) return; var expectedErrorMessage = path.basename(zipfilePath).replace(/(_\d+)?\.zip$/, ""); var failedYet = false; var emittedError = false; pend.go(function(cb) { var operationsInProgress = 0; if (/invalid characters in fileName/.test(zipfilePath)) { // this error can only happen when you specify an option yauzl.open(zipfilePath, {strictFileNames: true}, onZipFile); } else { yauzl.open(zipfilePath, onZipFile); } return; function onZipFile(err, zipfile) { if (err) return checkErrorMessage(err); zipfile.on("error", function(err) { noEventsAllowedAfterError(); emittedError = true; checkErrorMessage(err); }); zipfile.on("entry", function(entry) { noEventsAllowedAfterError(); // let's also try to read directories, cuz whatever. operationsInProgress += 1; zipfile.openReadStream(entry, function(err, stream) { if (err) return checkErrorMessage(err); stream.on("error", function(err) { checkErrorMessage(err); }); stream.on("data", function(data) { // ignore }); stream.on("end", function() { doneWithSomething(); }); }); }); operationsInProgress += 1; zipfile.on("end", function() { noEventsAllowedAfterError(); doneWithSomething(); }); function doneWithSomething() { operationsInProgress -= 1; if (operationsInProgress !== 0) return; if (!failedYet) { throw new Error(zipfilePath + ": expected failure"); } } } function checkErrorMessage(err) { var actualMessage = err.message.replace(/[^0-9A-Za-z-]+/g, " "); if (actualMessage !== expectedErrorMessage) { throw new Error(zipfilePath + ": wrong error message: " + actualMessage); } console.log(zipfilePath + ": pass"); failedYet = true; operationsInProgress = -Infinity; cb(); } function noEventsAllowedAfterError() { if (emittedError) throw new Error("events emitted after error event"); } }); }); // fromRandomAccessReader with errors pend.go(function(cb) { util.inherits(TestRandomAccessReader, yauzl.RandomAccessReader); function TestRandomAccessReader() { yauzl.RandomAccessReader.call(this); } TestRandomAccessReader.prototype._readStreamForRange = function(start, end) { var brokenator = new Readable(); brokenator._read = function(size) { brokenator.emit("error", new Error("all reads fail")); }; return brokenator; }; var reader = new TestRandomAccessReader(); yauzl.fromRandomAccessReader(reader, 0x1000, function(err, zipfile) { if (err.message === "all reads fail") { console.log("fromRandomAccessReader with errors: pass"); cb(); } else { throw err; } }); }); // read some entries, then close. pend.go(function(cb) { var prefix = "read some entries then close: "; // this zip file should have at least 3 entries in it yauzl.open(path.join(__dirname, "success/unicode.zip"), {lazyEntries: true}, function(err, zipfile) { if (err) throw err; var entryCount = 0; zipfile.readEntry(); zipfile.on("entry", function(entry) { entryCount += 1; console.log(prefix + "entryCount: " + entryCount); if (entryCount < 3) { zipfile.readEntry(); } else if (entryCount === 3) { zipfile.close(); console.log(prefix + "close()"); } else { throw new Error(prefix + "read too many entries"); } }); zipfile.on("close", function() { console.log(prefix + "closed"); if (entryCount === 3) { console.log(prefix + "pass"); cb(); } else { throw new Error(prefix + "not enough entries read before closed"); } }); zipfile.on("end", function() { throw new Error(prefix + "we weren't supposed to get to the end"); }); zipfile.on("error", function(err) { throw err; }); }); }); // abort open read stream pend.go(function(cb) { var prefix = "abort open read stream: "; yauzl.open(path.join(__dirname, "big-compression.zip"), {lazyEntries: true}, function(err, zipfile) { if (err) throw err; var doneWithStream = false; zipfile.readEntry(); zipfile.on("entry", function(entry) { zipfile.openReadStream(entry, function(err, readStream) { var writer = new Writable(); var bytesSeen = 0; writer._write = function(chunk, encoding, callback) { bytesSeen += chunk.length; if (bytesSeen < entry.uncompressedSize / 10) { // keep piping a bit longer callback(); } else { // alright, i've seen enough. doneWithStream = true; console.log(prefix + "destroy()"); readStream.unpipe(writer); readStream.destroy(); // now keep trying to use the fd zipfile.readEntry(); } }; readStream.pipe(writer); }); }); zipfile.on("end", function() { console.log(prefix + "end"); }); zipfile.on("close", function() { console.log(prefix + "closed"); if (doneWithStream) { console.log(prefix + "pass"); cb(); } else { throw new Error(prefix + "closed prematurely"); } }); zipfile.on("error", function(err) { throw err; }); }); }); // zip64 pend.go(zip64.runTest); // openReadStream with range pend.go(rangeTest.runTest); pend.wait(function() { // if you don't see this, something never happened. console.log("done"); }); function listZipFiles(dirList) { var zipfilePaths = []; dirList.forEach(function(dir) { fs.readdirSync(dir).filter(function(filepath) { return /\.zip$/.exec(filepath); }).forEach(function(name) { zipfilePaths.push(path.relative(".", path.join(dir, name))); }); }); zipfilePaths.sort(); return zipfilePaths; } function addUnicodeSupport(name) { // reading and writing unicode filenames on mac is broken. // we keep all our test data ascii, and then swap in the real names here. // see https://github.com/thejoshwolfe/yauzl/issues/10 name = name.replace(/Turmion Katilot/g, "Turmion Kätilöt"); name = name.replace(/Mista veri pakenee/g, "Mistä veri pakenee"); name = name.replace(/qi ge fangjian/g, "七个房间"); return name; } function manuallyDecodeFileName(fileName) { // file names in this test suite are always utf8 compatible. fileName = fileName.toString("utf8"); fileName = fileName.replace("\\", "/"); if (fileName === "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f") { // we're not doing the unicode path extra field decoding outside of yauzl. // just hardcode this answer. fileName = "七个房间.txt"; } return fileName; } function openWithRandomAccess(zipfilePath, options, implementRead, testId, callback) { util.inherits(InefficientRandomAccessReader, yauzl.RandomAccessReader); function InefficientRandomAccessReader() { yauzl.RandomAccessReader.call(this); } InefficientRandomAccessReader.prototype._readStreamForRange = function(start, end) { return fs.createReadStream(zipfilePath, {start: start, end: end - 1}); }; if (implementRead) { InefficientRandomAccessReader.prototype.read = function(buffer, offset, length, position, callback) { fs.open(zipfilePath, "r", function(err, fd) { if (err) throw err; fs.read(fd, buffer, offset, length, position, function(err, bytesRead) { if (bytesRead < length) throw new Error("unexpected EOF"); fs.close(fd, function(err) { if (err) throw err; callback(); }); }); }); }; } InefficientRandomAccessReader.prototype.close = function(cb) { console.log(testId + "close hook"); yauzl.RandomAccessReader.prototype.close.call(this, cb); }; fs.stat(zipfilePath, function(err, stats) { if (err) throw err; var reader = new InefficientRandomAccessReader(); yauzl.fromRandomAccessReader(reader, stats.size, options, function(err, zipfile) { if (err) throw err; callback(null, zipfile); }); }); } yauzl-2.10.0/test/wrong-entry-sizes/000077500000000000000000000000001331657221200173455ustar00rootroot00000000000000yauzl-2.10.0/test/wrong-entry-sizes/wrong-entry-sizes.zip000066400000000000000000000005001331657221200235120ustar00rootroot00000000000000PKrJa.txt%pPK'PKrJb.txtPK~.ELPKrJc/PK?rJ0a.txtPK?rJ~.E:b.txtPK?rJAtc/PKyauzl-2.10.0/test/wrong-entry-sizes/wrong-entry-sizes/000077500000000000000000000000001331657221200227735ustar00rootroot00000000000000yauzl-2.10.0/test/wrong-entry-sizes/wrong-entry-sizes/a.txt000066400000000000000000000000471331657221200237550ustar00rootroot00000000000000====================================== yauzl-2.10.0/test/wrong-entry-sizes/wrong-entry-sizes/b.txt000066400000000000000000000001141331657221200237510ustar00rootroot00000000000000=========================================================================== yauzl-2.10.0/test/wrong-entry-sizes/wrong-entry-sizes/c/000077500000000000000000000000001331657221200232155ustar00rootroot00000000000000yauzl-2.10.0/test/wrong-entry-sizes/wrong-entry-sizes/c/.git_please_make_this_directory000066400000000000000000000000001331657221200314300ustar00rootroot00000000000000yauzl-2.10.0/test/zip64.js000066400000000000000000000230141331657221200152310ustar00rootroot00000000000000var fs = require("fs"); var path = require("path"); var yauzl = require("../"); var util = require("util"); var Readable = require("stream").Readable; var Writable = require("stream").Writable; var BufferList = require("bl"); exports.runTest = runTest; function usage() { process.stdout.write("" + "zip64.js usage:\n" + " (no arguments) run the test\n" + " -d dir/ dump the contents of the expected zip file into the specified directory\n" + " -z in.zip out.zip compress in.zip into out.zip\n" + "\n" + "To recreate the file `zip64.zip_fragment`,\n" + "do the following (from the root directory of this project):\n" + "\n" + "```\n" + "mkdir tmp\n" + "node test/zip64.js -d tmp/\n" + "(cd tmp/ && zip -0 zip64.zip a.txt large.bin b.txt)\n" + "node test/zip64.js -z tmp/zip64.zip test/zip64/zip64.zip_fragment\n" + "rm -rf tmp/\n" + "```\n" + ""); process.exit(1); } function cli() { var arg1 = process.argv[2]; var arg2 = process.argv[3]; var arg3 = process.argv[4]; if (arg1 == null) { runTest(); return; } if (/^--?h/.test(arg1)) usage(); if (arg1 === "-d") { if (arg2 == null) usage(); dumpExpectedContents(arg2); return; } if (arg1 === "-z") { if (arg2 == null) usage(); if (arg3 == null) usage(); compressFile(arg2, arg3); return; } usage(); } function dumpExpectedContents(outputDir) { var readStream = newLargeBinContentsProducer(); readStream.pipe(fs.createWriteStream(path.join(outputDir, "large.bin"))); readStream.on("progress", function(numerator, denominator) { process.stderr.write("\r" + numerator + "/" + denominator + " " + ((numerator / denominator * 100) | 0) + "%"); if (numerator === denominator) process.stderr.write("\n"); }); fs.writeFileSync(path.join(outputDir, "a.txt"), "hello a\n"); fs.writeFileSync(path.join(outputDir, "b.txt"), "hello b\n"); } var largeBinLength = 8000000000; function newLargeBinContentsProducer() { // emits the fibonacci sequence: // 0, 1, 1, 2, 3, 5, 8, 13, ... // with each entry encoded in a UInt32BE. // arithmetic overflow will happen eventually, resulting in wrap around. // as a consequence of limited precision, this sequence repeats itself after 6442450944 entires. // however, we only require 2000000000 entires, so it's good enough. var readStream = new Readable(); var prev0 = -1; var prev1 = 1; var byteCount = 0; readStream._read = function(size) { while (true) { if (byteCount >= largeBinLength) { readStream.push(null); return; } var bufferSize = Math.min(0x10000, largeBinLength - byteCount); var buffer = new Buffer(bufferSize); for (var i = 0; i < bufferSize; i += 4) { var n = ((prev0 + prev1) & 0xffffffff) >>> 0; prev0 = prev1; prev1 = n; byteCount += 4; buffer.writeUInt32BE(n, i, true); } readStream.emit("progress", byteCount, largeBinLength); if (!readStream.push(buffer)) return; } }; readStream.destroy = function() {}; return readStream; } // this is just some bytes so we can identify it. var prefixLength = 0x100; function getPrefixOfStream(stream, cb) { var prefixBuffer = new Buffer(prefixLength); var writer = new Writable(); writer._write = function(chunk, encoding, callback) { chunk.copy(prefixBuffer, 0, 0, prefixLength); stream.unpipe(writer); cb(prefixBuffer); }; stream.pipe(writer); } function getPrefixOfLargeBinContents(cb) { getPrefixOfStream(newLargeBinContentsProducer(), cb); } function compressFile(inputPath, outputPath) { getPrefixOfLargeBinContents(function(prefixBuffer) { findPrefixInPath(prefixBuffer, function(largeBinContentsOffset) { writeCompressedFile(largeBinContentsOffset); }); }); function findPrefixInPath(prefixBuffer, cb) { var previewLength = 0x1000; fs.createReadStream(inputPath, { start: 0, end: previewLength + prefixLength - 1, }).pipe(BufferList(function(err, data) { if (err) throw err; for (var i = 0; i < previewLength; i++) { if (buffersEqual(data.slice(i, prefixLength), prefixBuffer)) { return cb(i); } } throw new Error("can't find large.bin contents"); })); } function writeCompressedFile(largeBinContentsOffset) { var writeStream = fs.createWriteStream(outputPath); var headerBuffer = new Buffer(4); headerBuffer.writeUInt32BE(largeBinContentsOffset, 0); writeStream.write(headerBuffer); var firstReader = fs.createReadStream(inputPath, { start: 0, end: largeBinContentsOffset - 1, }); firstReader.pipe(writeStream, {end: false}); firstReader.on("end", function() { var secondReader = fs.createReadStream(inputPath, { start: largeBinContentsOffset + largeBinLength, }); secondReader.pipe(writeStream); }); } } var logPrefix = "test/zip64: "; function runTest(cb) { if (cb == null) cb = function() {}; makeRandomAccessReader(function(reader, size) { yauzl.fromRandomAccessReader(reader, size, function(err, zipfile) { if (err) throw err; var entryIndex = 0; zipfile.on("entry", function(entry) { var expectedContents; if (entryIndex === 0) { if (entry.fileName !== "a.txt") throw new Error(logPrefix + "expected 'a.txt'. got '" + entry.fileName + "'."); expectedContents = "hello a\n"; } else if (entryIndex === 1) { if (entry.fileName !== "large.bin") throw new Error(logPrefix + "expected 'large.bin'. got '" + entry.fileName + "'."); expectedContents = null; // special case } else if (entryIndex === 2) { if (entry.fileName !== "b.txt") throw new Error(logPrefix + "expected 'b.txt'. got '" + entry.fileName + "'."); expectedContents = "hello b\n"; } else { throw new Error(logPrefix + "too many entries"); } entryIndex += 1; zipfile.openReadStream(entry, function(err, readStream) { if (err) throw err; if (expectedContents != null) { readStream.pipe(BufferList(function(err, data) { if (data.toString() !== expectedContents) throw new Error(logPrefix + "expected contents:\n" + expectedContents + "\ngot:\n" + data.toString() + "\n"); console.log(logPrefix + entry.fileName + ": pass"); })); } else { // make sure this is the big thing getPrefixOfLargeBinContents(function(expectedPrefixBuffer) { getPrefixOfStream(readStream, function(actualPrefixBuffer) { readStream.destroy(); if (buffersEqual(expectedPrefixBuffer, actualPrefixBuffer)) { console.log(logPrefix + entry.fileName + ": pass"); } else { throw new Error(logPrefix + "large.bin contents read did not return expected stream"); } }); }); } }); }); zipfile.on("close", function() { console.log(logPrefix + "closed"); if (entryIndex === 3) { console.log(logPrefix + "pass"); cb(); } else { throw new Error(logPrefix + "closed prematurely"); } }); }); }); } function makeRandomAccessReader(cb) { var fileName = "zip64/zip64.zip_fragment"; fs.readFile(path.join(__dirname, fileName), function(err, backendContents) { if (err) return callback(err); if (backendContents.length <= 4) throw new Error("unexpected EOF"); var largeBinContentsOffset = backendContents.readUInt32BE(0) - 4; if (largeBinContentsOffset > backendContents.length) throw new Error(".zip_fragment header is malformed"); var largeBinContentsEnd = largeBinContentsOffset + largeBinLength; var firstRead = true; var pretendSize = backendContents.length + largeBinLength - 4; util.inherits(InflatingRandomAccessReader, yauzl.RandomAccessReader); function InflatingRandomAccessReader() { yauzl.RandomAccessReader.call(this); } InflatingRandomAccessReader.prototype._readStreamForRange = function(start, end) { var thisIsTheFirstRead = firstRead; firstRead = false; var result = new BufferList(); if (end <= largeBinContentsOffset) { result.append(backendContents.slice(start + 4, end + 4)); } else if (start >= largeBinContentsOffset + largeBinLength) { result.append(backendContents.slice(start - largeBinLength + 4, end - largeBinLength + 4)); } else if (start === largeBinContentsOffset && end === largeBinContentsEnd) { return newLargeBinContentsProducer(); } else if (thisIsTheFirstRead && start > largeBinContentsOffset && end === pretendSize) { // yauzl's first move is to cast a large net to try to find the EOCDR. // yauzl's only going to care about the end of this data, so fill in the gaps with dummy data. var dummyTrash = new Buffer(largeBinContentsEnd - start); result.append(dummyTrash); result.append(backendContents.slice(largeBinContentsOffset + 4)); } else { throw new Error("_readStreamForRange("+start+", "+end+") misaligned to range ["+largeBinContentsOffset+", "+largeBinContentsEnd+"]"); } result.destroy = function() {}; return result; }; var reader = new InflatingRandomAccessReader(); cb(reader, pretendSize); }); } function buffersEqual(buf1, buf2) { for (var i = 0; i < buf1.length; i++) { if (buf1[i] !== buf2[i]) return false; } return true; } if (require.main === module) cli(); yauzl-2.10.0/test/zip64/000077500000000000000000000000001331657221200146735ustar00rootroot00000000000000yauzl-2.10.0/test/zip64/zip64.zip_fragment000066400000000000000000000011201331657221200202500ustar00rootroot00000000000000PK WuG@m[a.txtUT qVqVux hello a PK-uG9's 0large.binUT 5qVqVux PPPK WuG@pb.txtUT qVqVux hello b PK WuG@m[a.txtUTqVux PK-uG9's ,Glarge.binUT5qVux PPPK WuG@p$b.txtUTqVux PPK,-PPKQPK