[Pkg-javascript-commits] [node-yauzl] 01/04: Imported Upstream version 2.0.0
Andrew Kelley
andrewrk-guest at moszumanska.debian.org
Mon Sep 29 18:43:29 UTC 2014
This is an automated email from the git hooks/post-receive script.
andrewrk-guest pushed a commit to branch master
in repository node-yauzl.
commit 571bb1399f073ff971da3f2c9f38ee0506c86e72
Author: Andrew Kelley <superjoe30 at gmail.com>
Date: Mon Sep 29 17:59:54 2014 +0000
Imported Upstream version 2.0.0
---
.gitignore | 1 +
.npmignore | 1 +
LICENSE | 21 ++
README.md | 268 ++++++++++++++++
index.js | 355 +++++++++++++++++++++
package.json | 27 ++
test/dump.js | 31 ++
test/failure/absolute path C xt.zip | Bin 0 -> 308 bytes
test/failure/absolute path atxt.zip | Bin 0 -> 308 bytes
...entral directory record signature not found.zip | Bin 0 -> 308 bytes
...ata overflows file bounds 63 2147483647 308.zip | Bin 0 -> 308 bytes
...l directory file header signature 0x1014b50.zip | Bin 0 -> 308 bytes
.../invalid characters in fileName a txt.zip | Bin 0 -> 308 bytes
test/failure/invalid code lengths set.zip | Bin 0 -> 2993 bytes
.../invalid comment length expected 1 found 0.zip | Bin 0 -> 309 bytes
...valid local file header signature 0x3034b50.zip | Bin 0 -> 308 bytes
test/failure/invalid relative path xt.zip | Bin 0 -> 308 bytes
...files are not supported found disk number 1.zip | Bin 0 -> 308 bytes
.../too many length or distance symbols.zip | Bin 0 -> 2993 bytes
test/failure/unsupported compression method 1.zip | Bin 0 -> 308 bytes
test/success/cygwin-info-zip.zip | Bin 0 -> 308 bytes
test/success/cygwin-info-zip/a.txt | 1 +
test/success/cygwin-info-zip/b.txt | 1 +
test/success/deflate.zip | Bin 0 -> 2993 bytes
test/success/deflate/index.js | 262 +++++++++++++++
test/success/directories.zip | Bin 0 -> 428 bytes
test/success/directories/a/a.txt | 0
.../directories/b/.git_please_make_this_directory | 0
test/success/empty.zip | Bin 0 -> 22 bytes
test/success/empty/.git_please_make_this_directory | 0
test/success/linux-info-zip.zip | Bin 0 -> 308 bytes
test/success/linux-info-zip/a.txt | 1 +
test/success/linux-info-zip/b.txt | 1 +
test/success/unicode.zip | Bin 0 -> 1012 bytes
.../Hoitovirhe/Rautaketju.mp3" | 0
.../Pirun nyrkki/Mist\303\244 veri pakenee.mp3" | 0
test/success/windows-7-zip.zip | Bin 0 -> 276 bytes
test/success/windows-7-zip/a.txt | 1 +
test/success/windows-7-zip/b.txt | 1 +
test/success/windows-compressed-folder.zip | Bin 0 -> 204 bytes
test/success/windows-compressed-folder/a.txt | 1 +
test/success/windows-compressed-folder/b.txt | 1 +
test/test.js | 168 ++++++++++
43 files changed, 1142 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c2658d7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules/
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..65e3ba2
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1 @@
+test/
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..37538d4
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The 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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..2566f8e
--- /dev/null
+++ b/README.md
@@ -0,0 +1,268 @@
+# yauzl
+
+yet another unzip library for node.
+
+Design principles:
+
+ * Follow the spec.
+ Don't scan for local file headers.
+ Read the central directory for file metadata.
+ * 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 filenames entries.
+ A zip file entry throws an error if its file name starts with `"/"` or `/[A-Za-z]:\//`
+ or if it contains `".."` path segments or `"\\"` (per the spec).
+
+## Usage
+
+```js
+var yauzl = require("yauzl");
+var fs = require("fs");
+
+yauzl.open("path/to/file.zip", function(err, zipfile) {
+ if (err) throw err;
+ zipfile.on("entry", function(entry) {
+ if (/\/$/.test(entry.fileName)) {
+ // directory file names end with '/'
+ return;
+ }
+ zipfile.openReadStream(entry, function(err, readStream) {
+ if (err) throw err;
+ // ensure parent directory exists, and then:
+ readStream.pipe(fs.createWriteStream(entry.fileName));
+ });
+ });
+});
+```
+
+## API
+
+The default for every `callback` parameter is:
+
+```js
+function defaultCallback(err) {
+ if (err) throw err;
+}
+```
+
+### open(path, [options], [callback])
+
+Calls `fs.open(path, "r")` and gives the `fd`, `options`, and `callback` to `fromFd` below.
+
+`options` may be omitted or `null` and defaults to `{autoClose: true}`.
+
+### 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.
+
+The `callback` is given the arguments `(err, zipfile)`.
+An `err` is provided if the End of Central Directory Record Signature cannot be found in the file,
+which indicates that the fd is not a zip file.
+`zipfile` is an instance of `ZipFile`.
+
+`options` may be omitted or `null` and defaults to `{autoClose: false}`.
+`autoClose` is effectively equivalent to:
+
+```js
+zipfile.once("end", function() {
+ zipfile.close();
+});
+```
+
+### fromBuffer(buffer, [callback])
+
+Like `fromFd`, but reads from a RAM buffer instead of an open file.
+`buffer` is a `Buffer`.
+`callback` is effectively passed directly to `fromFd`.
+
+If a `ZipFile` is acquired from this method,
+it will never emit the `close` event,
+and calling `close()` is not necessary.
+
+### 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.
+
+### Class: ZipFile
+
+The constructor for the class is not part of the public API.
+Use `open`, `fromFd`, or `fromBuffer` instead.
+
+#### Event: "entry"
+
+Callback gets `(entry)`, which is an `Entry`.
+
+#### Event: "end"
+
+Emitted after the last `entry` event has been 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 streams created from `openReadStream` have emitted their `end` events.
+
+This event is never emitted if this `ZipFile` was acquired from `fromBuffer()`.
+
+#### openReadStream(entry, [callback])
+
+`entry` must be an `Entry` object from this `ZipFile`.
+`callback` gets `(err, readStream)`, where `readStream` is a `Readable Stream`.
+If the entry is compressed (with a supported compression method),
+the read stream provides the decompressed data.
+If this zipfile is already closed (see `close`), the `callback` will receive an `err`.
+
+#### close([callback])
+
+Causes all future calls to `openReadStream` to fail,
+and calls `fs.close(fd, callback)` after all streams created by `openReadStream` have emitted their `end` events.
+If this object's `end` event has not been emitted yet, this function causes undefined behavior.
+
+If `autoClose` is `true` in the original `open` or `fromFd` call,
+this function will be called automatically effectively in response to this object's `end` event.
+
+#### 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.
+
+### 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`.
+
+If `fileName` would contain unsafe characters, such as an absolute path or
+a relative directory, yauzl emits an error instead of an entry.
+
+#### extraFields
+
+`Array` with each entry in the form `{id: id, data: data}`,
+where `id` is a `Number` and `data` is a `Buffer`.
+None of the extra fields are considered significant by this library.
+
+#### comment
+
+`String` decoded with the same charset as used for `fileName`.
+
+#### getLastModDate()
+
+Effectively implemented as:
+
+```js
+return dosDateTimeToDate(this.lastModFileDate, this.lastModFileTime);
+```
+
+## 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`, or `fromBuffer`.
+ * Attach a listener for the `error` event on any stream you get from `openReadStream`.
+
+## Limitations
+
+### 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`, or `fromBuffer` `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
+
+### No Encryption Support
+
+Currently, the presence of encryption is not even checked,
+and encrypted zip files will cause undefined behavior.
+
+### 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.
+There may be conflicts between the Central Directory information and the Local File Header,
+but the Local File Header is always ignored.
+
+### 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`.
+
+### No ZIP64 Support
+
+A ZIP64 file will probably cause undefined behavior.
+
+### 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.
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..3648c8f
--- /dev/null
+++ b/index.js
@@ -0,0 +1,355 @@
+var fs = require("fs");
+var zlib = require("zlib");
+var FdSlicer = require("fd-slicer");
+var util = require("util");
+var EventEmitter = require("events").EventEmitter;
+var PassThrough = require("stream").PassThrough;
+var Iconv = require("iconv").Iconv;
+
+exports.open = open;
+exports.fromFd = fromFd;
+exports.fromBuffer = fromBuffer;
+exports.ZipFile = ZipFile;
+exports.Entry = Entry;
+exports.dosDateTimeToDate = dosDateTimeToDate;
+
+// cd - Central Directory
+// cdr - Central Directory Record
+// eocdr - End of Central Directory Record
+
+function open(path, options, callback) {
+ if (typeof options === "function") {
+ callback = options;
+ options = null;
+ }
+ if (options == null) options = {autoClose: true};
+ 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 = {autoClose: false};
+ if (callback == null) callback = defaultCallback;
+ fs.fstat(fd, function(err, stats) {
+ if (err) return callback(err);
+ var fdSlicer = new FdSlicer(fd, {autoClose: true});
+ // this ref is unreffed in zipfile.close()
+ fdSlicer.ref();
+ fromFdSlicer(fdSlicer, stats.size, options, callback);
+ });
+}
+
+function fromBuffer(buffer, callback) {
+ if (callback == null) callback = defaultCallback;
+ // i got your open file right here.
+ var fdSlicer = new FakeFdSlicer(buffer);
+ fromFdSlicer(fdSlicer, buffer.length, {}, callback);
+}
+function fromFdSlicer(fdSlicer, totalSize, options, callback) {
+ // 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 = 0x10000; // 2-byte size
+ var bufferSize = Math.min(eocdrWithoutCommentSize + maxCommentSize, totalSize);
+ var buffer = new Buffer(bufferSize);
+ var bufferReadStart = totalSize - buffer.length;
+ readFdSlicerNoEof(fdSlicer, 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 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
+ // the encoding is always cp437.
+ var comment = bufferToString(eocdrBuffer, 22, eocdrBuffer.length, false);
+ return callback(null, new ZipFile(fdSlicer, cdOffset, totalSize, entryCount, comment, options.autoClose));
+ }
+ callback(new Error("end of central directory record signature not found"));
+ });
+}
+
+util.inherits(ZipFile, EventEmitter);
+function ZipFile(fdSlicer, cdOffset, fileSize, entryCount, comment, autoClose) {
+ var self = this;
+ EventEmitter.call(self);
+ self.fdSlicer = fdSlicer;
+ // forward close events
+ self.fdSlicer.on("error", function(err) {
+ // error closing the fd
+ self.emit("error", err);
+ });
+ self.fdSlicer.once("close", function() {
+ self.emit("close");
+ });
+ self.readEntryCursor = cdOffset;
+ self.fileSize = fileSize;
+ self.entryCount = entryCount;
+ self.comment = comment;
+ self.entriesRead = 0;
+ self.autoClose = !!autoClose;
+ self.isOpen = true;
+ // make sure events don't fire outta here until the client has a chance to attach listeners
+ setImmediate(function() { readEntries(self); });
+}
+ZipFile.prototype.close = function() {
+ if (!this.isOpen) return;
+ this.isOpen = false;
+ this.fdSlicer.unref();
+};
+
+function emitErrorAndAutoClose(self, err) {
+ if (self.autoClose) self.close();
+ self.emit("error", err);
+}
+
+function readEntries(self) {
+ if (self.entryCount === self.entriesRead) {
+ // done with metadata
+ if (self.autoClose) self.close();
+ return self.emit("end");
+ }
+ var buffer = new Buffer(46);
+ readFdSlicerNoEof(self.fdSlicer, buffer, 0, buffer.length, self.readEntryCursor, function(err) {
+ if (err) return emitErrorAndAutoClose(self, err);
+ 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);
+
+ 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 emitErrorAndAutoClose(self, err);
+ // 46 - File name
+ var isUtf8 = entry.generalPurposeBitFlag & 0x800
+ try {
+ entry.fileName = bufferToString(buffer, 0, entry.fileNameLength, isUtf8);
+ } catch (e) {
+ return emitErrorAndAutoClose(self, e);
+ }
+
+ // 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
+ try {
+ entry.fileComment = bufferToString(buffer, fileCommentStart, fileCommentStart + entry.fileCommentLength, isUtf8);
+ } catch (e) {
+ return emitErrorAndAutoClose(self, e);
+ }
+
+ self.readEntryCursor += buffer.length;
+ self.entriesRead += 1;
+
+ // validate file name
+ if (entry.fileName.indexOf("\\") !== -1) return emitErrorAndAutoClose(self, new Error("invalid characters in fileName: " + entry.fileName));
+ if (/^[a-zA-Z]:/.test(entry.fileName) || /^\//.test(entry.fileName)) return emitErrorAndAutoClose(self, new Error("absolute path: " + entry.fileName));
+ if (entry.fileName.split("/").indexOf("..") !== -1) return emitErrorAndAutoClose(self, new Error("invalid relative path: " + entry.fileName));
+ self.emit("entry", entry);
+ readEntries(self);
+ });
+ });
+}
+
+ZipFile.prototype.openReadStream = function(entry, callback) {
+ var self = this;
+ if (!self.isOpen) return self.emit("error", new Error("closed"));
+ // make sure we don't lose the fd before we open the actual read stream
+ self.fdSlicer.ref();
+ var buffer = new Buffer(30);
+ readFdSlicerNoEof(self.fdSlicer, 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 filterStream = null;
+ if (entry.compressionMethod === 0) {
+ // 0 - The file is stored (no compression)
+ } else if (entry.compressionMethod === 8) {
+ // 8 - The file is Deflated
+ filterStream = zlib.createInflateRaw();
+ } 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 stream = self.fdSlicer.createReadStream({start: fileDataStart, end: fileDataEnd});
+ if (filterStream != null) {
+ stream = stream.pipe(filterStream);
+ }
+ callback(null, stream);
+ } finally {
+ self.fdSlicer.unref();
+ }
+ });
+};
+
+function Entry() {
+}
+Entry.prototype.getLastModDate = function() {
+ return dosDateTimeToDate(this.lastModFileDate, this.lastModFileTime);
+};
+
+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 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();
+ });
+}
+var cp437_to_utf8 = new Iconv("cp437", "utf8");
+function bufferToString(buffer, start, end, isUtf8) {
+ if (isUtf8) {
+ return buffer.toString("utf8", start, end);
+ } else {
+ return cp437_to_utf8.convert(buffer.slice(start, end)).toString("utf8");
+ }
+}
+
+function FakeFdSlicer(buffer) {
+ // pretend that a buffer is an FdSlicer
+ this.buffer = buffer;
+}
+FakeFdSlicer.prototype.read = function(buffer, offset, length, position, callback) {
+ this.buffer.copy(buffer, offset, position, position + length);
+ setImmediate(function() {
+ callback(null, length);
+ });
+};
+FakeFdSlicer.prototype.createReadStream = function(options) {
+ var start = options.start;
+ var end = options.end;
+ var buffer = new Buffer(end - start);
+ this.buffer.copy(buffer, 0, start, end);
+ var stream = new PassThrough();
+ stream.write(buffer);
+ stream.end();
+ return stream;
+};
+// i promise these functions are working properly :|
+FakeFdSlicer.prototype.ref = function() {};
+FakeFdSlicer.prototype.unref = function() {};
+FakeFdSlicer.prototype.on = function() {};
+FakeFdSlicer.prototype.once = function() {};
+
+function defaultCallback(err) {
+ if (err) throw err;
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..39ac5ea
--- /dev/null
+++ b/package.json
@@ -0,0 +1,27 @@
+{
+ "name": "yauzl",
+ "version": "2.0.0",
+ "description": "yet another unzip library for node",
+ "main": "index.js",
+ "scripts": {
+ "test": "node test/test.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/thejoshwolfe/yauzl.git"
+ },
+ "keywords": [
+ "unzip"
+ ],
+ "author": "Josh Wolfe <thejoshwolfe at gmail.com>",
+ "license": "MIT",
+ "bugs": {
+ "url": "https://github.com/thejoshwolfe/yauzl/issues"
+ },
+ "homepage": "https://github.com/thejoshwolfe/yauzl",
+ "dependencies": {
+ "fd-slicer": "~0.2.1",
+ "iconv": "~2.1.4",
+ "pend": "~1.1.3"
+ }
+}
diff --git a/test/dump.js b/test/dump.js
new file mode 100644
index 0000000..b1437bc
--- /dev/null
+++ b/test/dump.js
@@ -0,0 +1,31 @@
+
+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);
+ });
+ });
+ });
+});
+
diff --git a/test/failure/absolute path C xt.zip b/test/failure/absolute path C xt.zip
new file mode 100644
index 0000000..f1c29db
Binary files /dev/null and b/test/failure/absolute path C xt.zip differ
diff --git a/test/failure/absolute path atxt.zip b/test/failure/absolute path atxt.zip
new file mode 100644
index 0000000..049ca25
Binary files /dev/null and b/test/failure/absolute path atxt.zip differ
diff --git a/test/failure/end of central directory record signature not found.zip b/test/failure/end of central directory record signature not found.zip
new file mode 100644
index 0000000..4ec8222
Binary files /dev/null and b/test/failure/end of central directory record signature not found.zip differ
diff --git a/test/failure/file data overflows file bounds 63 2147483647 308.zip b/test/failure/file data overflows file bounds 63 2147483647 308.zip
new file mode 100644
index 0000000..c64c3fa
Binary files /dev/null and b/test/failure/file data overflows file bounds 63 2147483647 308.zip differ
diff --git a/test/failure/invalid central directory file header signature 0x1014b50.zip b/test/failure/invalid central directory file header signature 0x1014b50.zip
new file mode 100644
index 0000000..dcd0347
Binary files /dev/null and b/test/failure/invalid central directory file header signature 0x1014b50.zip differ
diff --git a/test/failure/invalid characters in fileName a txt.zip b/test/failure/invalid characters in fileName a txt.zip
new file mode 100644
index 0000000..a90d750
Binary files /dev/null and b/test/failure/invalid characters in fileName a txt.zip differ
diff --git a/test/failure/invalid code lengths set.zip b/test/failure/invalid code lengths set.zip
new file mode 100644
index 0000000..834a74d
Binary files /dev/null and b/test/failure/invalid code lengths set.zip differ
diff --git a/test/failure/invalid comment length expected 1 found 0.zip b/test/failure/invalid comment length expected 1 found 0.zip
new file mode 100644
index 0000000..64cca97
Binary files /dev/null and b/test/failure/invalid comment length expected 1 found 0.zip differ
diff --git a/test/failure/invalid local file header signature 0x3034b50.zip b/test/failure/invalid local file header signature 0x3034b50.zip
new file mode 100644
index 0000000..8393a9d
Binary files /dev/null and b/test/failure/invalid local file header signature 0x3034b50.zip differ
diff --git a/test/failure/invalid relative path xt.zip b/test/failure/invalid relative path xt.zip
new file mode 100644
index 0000000..52a55ab
Binary files /dev/null and b/test/failure/invalid relative path xt.zip differ
diff --git a/test/failure/multi-disk zip files are not supported found disk number 1.zip b/test/failure/multi-disk zip files are not supported found disk number 1.zip
new file mode 100644
index 0000000..098d4e5
Binary files /dev/null and b/test/failure/multi-disk zip files are not supported found disk number 1.zip differ
diff --git a/test/failure/too many length or distance symbols.zip b/test/failure/too many length or distance symbols.zip
new file mode 100644
index 0000000..50854ab
Binary files /dev/null and b/test/failure/too many length or distance symbols.zip differ
diff --git a/test/failure/unsupported compression method 1.zip b/test/failure/unsupported compression method 1.zip
new file mode 100644
index 0000000..3bbd831
Binary files /dev/null and b/test/failure/unsupported compression method 1.zip differ
diff --git a/test/success/cygwin-info-zip.zip b/test/success/cygwin-info-zip.zip
new file mode 100644
index 0000000..7dcb601
Binary files /dev/null and b/test/success/cygwin-info-zip.zip differ
diff --git a/test/success/cygwin-info-zip/a.txt b/test/success/cygwin-info-zip/a.txt
new file mode 100644
index 0000000..8bd6648
--- /dev/null
+++ b/test/success/cygwin-info-zip/a.txt
@@ -0,0 +1 @@
+asdf
diff --git a/test/success/cygwin-info-zip/b.txt b/test/success/cygwin-info-zip/b.txt
new file mode 100644
index 0000000..302b0b8
--- /dev/null
+++ b/test/success/cygwin-info-zip/b.txt
@@ -0,0 +1 @@
+bsdf
diff --git a/test/success/deflate.zip b/test/success/deflate.zip
new file mode 100644
index 0000000..26c5014
Binary files /dev/null and b/test/success/deflate.zip differ
diff --git a/test/success/deflate/index.js b/test/success/deflate/index.js
new file mode 100644
index 0000000..35137ea
--- /dev/null
+++ b/test/success/deflate/index.js
@@ -0,0 +1,262 @@
+var 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;
+}
diff --git a/test/success/directories.zip b/test/success/directories.zip
new file mode 100644
index 0000000..31803cb
Binary files /dev/null and b/test/success/directories.zip differ
diff --git a/test/success/directories/a/a.txt b/test/success/directories/a/a.txt
new file mode 100644
index 0000000..e69de29
diff --git a/test/success/directories/b/.git_please_make_this_directory b/test/success/directories/b/.git_please_make_this_directory
new file mode 100644
index 0000000..e69de29
diff --git a/test/success/empty.zip b/test/success/empty.zip
new file mode 100644
index 0000000..15cb0ec
Binary files /dev/null and b/test/success/empty.zip differ
diff --git a/test/success/empty/.git_please_make_this_directory b/test/success/empty/.git_please_make_this_directory
new file mode 100644
index 0000000..e69de29
diff --git a/test/success/linux-info-zip.zip b/test/success/linux-info-zip.zip
new file mode 100644
index 0000000..d30d815
Binary files /dev/null and b/test/success/linux-info-zip.zip differ
diff --git a/test/success/linux-info-zip/a.txt b/test/success/linux-info-zip/a.txt
new file mode 100644
index 0000000..8bd6648
--- /dev/null
+++ b/test/success/linux-info-zip/a.txt
@@ -0,0 +1 @@
+asdf
diff --git a/test/success/linux-info-zip/b.txt b/test/success/linux-info-zip/b.txt
new file mode 100644
index 0000000..302b0b8
--- /dev/null
+++ b/test/success/linux-info-zip/b.txt
@@ -0,0 +1 @@
+bsdf
diff --git a/test/success/unicode.zip b/test/success/unicode.zip
new file mode 100644
index 0000000..7c4327f
Binary files /dev/null and b/test/success/unicode.zip differ
diff --git "a/test/success/unicode/Turmion K\303\244til\303\266t/Hoitovirhe/Rautaketju.mp3" "b/test/success/unicode/Turmion K\303\244til\303\266t/Hoitovirhe/Rautaketju.mp3"
new file mode 100644
index 0000000..e69de29
diff --git "a/test/success/unicode/Turmion K\303\244til\303\266t/Pirun nyrkki/Mist\303\244 veri pakenee.mp3" "b/test/success/unicode/Turmion K\303\244til\303\266t/Pirun nyrkki/Mist\303\244 veri pakenee.mp3"
new file mode 100644
index 0000000..e69de29
diff --git a/test/success/windows-7-zip.zip b/test/success/windows-7-zip.zip
new file mode 100755
index 0000000..f2e5298
Binary files /dev/null and b/test/success/windows-7-zip.zip differ
diff --git a/test/success/windows-7-zip/a.txt b/test/success/windows-7-zip/a.txt
new file mode 100644
index 0000000..8bd6648
--- /dev/null
+++ b/test/success/windows-7-zip/a.txt
@@ -0,0 +1 @@
+asdf
diff --git a/test/success/windows-7-zip/b.txt b/test/success/windows-7-zip/b.txt
new file mode 100644
index 0000000..302b0b8
--- /dev/null
+++ b/test/success/windows-7-zip/b.txt
@@ -0,0 +1 @@
+bsdf
diff --git a/test/success/windows-compressed-folder.zip b/test/success/windows-compressed-folder.zip
new file mode 100755
index 0000000..c67d3a8
Binary files /dev/null and b/test/success/windows-compressed-folder.zip differ
diff --git a/test/success/windows-compressed-folder/a.txt b/test/success/windows-compressed-folder/a.txt
new file mode 100644
index 0000000..8bd6648
--- /dev/null
+++ b/test/success/windows-compressed-folder/a.txt
@@ -0,0 +1 @@
+asdf
diff --git a/test/success/windows-compressed-folder/b.txt b/test/success/windows-compressed-folder/b.txt
new file mode 100644
index 0000000..302b0b8
--- /dev/null
+++ b/test/success/windows-compressed-folder/b.txt
@@ -0,0 +1 @@
+bsdf
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..c01a78d
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,168 @@
+var yauzl = require("../");
+var fs = require("fs");
+var path = require("path");
+var Pend = require("pend");
+
+// 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;
+
+// success tests
+listZipFiles(path.join(__dirname, "success")).forEach(function(zipfilePath) {
+ var openFunctions = [
+ function(callback) { yauzl.open(zipfilePath, callback); },
+ function(callback) { yauzl.fromBuffer(fs.readFileSync(zipfilePath), callback); },
+ ];
+ openFunctions.forEach(function(openFunction, i) {
+ var testId = zipfilePath + "(" + ["fd", "buffer"][i] + "): ";
+ var expectedPathPrefix = zipfilePath.replace(/\.zip$/, "");
+ var expectedArchiveContents = {};
+ var DIRECTORY = 1; // not a string
+ recursiveRead(".");
+ function recursiveRead(name) {
+ var realPath = path.join(expectedPathPrefix, name);
+ if (fs.statSync(realPath).isFile()) {
+ if (path.basename(name) !== ".git_please_make_this_directory") {
+ expectedArchiveContents[name] = fs.readFileSync(realPath);
+ }
+ } else {
+ if (name !== ".") expectedArchiveContents[name] = DIRECTORY;
+ fs.readdirSync(realPath).forEach(function(child) {
+ recursiveRead(path.join(name, child));
+ });
+ }
+ }
+ pend.go(function(zipfileCallback) {
+ openFunction(function(err, zipfile) {
+ if (err) throw err;
+ var entryProcessing = new Pend();
+ entryProcessing.max = 1;
+ zipfile.on("entry", function(entry) {
+ var messagePrefix = testId + entry.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);
+ entryProcessing.go(function(entryCallback) {
+ var fileNameKey = entry.fileName.replace(/\/$/, "");
+ var expectedContents = expectedArchiveContents[fileNameKey];
+ if (expectedContents == null) {
+ throw new Error(messagePrefix + "not supposed to exist");
+ }
+ delete expectedArchiveContents[fileNameKey];
+ if (entry.fileName !== fileNameKey) {
+ // directory
+ console.log(messagePrefix + "pass");
+ entryCallback();
+ } else {
+ zipfile.openReadStream(entry, function(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");
+ entryCallback();
+ });
+ readStream.on("error", function(err) {
+ throw err;
+ });
+ });
+ }
+ });
+ });
+ zipfile.on("end", function() {
+ entryProcessing.wait(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) {
+ var expectedErrorMessage = path.basename(zipfilePath).replace(/\.zip$/, "");
+ var failedYet = false;
+ pend.go(function(cb) {
+ var operationsInProgress = 0;
+ yauzl.open(zipfilePath, function(err, zipfile) {
+ if (err) return checkErrorMessage(err);
+ zipfile.on("error", function(err) {
+ checkErrorMessage(err);
+ });
+ zipfile.on("entry", function(entry) {
+ // 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() {
+ 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();
+ }
+ });
+});
+
+pend.wait(function() {
+ // if you don't see this, something never happened.
+ console.log("done");
+});
+
+function listZipFiles(dir) {
+ var zipfilePaths = fs.readdirSync(dir).filter(function(filepath) {
+ return /\.zip$/.exec(filepath);
+ }).map(function(name) {
+ return path.relative(".", path.join(dir, name));
+ });
+ zipfilePaths.sort();
+ return zipfilePaths;
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-yauzl.git
More information about the Pkg-javascript-commits
mailing list