1 var fs = require("fs");
2 var zlib = require("zlib");
3 var fd_slicer = require("fd-slicer");
4 var util = require("util");
5 var EventEmitter = require("events").EventEmitter;
6 var Transform = require("stream").Transform;
7 var PassThrough = require("stream").PassThrough;
8 var Writable = require("stream").Writable;
11 exports.fromFd = fromFd;
12 exports.fromBuffer = fromBuffer;
13 exports.fromRandomAccessReader = fromRandomAccessReader;
14 exports.dosDateTimeToDate = dosDateTimeToDate;
15 exports.ZipFile = ZipFile;
16 exports.Entry = Entry;
17 exports.RandomAccessReader = RandomAccessReader;
19 function open(path, options, callback) {
20 if (typeof options === "function") {
24 if (options == null) options = {};
25 if (options.autoClose == null) options.autoClose = true;
26 if (options.lazyEntries == null) options.lazyEntries = false;
27 if (callback == null) callback = defaultCallback;
28 fs.open(path, "r", function(err, fd) {
29 if (err) return callback(err);
30 fromFd(fd, options, function(err, zipfile) {
31 if (err) fs.close(fd, defaultCallback);
32 callback(err, zipfile);
37 function fromFd(fd, options, callback) {
38 if (typeof options === "function") {
42 if (options == null) options = {};
43 if (options.autoClose == null) options.autoClose = false;
44 if (options.lazyEntries == null) options.lazyEntries = false;
45 if (callback == null) callback = defaultCallback;
46 fs.fstat(fd, function(err, stats) {
47 if (err) return callback(err);
48 var reader = fd_slicer.createFromFd(fd, {autoClose: true});
49 fromRandomAccessReader(reader, stats.size, options, callback);
53 function fromBuffer(buffer, options, callback) {
54 if (typeof options === "function") {
58 if (options == null) options = {};
59 options.autoClose = false;
60 if (options.lazyEntries == null) options.lazyEntries = false;
61 // i got your open file right here.
62 var reader = fd_slicer.createFromBuffer(buffer);
63 fromRandomAccessReader(reader, buffer.length, options, callback);
66 function fromRandomAccessReader(reader, totalSize, options, callback) {
67 if (typeof options === "function") {
71 if (options == null) options = {};
72 if (options.autoClose == null) options.autoClose = true;
73 if (options.lazyEntries == null) options.lazyEntries = false;
74 if (callback == null) callback = defaultCallback;
75 if (typeof totalSize !== "number") throw new Error("expected totalSize parameter to be a number");
76 if (totalSize > Number.MAX_SAFE_INTEGER) {
77 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.");
80 // the matching unref() call is in zipfile.close()
83 // eocdr means End of Central Directory Record.
84 // search backwards for the eocdr signature.
85 // the last field of the eocdr is a variable-length comment.
86 // 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.
87 // as a consequence of this design decision, it's possible to have ambiguous zip file metadata if a coherent eocdr was in the comment.
88 // 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.
89 var eocdrWithoutCommentSize = 22;
90 var maxCommentSize = 0x10000; // 2-byte size
91 var bufferSize = Math.min(eocdrWithoutCommentSize + maxCommentSize, totalSize);
92 var buffer = new Buffer(bufferSize);
93 var bufferReadStart = totalSize - buffer.length;
94 readAndAssertNoEof(reader, buffer, 0, bufferSize, bufferReadStart, function(err) {
95 if (err) return callback(err);
96 for (var i = bufferSize - eocdrWithoutCommentSize; i >= 0; i -= 1) {
97 if (buffer.readUInt32LE(i) !== 0x06054b50) continue;
99 var eocdrBuffer = buffer.slice(i);
101 // 0 - End of central directory signature = 0x06054b50
102 // 4 - Number of this disk
103 var diskNumber = eocdrBuffer.readUInt16LE(4);
104 if (diskNumber !== 0) return callback(new Error("multi-disk zip files are not supported: found disk number: " + diskNumber));
105 // 6 - Disk where central directory starts
106 // 8 - Number of central directory records on this disk
107 // 10 - Total number of central directory records
108 var entryCount = eocdrBuffer.readUInt16LE(10);
109 // 12 - Size of central directory (bytes)
110 // 16 - Offset of start of central directory, relative to start of archive
111 var centralDirectoryOffset = eocdrBuffer.readUInt32LE(16);
112 // 20 - Comment length
113 var commentLength = eocdrBuffer.readUInt16LE(20);
114 var expectedCommentLength = eocdrBuffer.length - eocdrWithoutCommentSize;
115 if (commentLength !== expectedCommentLength) {
116 return callback(new Error("invalid comment length. expected: " + expectedCommentLength + ". found: " + commentLength));
119 // the encoding is always cp437.
120 var comment = bufferToString(eocdrBuffer, 22, eocdrBuffer.length, false);
122 if (!(entryCount === 0xffff || centralDirectoryOffset === 0xffffffff)) {
123 return callback(null, new ZipFile(reader, centralDirectoryOffset, totalSize, entryCount, comment, options.autoClose, options.lazyEntries));
128 // ZIP64 Zip64 end of central directory locator
129 var zip64EocdlBuffer = new Buffer(20);
130 var zip64EocdlOffset = bufferReadStart + i - zip64EocdlBuffer.length;
131 readAndAssertNoEof(reader, zip64EocdlBuffer, 0, zip64EocdlBuffer.length, zip64EocdlOffset, function(err) {
132 if (err) return callback(err);
134 // 0 - zip64 end of central dir locator signature = 0x07064b50
135 if (zip64EocdlBuffer.readUInt32LE(0) !== 0x07064b50) {
136 return callback(new Error("invalid ZIP64 End of Central Directory Locator signature"));
138 // 4 - number of the disk with the start of the zip64 end of central directory
139 // 8 - relative offset of the zip64 end of central directory record
140 var zip64EocdrOffset = readUInt64LE(zip64EocdlBuffer, 8);
141 // 16 - total number of disks
143 // ZIP64 end of central directory record
144 var zip64EocdrBuffer = new Buffer(56);
145 readAndAssertNoEof(reader, zip64EocdrBuffer, 0, zip64EocdrBuffer.length, zip64EocdrOffset, function(err) {
146 if (err) return callback(err);
148 // 0 - zip64 end of central dir signature 4 bytes (0x06064b50)
149 if (zip64EocdrBuffer.readUInt32LE(0) !== 0x06064b50) return callback(new Error("invalid ZIP64 end of central directory record signature"));
150 // 4 - size of zip64 end of central directory record 8 bytes
151 // 12 - version made by 2 bytes
152 // 14 - version needed to extract 2 bytes
153 // 16 - number of this disk 4 bytes
154 // 20 - number of the disk with the start of the central directory 4 bytes
155 // 24 - total number of entries in the central directory on this disk 8 bytes
156 // 32 - total number of entries in the central directory 8 bytes
157 entryCount = readUInt64LE(zip64EocdrBuffer, 32);
158 // 40 - size of the central directory 8 bytes
159 // 48 - offset of start of central directory with respect to the starting disk number 8 bytes
160 centralDirectoryOffset = readUInt64LE(zip64EocdrBuffer, 48);
161 // 56 - zip64 extensible data sector (variable size)
162 return callback(null, new ZipFile(reader, centralDirectoryOffset, totalSize, entryCount, comment, options.autoClose, options.lazyEntries));
167 callback(new Error("end of central directory record signature not found"));
171 util.inherits(ZipFile, EventEmitter);
172 function ZipFile(reader, centralDirectoryOffset, fileSize, entryCount, comment, autoClose, lazyEntries) {
174 EventEmitter.call(self);
175 self.reader = reader;
176 // forward close events
177 self.reader.on("error", function(err) {
178 // error closing the fd
179 emitError(self, err);
181 self.reader.once("close", function() {
184 self.readEntryCursor = centralDirectoryOffset;
185 self.fileSize = fileSize;
186 self.entryCount = entryCount;
187 self.comment = comment;
188 self.entriesRead = 0;
189 self.autoClose = !!autoClose;
190 self.lazyEntries = !!lazyEntries;
192 self.emittedError = false;
194 if (!self.lazyEntries) self.readEntry();
196 ZipFile.prototype.close = function() {
197 if (!this.isOpen) return;
202 function emitErrorAndAutoClose(self, err) {
203 if (self.autoClose) self.close();
204 emitError(self, err);
206 function emitError(self, err) {
207 if (self.emittedError) return;
208 self.emittedError = true;
209 self.emit("error", err);
212 ZipFile.prototype.readEntry = function() {
214 if (self.entryCount === self.entriesRead) {
215 // done with metadata
216 setImmediate(function() {
217 if (self.autoClose) self.close();
218 if (self.emittedError) return;
223 if (self.emittedError) return;
224 var buffer = new Buffer(46);
225 readAndAssertNoEof(self.reader, buffer, 0, buffer.length, self.readEntryCursor, function(err) {
226 if (err) return emitErrorAndAutoClose(self, err);
227 if (self.emittedError) return;
228 var entry = new Entry();
229 // 0 - Central directory file header signature
230 var signature = buffer.readUInt32LE(0);
231 if (signature !== 0x02014b50) return emitErrorAndAutoClose(self, new Error("invalid central directory file header signature: 0x" + signature.toString(16)));
232 // 4 - Version made by
233 entry.versionMadeBy = buffer.readUInt16LE(4);
234 // 6 - Version needed to extract (minimum)
235 entry.versionNeededToExtract = buffer.readUInt16LE(6);
236 // 8 - General purpose bit flag
237 entry.generalPurposeBitFlag = buffer.readUInt16LE(8);
238 // 10 - Compression method
239 entry.compressionMethod = buffer.readUInt16LE(10);
240 // 12 - File last modification time
241 entry.lastModFileTime = buffer.readUInt16LE(12);
242 // 14 - File last modification date
243 entry.lastModFileDate = buffer.readUInt16LE(14);
245 entry.crc32 = buffer.readUInt32LE(16);
246 // 20 - Compressed size
247 entry.compressedSize = buffer.readUInt32LE(20);
248 // 24 - Uncompressed size
249 entry.uncompressedSize = buffer.readUInt32LE(24);
250 // 28 - File name length (n)
251 entry.fileNameLength = buffer.readUInt16LE(28);
252 // 30 - Extra field length (m)
253 entry.extraFieldLength = buffer.readUInt16LE(30);
254 // 32 - File comment length (k)
255 entry.fileCommentLength = buffer.readUInt16LE(32);
256 // 34 - Disk number where file starts
257 // 36 - Internal file attributes
258 entry.internalFileAttributes = buffer.readUInt16LE(36);
259 // 38 - External file attributes
260 entry.externalFileAttributes = buffer.readUInt32LE(38);
261 // 42 - Relative offset of local file header
262 entry.relativeOffsetOfLocalHeader = buffer.readUInt32LE(42);
264 self.readEntryCursor += 46;
266 buffer = new Buffer(entry.fileNameLength + entry.extraFieldLength + entry.fileCommentLength);
267 readAndAssertNoEof(self.reader, buffer, 0, buffer.length, self.readEntryCursor, function(err) {
268 if (err) return emitErrorAndAutoClose(self, err);
269 if (self.emittedError) return;
271 var isUtf8 = entry.generalPurposeBitFlag & 0x800
273 entry.fileName = bufferToString(buffer, 0, entry.fileNameLength, isUtf8);
275 return emitErrorAndAutoClose(self, e);
278 // 46+n - Extra field
279 var fileCommentStart = entry.fileNameLength + entry.extraFieldLength;
280 var extraFieldBuffer = buffer.slice(entry.fileNameLength, fileCommentStart);
281 entry.extraFields = [];
283 while (i < extraFieldBuffer.length) {
284 var headerId = extraFieldBuffer.readUInt16LE(i + 0);
285 var dataSize = extraFieldBuffer.readUInt16LE(i + 2);
286 var dataStart = i + 4;
287 var dataEnd = dataStart + dataSize;
288 var dataBuffer = new Buffer(dataSize);
289 extraFieldBuffer.copy(dataBuffer, 0, dataStart, dataEnd);
290 entry.extraFields.push({
297 // 46+n+m - File comment
299 entry.fileComment = bufferToString(buffer, fileCommentStart, fileCommentStart + entry.fileCommentLength, isUtf8);
301 return emitErrorAndAutoClose(self, e);
304 self.readEntryCursor += buffer.length;
305 self.entriesRead += 1;
307 if (entry.uncompressedSize === 0xffffffff ||
308 entry.compressedSize === 0xffffffff ||
309 entry.relativeOffsetOfLocalHeader === 0xffffffff) {
311 // find the Zip64 Extended Information Extra Field
312 var zip64EiefBuffer = null;
313 for (var i = 0; i < entry.extraFields.length; i++) {
314 var extraField = entry.extraFields[i];
315 if (extraField.id === 0x0001) {
316 zip64EiefBuffer = extraField.data;
320 if (zip64EiefBuffer == null) return emitErrorAndAutoClose(self, new Error("expected Zip64 Extended Information Extra Field"));
322 // 0 - Original Size 8 bytes
323 if (entry.uncompressedSize === 0xffffffff) {
324 if (index + 8 > zip64EiefBuffer.length) return emitErrorAndAutoClose(self, new Error("Zip64 Extended Information Extra Field does not include Original Size"));
325 entry.uncompressedSize = readUInt64LE(zip64EiefBuffer, index);
328 // 8 - Compressed Size 8 bytes
329 if (entry.compressedSize === 0xffffffff) {
330 if (index + 8 > zip64EiefBuffer.length) return emitErrorAndAutoClose(self, new Error("Zip64 Extended Information Extra Field does not include Compressed Size"));
331 entry.compressedSize = readUInt64LE(zip64EiefBuffer, index);
334 // 16 - Relative Header Offset 8 bytes
335 if (entry.relativeOffsetOfLocalHeader === 0xffffffff) {
336 if (index + 8 > zip64EiefBuffer.length) return emitErrorAndAutoClose(self, new Error("Zip64 Extended Information Extra Field does not include Relative Header Offset"));
337 entry.relativeOffsetOfLocalHeader = readUInt64LE(zip64EiefBuffer, index);
340 // 24 - Disk Start Number 4 bytes
343 // validate file size
344 if (entry.compressionMethod === 0) {
345 if (entry.compressedSize !== entry.uncompressedSize) {
346 var msg = "compressed/uncompressed size mismatch for stored file: " + entry.compressedSize + " != " + entry.uncompressedSize;
347 return emitErrorAndAutoClose(self, new Error(msg));
351 // validate file name
352 if (entry.fileName.indexOf("\\") !== -1) return emitErrorAndAutoClose(self, new Error("invalid characters in fileName: " + entry.fileName));
353 if (/^[a-zA-Z]:/.test(entry.fileName) || /^\//.test(entry.fileName)) return emitErrorAndAutoClose(self, new Error("absolute path: " + entry.fileName));
354 if (entry.fileName.split("/").indexOf("..") !== -1) return emitErrorAndAutoClose(self, new Error("invalid relative path: " + entry.fileName));
355 self.emit("entry", entry);
357 if (!self.lazyEntries) self.readEntry();
362 ZipFile.prototype.openReadStream = function(entry, callback) {
364 if (!self.isOpen) return callback(new Error("closed"));
365 // make sure we don't lose the fd before we open the actual read stream
367 var buffer = new Buffer(30);
368 readAndAssertNoEof(self.reader, buffer, 0, buffer.length, entry.relativeOffsetOfLocalHeader, function(err) {
370 if (err) return callback(err);
371 // 0 - Local file header signature = 0x04034b50
372 var signature = buffer.readUInt32LE(0);
373 if (signature !== 0x04034b50) return callback(new Error("invalid local file header signature: 0x" + signature.toString(16)));
374 // all this should be redundant
375 // 4 - Version needed to extract (minimum)
376 // 6 - General purpose bit flag
377 // 8 - Compression method
378 // 10 - File last modification time
379 // 12 - File last modification date
381 // 18 - Compressed size
382 // 22 - Uncompressed size
383 // 26 - File name length (n)
384 var fileNameLength = buffer.readUInt16LE(26);
385 // 28 - Extra field length (m)
386 var extraFieldLength = buffer.readUInt16LE(28);
388 // 30+n - Extra field
389 var localFileHeaderEnd = entry.relativeOffsetOfLocalHeader + buffer.length + fileNameLength + extraFieldLength;
391 if (entry.compressionMethod === 0) {
392 // 0 - The file is stored (no compression)
394 } else if (entry.compressionMethod === 8) {
395 // 8 - The file is Deflated
398 return callback(new Error("unsupported compression method: " + entry.compressionMethod));
400 var fileDataStart = localFileHeaderEnd;
401 var fileDataEnd = fileDataStart + entry.compressedSize;
402 if (entry.compressedSize !== 0) {
403 // bounds check now, because the read streams will probably not complain loud enough.
404 // since we're dealing with an unsigned offset plus an unsigned size,
405 // we only have 1 thing to check for.
406 if (fileDataEnd > self.fileSize) {
407 return callback(new Error("file data overflows file bounds: " +
408 fileDataStart + " + " + entry.compressedSize + " > " + self.fileSize));
411 var readStream = self.reader.createReadStream({start: fileDataStart, end: fileDataEnd});
412 var endpointStream = readStream;
414 var destroyed = false;
415 var inflateFilter = zlib.createInflateRaw();
416 readStream.on("error", function(err) {
417 // setImmediate here because errors can be emitted during the first call to pipe()
418 setImmediate(function() {
419 if (!destroyed) inflateFilter.emit("error", err);
423 var checkerStream = new AssertByteCountStream(entry.uncompressedSize);
424 inflateFilter.on("error", function(err) {
425 // forward zlib errors to the client-visible stream
426 setImmediate(function() {
427 if (!destroyed) checkerStream.emit("error", err);
430 checkerStream.destroy = function() {
432 inflateFilter.unpipe(checkerStream);
433 readStream.unpipe(inflateFilter);
434 // TODO: the inflateFilter now causes a memory leak. see Issue #27.
435 readStream.destroy();
437 endpointStream = readStream.pipe(inflateFilter).pipe(checkerStream);
439 callback(null, endpointStream);
448 Entry.prototype.getLastModDate = function() {
449 return dosDateTimeToDate(this.lastModFileDate, this.lastModFileTime);
452 function dosDateTimeToDate(date, time) {
453 var day = date & 0x1f; // 1-31
454 var month = (date >> 5 & 0xf) - 1; // 1-12, 0-11
455 var year = (date >> 9 & 0x7f) + 1980; // 0-128, 1980-2108
458 var second = (time & 0x1f) * 2; // 0-29, 0-58 (even numbers)
459 var minute = time >> 5 & 0x3f; // 0-59
460 var hour = time >> 11 & 0x1f; // 0-23
462 return new Date(year, month, day, hour, minute, second, millisecond);
465 function readAndAssertNoEof(reader, buffer, offset, length, position, callback) {
467 // fs.read will throw an out-of-bounds error if you try to read 0 bytes from a 0 byte file
468 return setImmediate(function() { callback(null, new Buffer(0)); });
470 reader.read(buffer, offset, length, position, function(err, bytesRead) {
471 if (err) return callback(err);
472 if (bytesRead < length) return callback(new Error("unexpected EOF"));
477 util.inherits(AssertByteCountStream, Transform);
478 function AssertByteCountStream(byteCount) {
479 Transform.call(this);
480 this.actualByteCount = 0;
481 this.expectedByteCount = byteCount;
483 AssertByteCountStream.prototype._transform = function(chunk, encoding, cb) {
484 this.actualByteCount += chunk.length;
485 if (this.actualByteCount > this.expectedByteCount) {
486 var msg = "too many bytes in the stream. expected " + this.expectedByteCount + ". got at least " + this.actualByteCount;
487 return cb(new Error(msg));
491 AssertByteCountStream.prototype._flush = function(cb) {
492 if (this.actualByteCount < this.expectedByteCount) {
493 var msg = "not enough bytes in the stream. expected " + this.expectedByteCount + ". got only " + this.actualByteCount;
494 return cb(new Error(msg));
499 util.inherits(RandomAccessReader, EventEmitter);
500 function RandomAccessReader() {
501 EventEmitter.call(this);
504 RandomAccessReader.prototype.ref = function() {
507 RandomAccessReader.prototype.unref = function() {
511 if (self.refCount > 0) return;
512 if (self.refCount < 0) throw new Error("invalid unref");
514 self.close(onCloseDone);
516 function onCloseDone(err) {
517 if (err) return self.emit('error', err);
521 RandomAccessReader.prototype.createReadStream = function(options) {
522 var start = options.start;
523 var end = options.end;
525 var emptyStream = new PassThrough();
526 setImmediate(function() {
531 var stream = this._readStreamForRange(start, end);
533 var destroyed = false;
534 var refUnrefFilter = new RefUnrefFilter(this);
535 stream.on("error", function(err) {
536 setImmediate(function() {
537 if (!destroyed) refUnrefFilter.emit("error", err);
540 refUnrefFilter.destroy = function() {
541 stream.unpipe(refUnrefFilter);
542 refUnrefFilter.unref();
546 var byteCounter = new AssertByteCountStream(end - start);
547 refUnrefFilter.on("error", function(err) {
548 setImmediate(function() {
549 if (!destroyed) byteCounter.emit("error", err);
552 byteCounter.destroy = function() {
554 refUnrefFilter.unpipe(byteCounter);
555 refUnrefFilter.destroy();
558 return stream.pipe(refUnrefFilter).pipe(byteCounter);
560 RandomAccessReader.prototype._readStreamForRange = function(start, end) {
561 throw new Error("not implemented");
563 RandomAccessReader.prototype.read = function(buffer, offset, length, position, callback) {
564 var readStream = this.createReadStream({start: position, end: position + length});
565 var writeStream = new Writable();
567 writeStream._write = function(chunk, encoding, cb) {
568 chunk.copy(buffer, offset + written, 0, chunk.length);
569 written += chunk.length;
572 writeStream.on("finish", callback);
573 readStream.on("error", function(error) {
576 readStream.pipe(writeStream);
578 RandomAccessReader.prototype.close = function(callback) {
579 setImmediate(callback);
582 util.inherits(RefUnrefFilter, PassThrough);
583 function RefUnrefFilter(context) {
584 PassThrough.call(this);
585 this.context = context;
587 this.unreffedYet = false;
589 RefUnrefFilter.prototype._flush = function(cb) {
593 RefUnrefFilter.prototype.unref = function(cb) {
594 if (this.unreffedYet) return;
595 this.unreffedYet = true;
596 this.context.unref();
599 var cp437 = '\u0000☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ';
600 function bufferToString(buffer, start, end, isUtf8) {
602 return buffer.toString("utf8", start, end);
605 for (var i = start; i < end; i++) {
606 result += cp437[buffer[i]];
612 function readUInt64LE(buffer, offset) {
613 // there is no native function for this, because we can't actually store 64-bit integers precisely.
614 // after 53 bits, JavaScript's Number type (IEEE 754 double) can't store individual integers anymore.
615 // but since 53 bits is a whole lot more than 32 bits, we do our best anyway.
616 var lower32 = buffer.readUInt32LE(offset);
617 var upper32 = buffer.readUInt32LE(offset + 4);
618 // we can't use bitshifting here, because JavaScript bitshifting only works on 32-bit integers.
619 return upper32 * 0x100000000 + lower32;
620 // as long as we're bounds checking the result of this function against the total file size,
621 // we'll catch any overflow errors, because we already made sure the total file size was within reason.
624 function defaultCallback(err) {