1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var
|
9 | util = require('util'),
|
10 | fs = require('fs'),
|
11 | path = require('path'),
|
12 | events = require('events'),
|
13 | zlib = require('zlib'),
|
14 | stream = require('stream');
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | var consts = {
|
21 |
|
22 | LOCHDR : 30,
|
23 | LOCSIG : 0x04034b50,
|
24 | LOCVER : 4,
|
25 | LOCFLG : 6,
|
26 | LOCHOW : 8,
|
27 | LOCTIM : 10,
|
28 | LOCCRC : 14,
|
29 | LOCSIZ : 18,
|
30 | LOCLEN : 22,
|
31 | LOCNAM : 26,
|
32 | LOCEXT : 28,
|
33 |
|
34 |
|
35 | EXTSIG : 0x08074b50,
|
36 | EXTHDR : 16,
|
37 | EXTCRC : 4,
|
38 | EXTSIZ : 8,
|
39 | EXTLEN : 12,
|
40 |
|
41 |
|
42 | CENHDR : 46,
|
43 | CENSIG : 0x02014b50,
|
44 | CENVEM : 4,
|
45 | CENVER : 6,
|
46 | CENFLG : 8,
|
47 | CENHOW : 10,
|
48 | CENTIM : 12,
|
49 | CENCRC : 16,
|
50 | CENSIZ : 20,
|
51 | CENLEN : 24,
|
52 | CENNAM : 28,
|
53 | CENEXT : 30,
|
54 | CENCOM : 32,
|
55 | CENDSK : 34,
|
56 | CENATT : 36,
|
57 | CENATX : 38,
|
58 | CENOFF : 42,
|
59 |
|
60 |
|
61 | ENDHDR : 22,
|
62 | ENDSIG : 0x06054b50,
|
63 | ENDSIGFIRST : 0x50,
|
64 | ENDSUB : 8,
|
65 | ENDTOT : 10,
|
66 | ENDSIZ : 12,
|
67 | ENDOFF : 16,
|
68 | ENDCOM : 20,
|
69 | MAXFILECOMMENT : 0xFFFF,
|
70 |
|
71 |
|
72 | ENDL64HDR : 20,
|
73 | ENDL64SIG : 0x07064b50,
|
74 | ENDL64SIGFIRST : 0x50,
|
75 | ENDL64OFS : 8,
|
76 |
|
77 |
|
78 | END64HDR : 56,
|
79 | END64SIG : 0x06064b50,
|
80 | END64SIGFIRST : 0x50,
|
81 | END64SUB : 24,
|
82 | END64TOT : 32,
|
83 | END64SIZ : 40,
|
84 | END64OFF : 48,
|
85 |
|
86 |
|
87 | STORED : 0,
|
88 | SHRUNK : 1,
|
89 | REDUCED1 : 2,
|
90 | REDUCED2 : 3,
|
91 | REDUCED3 : 4,
|
92 | REDUCED4 : 5,
|
93 | IMPLODED : 6,
|
94 |
|
95 | DEFLATED : 8,
|
96 | ENHANCED_DEFLATED: 9,
|
97 | PKWARE : 10,
|
98 |
|
99 | BZIP2 : 12,
|
100 |
|
101 | LZMA : 14,
|
102 |
|
103 | IBM_TERSE : 18,
|
104 | IBM_LZ77 : 19,
|
105 |
|
106 |
|
107 | FLG_ENC : 0,
|
108 | FLG_COMP1 : 1,
|
109 | FLG_COMP2 : 2,
|
110 | FLG_DESC : 4,
|
111 | FLG_ENH : 8,
|
112 | FLG_STR : 16,
|
113 | FLG_LNG : 1024,
|
114 | FLG_MSK : 4096,
|
115 | FLG_ENTRY_ENC : 1,
|
116 |
|
117 |
|
118 | EF_ID : 0,
|
119 | EF_SIZE : 2,
|
120 |
|
121 |
|
122 | ID_ZIP64 : 0x0001,
|
123 | ID_AVINFO : 0x0007,
|
124 | ID_PFS : 0x0008,
|
125 | ID_OS2 : 0x0009,
|
126 | ID_NTFS : 0x000a,
|
127 | ID_OPENVMS : 0x000c,
|
128 | ID_UNIX : 0x000d,
|
129 | ID_FORK : 0x000e,
|
130 | ID_PATCH : 0x000f,
|
131 | ID_X509_PKCS7 : 0x0014,
|
132 | ID_X509_CERTID_F : 0x0015,
|
133 | ID_X509_CERTID_C : 0x0016,
|
134 | ID_STRONGENC : 0x0017,
|
135 | ID_RECORD_MGT : 0x0018,
|
136 | ID_X509_PKCS7_RL : 0x0019,
|
137 | ID_IBM1 : 0x0065,
|
138 | ID_IBM2 : 0x0066,
|
139 | ID_POSZIP : 0x4690,
|
140 |
|
141 | EF_ZIP64_OR_32 : 0xffffffff,
|
142 | EF_ZIP64_OR_16 : 0xffff
|
143 | };
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | var StreamZip = function(config) {
|
150 | var
|
151 | fd,
|
152 | fileSize,
|
153 | chunkSize,
|
154 | ready = false,
|
155 | that = this,
|
156 | op,
|
157 | centralDirectory,
|
158 | closed,
|
159 |
|
160 | entries = config.storeEntries !== false ? {} : null,
|
161 | fileName = config.file;
|
162 |
|
163 | open();
|
164 |
|
165 | function open() {
|
166 | fs.open(fileName, 'r', function(err, f) {
|
167 | if (err)
|
168 | return that.emit('error', err);
|
169 | fd = f;
|
170 | fs.fstat(fd, function(err, stat) {
|
171 | if (err)
|
172 | return that.emit('error', err);
|
173 | fileSize = stat.size;
|
174 | chunkSize = config.chunkSize || Math.round(fileSize / 1000);
|
175 | chunkSize = Math.max(Math.min(chunkSize, Math.min(128*1024, fileSize)), Math.min(1024, fileSize));
|
176 | readCentralDirectory();
|
177 | });
|
178 | });
|
179 | }
|
180 |
|
181 | function readUntilFoundCallback(err, bytesRead) {
|
182 | if (err || !bytesRead)
|
183 | return that.emit('error', err || 'Archive read error');
|
184 | var
|
185 | buffer = op.win.buffer,
|
186 | pos = op.lastPos,
|
187 | bufferPosition = pos - op.win.position,
|
188 | minPos = op.minPos;
|
189 | while (--pos >= minPos && --bufferPosition >= 0) {
|
190 | if (buffer.length - bufferPosition >= 4 &&
|
191 | buffer[bufferPosition] === op.firstByte) {
|
192 | if (buffer.readUInt32LE(bufferPosition) === op.sig) {
|
193 | op.lastBufferPosition = bufferPosition;
|
194 | op.lastBytesRead = bytesRead;
|
195 | op.complete();
|
196 | return;
|
197 | }
|
198 | }
|
199 | }
|
200 | if (pos === minPos) {
|
201 | return that.emit('error', 'Bad archive');
|
202 | }
|
203 | op.lastPos = pos + 1;
|
204 | op.chunkSize *= 2;
|
205 | if (pos <= minPos)
|
206 | return that.emit('error', 'Bad archive');
|
207 | var expandLength = Math.min(op.chunkSize, pos - minPos);
|
208 | op.win.expandLeft(expandLength, readUntilFoundCallback);
|
209 |
|
210 | }
|
211 |
|
212 | function readCentralDirectory() {
|
213 | var totalReadLength = Math.min(consts.ENDHDR + consts.MAXFILECOMMENT, fileSize);
|
214 | op = {
|
215 | win: new FileWindowBuffer(fd),
|
216 | totalReadLength: totalReadLength,
|
217 | minPos: fileSize - totalReadLength,
|
218 | lastPos: fileSize,
|
219 | chunkSize: Math.min(1024, chunkSize),
|
220 | firstByte: consts.ENDSIGFIRST,
|
221 | sig: consts.ENDSIG,
|
222 | complete: readCentralDirectoryComplete
|
223 | };
|
224 | op.win.read(fileSize - op.chunkSize, op.chunkSize, readUntilFoundCallback);
|
225 | }
|
226 |
|
227 | function readCentralDirectoryComplete() {
|
228 | var buffer = op.win.buffer;
|
229 | var pos = op.lastBufferPosition;
|
230 | try {
|
231 | centralDirectory = new CentralDirectoryHeader();
|
232 | centralDirectory.read(buffer.slice(pos, pos + consts.ENDHDR));
|
233 | centralDirectory.headerOffset = op.win.position + pos;
|
234 | if (centralDirectory.commentLength)
|
235 | that.comment = buffer.slice(pos + consts.ENDHDR,
|
236 | pos + consts.ENDHDR + centralDirectory.commentLength).toString();
|
237 | else
|
238 | that.comment = null;
|
239 | that.entriesCount = centralDirectory.volumeEntries;
|
240 | that.centralDirectory = centralDirectory;
|
241 | if (centralDirectory.volumeEntries === consts.EF_ZIP64_OR_16 && centralDirectory.totalEntries === consts.EF_ZIP64_OR_16
|
242 | || centralDirectory.size === consts.EF_ZIP64_OR_32 || centralDirectory.offset === consts.EF_ZIP64_OR_32) {
|
243 | readZip64CentralDirectoryLocator();
|
244 | } else {
|
245 | op = {};
|
246 | readEntries();
|
247 | }
|
248 | } catch (err) {
|
249 | that.emit('error', err);
|
250 | }
|
251 | }
|
252 |
|
253 | function readZip64CentralDirectoryLocator() {
|
254 | var length = consts.ENDL64HDR;
|
255 | if (op.lastBufferPosition > length) {
|
256 | op.lastBufferPosition -= length;
|
257 | readZip64CentralDirectoryLocatorComplete();
|
258 | } else {
|
259 | op = {
|
260 | win: op.win,
|
261 | totalReadLength: length,
|
262 | minPos: op.win.position - length,
|
263 | lastPos: op.win.position,
|
264 | chunkSize: op.chunkSize,
|
265 | firstByte: consts.ENDL64SIGFIRST,
|
266 | sig: consts.ENDL64SIG,
|
267 | complete: readZip64CentralDirectoryLocatorComplete
|
268 | };
|
269 | op.win.read(op.lastPos - op.chunkSize, op.chunkSize, readUntilFoundCallback);
|
270 | }
|
271 | }
|
272 |
|
273 | function readZip64CentralDirectoryLocatorComplete() {
|
274 | var buffer = op.win.buffer;
|
275 | var locHeader = new CentralDirectoryLoc64Header();
|
276 | locHeader.read(buffer.slice(op.lastBufferPosition, op.lastBufferPosition + consts.ENDL64HDR));
|
277 | var readLength = fileSize - locHeader.headerOffset;
|
278 | op = {
|
279 | win: op.win,
|
280 | totalReadLength: readLength,
|
281 | minPos: locHeader.headerOffset,
|
282 | lastPos: op.lastPos,
|
283 | chunkSize: op.chunkSize,
|
284 | firstByte: consts.END64SIGFIRST,
|
285 | sig: consts.END64SIG,
|
286 | complete: readZip64CentralDirectoryComplete
|
287 | };
|
288 | op.win.read(fileSize - op.chunkSize, op.chunkSize, readUntilFoundCallback);
|
289 | }
|
290 |
|
291 | function readZip64CentralDirectoryComplete() {
|
292 | var buffer = op.win.buffer;
|
293 | var zip64cd = new CentralDirectoryZip64Header();
|
294 | zip64cd.read(buffer.slice(op.lastBufferPosition, op.lastBufferPosition + consts.END64HDR));
|
295 | that.centralDirectory.volumeEntries = zip64cd.volumeEntries;
|
296 | that.centralDirectory.totalEntries = zip64cd.totalEntries;
|
297 | that.centralDirectory.size = zip64cd.size;
|
298 | that.centralDirectory.offset = zip64cd.offset;
|
299 | that.entriesCount = zip64cd.volumeEntries;
|
300 | op = {};
|
301 | readEntries();
|
302 | }
|
303 |
|
304 | function readEntries() {
|
305 | op = {
|
306 | win: new FileWindowBuffer(fd),
|
307 | pos: centralDirectory.offset,
|
308 | chunkSize: chunkSize,
|
309 | entriesLeft: centralDirectory.volumeEntries
|
310 | };
|
311 | op.win.read(op.pos, Math.min(chunkSize, fileSize - op.pos), readEntriesCallback);
|
312 | }
|
313 |
|
314 | function readEntriesCallback(err, bytesRead) {
|
315 | if (err || !bytesRead)
|
316 | return that.emit('error', err || 'Entries read error');
|
317 | var
|
318 | buffer = op.win.buffer,
|
319 | bufferPos = op.pos - op.win.position,
|
320 | bufferLength = buffer.length,
|
321 | entry = op.entry;
|
322 | try {
|
323 | while (op.entriesLeft > 0) {
|
324 | if (!entry) {
|
325 | entry = new ZipEntry();
|
326 | entry.readHeader(buffer, bufferPos);
|
327 | entry.headerOffset = op.win.position + bufferPos;
|
328 | op.entry = entry;
|
329 | op.pos += consts.CENHDR;
|
330 | bufferPos += consts.CENHDR;
|
331 | }
|
332 | var entryHeaderSize = entry.fnameLen + entry.extraLen + entry.comLen;
|
333 | var advanceBytes = entryHeaderSize + (op.entriesLeft > 1 ? consts.CENHDR : 0);
|
334 | if (bufferLength - bufferPos < advanceBytes) {
|
335 | op.win.moveRight(chunkSize, readEntriesCallback, bufferPos);
|
336 | op.move = true;
|
337 | return;
|
338 | }
|
339 | entry.read(buffer, bufferPos);
|
340 | if (!config.skipEntryNameValidation) {
|
341 | entry.validateName();
|
342 | }
|
343 | if (entries)
|
344 | entries[entry.name] = entry;
|
345 | that.emit('entry', entry);
|
346 | op.entry = entry = null;
|
347 | op.entriesLeft--;
|
348 | op.pos += entryHeaderSize;
|
349 | bufferPos += entryHeaderSize;
|
350 | }
|
351 | that.emit('ready');
|
352 | } catch (err) {
|
353 | that.emit('error', err);
|
354 | }
|
355 | }
|
356 |
|
357 | function checkEntriesExist() {
|
358 | if (!entries)
|
359 | throw new Error('storeEntries disabled');
|
360 | }
|
361 |
|
362 | Object.defineProperty(this, 'ready', { get: function() { return ready; } });
|
363 |
|
364 | this.entry = function(name) {
|
365 | checkEntriesExist();
|
366 | return entries[name];
|
367 | };
|
368 |
|
369 | this.entries = function() {
|
370 | checkEntriesExist();
|
371 | return entries;
|
372 | };
|
373 |
|
374 | this.stream = function(entry, callback) {
|
375 | return this.openEntry(entry, function(err, entry) {
|
376 | if (err)
|
377 | return callback(err);
|
378 | var offset = dataOffset(entry);
|
379 | var entryStream = new EntryDataReaderStream(fd, offset, entry.compressedSize);
|
380 | if (entry.method === consts.STORED) {
|
381 | } else if (entry.method === consts.DEFLATED || entry.method === consts.ENHANCED_DEFLATED) {
|
382 | entryStream = entryStream.pipe(zlib.createInflateRaw());
|
383 | } else {
|
384 | return callback('Unknown compression method: ' + entry.method);
|
385 | }
|
386 | if (canVerifyCrc(entry))
|
387 | entryStream = entryStream.pipe(new EntryVerifyStream(entryStream, entry.crc, entry.size));
|
388 | callback(null, entryStream);
|
389 | }, false);
|
390 | };
|
391 |
|
392 | this.entryDataSync = function(entry) {
|
393 | var err = null;
|
394 | this.openEntry(entry, function(e, en) {
|
395 | err = e;
|
396 | entry = en;
|
397 | }, true);
|
398 | if (err)
|
399 | throw err;
|
400 | var
|
401 | data = Buffer.alloc(entry.compressedSize),
|
402 | bytesRead;
|
403 | new FsRead(fd, data, 0, entry.compressedSize, dataOffset(entry), function(e, br) {
|
404 | err = e;
|
405 | bytesRead = br;
|
406 | }).read(true);
|
407 | if (err)
|
408 | throw err;
|
409 | if (entry.method === consts.STORED) {
|
410 | } else if (entry.method === consts.DEFLATED || entry.method === consts.ENHANCED_DEFLATED) {
|
411 | data = zlib.inflateRawSync(data);
|
412 | } else {
|
413 | throw new Error('Unknown compression method: ' + entry.method);
|
414 | }
|
415 | if (data.length !== entry.size)
|
416 | throw new Error('Invalid size');
|
417 | if (canVerifyCrc(entry)) {
|
418 | var verify = new CrcVerify(entry.crc, entry.size);
|
419 | verify.data(data);
|
420 | }
|
421 | return data;
|
422 | };
|
423 |
|
424 | this.openEntry = function(entry, callback, sync) {
|
425 | if (typeof entry === 'string') {
|
426 | checkEntriesExist();
|
427 | entry = entries[entry];
|
428 | if (!entry)
|
429 | return callback('Entry not found');
|
430 | }
|
431 | if (!entry.isFile)
|
432 | return callback('Entry is not file');
|
433 | if (!fd)
|
434 | return callback('Archive closed');
|
435 | var buffer = Buffer.alloc(consts.LOCHDR);
|
436 | new FsRead(fd, buffer, 0, buffer.length, entry.offset, function(err) {
|
437 | if (err)
|
438 | return callback(err);
|
439 | var readEx;
|
440 | try {
|
441 | entry.readDataHeader(buffer);
|
442 | if (entry.encrypted) {
|
443 | readEx = 'Entry encrypted';
|
444 | }
|
445 | } catch (ex) {
|
446 | readEx = ex
|
447 | }
|
448 | callback(readEx, entry);
|
449 | }).read(sync);
|
450 | };
|
451 |
|
452 | function dataOffset(entry) {
|
453 | return entry.offset + consts.LOCHDR + entry.fnameLen + entry.extraLen;
|
454 | }
|
455 |
|
456 | function canVerifyCrc(entry) {
|
457 |
|
458 | return (entry.flags & 0x8) != 0x8;
|
459 | }
|
460 |
|
461 | function extract(entry, outPath, callback) {
|
462 | that.stream(entry, function (err, stm) {
|
463 | if (err) {
|
464 | callback(err);
|
465 | } else {
|
466 | var fsStm, errThrown;
|
467 | stm.on('error', function(err) {
|
468 | errThrown = err;
|
469 | if (fsStm) {
|
470 | stm.unpipe(fsStm);
|
471 | fsStm.close(function () {
|
472 | callback(err);
|
473 | });
|
474 | }
|
475 | });
|
476 | fs.open(outPath, 'w', function(err, fdFile) {
|
477 | if (err)
|
478 | return callback(err || errThrown);
|
479 | if (errThrown) {
|
480 | fs.close(fd, function() {
|
481 | callback(errThrown);
|
482 | });
|
483 | return;
|
484 | }
|
485 | fsStm = fs.createWriteStream(outPath, { fd: fdFile });
|
486 | fsStm.on('finish', function() {
|
487 | that.emit('extract', entry, outPath);
|
488 | if (!errThrown)
|
489 | callback();
|
490 | });
|
491 | stm.pipe(fsStm);
|
492 | });
|
493 | }
|
494 | });
|
495 | }
|
496 |
|
497 | function createDirectories(baseDir, dirs, callback) {
|
498 | if (!dirs.length)
|
499 | return callback();
|
500 | var dir = dirs.shift();
|
501 | dir = path.join(baseDir, path.join.apply(path, dir));
|
502 | fs.mkdir(dir, function(err) {
|
503 | if (err && err.code !== 'EEXIST')
|
504 | return callback(err);
|
505 | createDirectories(baseDir, dirs, callback);
|
506 | });
|
507 | }
|
508 |
|
509 | function extractFiles(baseDir, baseRelPath, files, callback, extractedCount) {
|
510 | if (!files.length)
|
511 | return callback(null, extractedCount);
|
512 | var file = files.shift();
|
513 | var targetPath = path.join(baseDir, file.name.replace(baseRelPath, ''));
|
514 | extract(file, targetPath, function (err) {
|
515 | if (err)
|
516 | return callback(err, extractedCount);
|
517 | extractFiles(baseDir, baseRelPath, files, callback, extractedCount + 1);
|
518 | });
|
519 | }
|
520 |
|
521 | this.extract = function(entry, outPath, callback) {
|
522 | var entryName = entry || '';
|
523 | if (typeof entry === 'string') {
|
524 | entry = this.entry(entry);
|
525 | if (entry) {
|
526 | entryName = entry.name;
|
527 | } else {
|
528 | if (entryName.length && entryName[entryName.length - 1] !== '/')
|
529 | entryName += '/';
|
530 | }
|
531 | }
|
532 | if (!entry || entry.isDirectory) {
|
533 | var files = [], dirs = [], allDirs = {};
|
534 | for (var e in entries) {
|
535 | if (Object.prototype.hasOwnProperty.call(entries, e) && e.lastIndexOf(entryName, 0) === 0) {
|
536 | var relPath = e.replace(entryName, '');
|
537 | var childEntry = entries[e];
|
538 | if (childEntry.isFile) {
|
539 | files.push(childEntry);
|
540 | relPath = path.dirname(relPath);
|
541 | }
|
542 | if (relPath && !allDirs[relPath] && relPath !== '.') {
|
543 | allDirs[relPath] = true;
|
544 | var parts = relPath.split('/').filter(function (f) { return f; });
|
545 | if (parts.length)
|
546 | dirs.push(parts);
|
547 | while (parts.length > 1) {
|
548 | parts = parts.slice(0, parts.length - 1);
|
549 | var partsPath = parts.join('/');
|
550 | if (allDirs[partsPath] || partsPath === '.') {
|
551 | break;
|
552 | }
|
553 | allDirs[partsPath] = true;
|
554 | dirs.push(parts);
|
555 | }
|
556 | }
|
557 | }
|
558 | }
|
559 | dirs.sort(function(x, y) { return x.length - y.length; });
|
560 | if (dirs.length) {
|
561 | createDirectories(outPath, dirs, function (err) {
|
562 | if (err)
|
563 | callback(err);
|
564 | else
|
565 | extractFiles(outPath, entryName, files, callback, 0);
|
566 | });
|
567 | } else {
|
568 | extractFiles(outPath, entryName, files, callback, 0);
|
569 | }
|
570 | } else {
|
571 | fs.stat(outPath, function(err, stat) {
|
572 | if (stat && stat.isDirectory())
|
573 | extract(entry, path.join(outPath, path.basename(entry.name)), callback);
|
574 | else
|
575 | extract(entry, outPath, callback);
|
576 | });
|
577 | }
|
578 | };
|
579 |
|
580 | this.close = function(callback) {
|
581 | if (closed) {
|
582 | if (callback)
|
583 | callback();
|
584 | } else {
|
585 | closed = true;
|
586 | fs.close(fd, function(err) {
|
587 | fd = null;
|
588 | if (callback)
|
589 | callback(err);
|
590 | });
|
591 | }
|
592 | };
|
593 |
|
594 | var originalEmit = events.EventEmitter.prototype.emit;
|
595 | this.emit = function() {
|
596 | if (!closed) {
|
597 | return originalEmit.apply(this, arguments);
|
598 | }
|
599 | };
|
600 | };
|
601 |
|
602 | StreamZip.setFs = function(customFs) {
|
603 | fs = customFs;
|
604 | };
|
605 |
|
606 | util.inherits(StreamZip, events.EventEmitter);
|
607 |
|
608 |
|
609 |
|
610 |
|
611 |
|
612 | var CentralDirectoryHeader = function() {
|
613 | };
|
614 |
|
615 | CentralDirectoryHeader.prototype.read = function(data) {
|
616 | if (data.length != consts.ENDHDR || data.readUInt32LE(0) != consts.ENDSIG)
|
617 | throw new Error('Invalid central directory');
|
618 |
|
619 | this.volumeEntries = data.readUInt16LE(consts.ENDSUB);
|
620 |
|
621 | this.totalEntries = data.readUInt16LE(consts.ENDTOT);
|
622 |
|
623 | this.size = data.readUInt32LE(consts.ENDSIZ);
|
624 |
|
625 | this.offset = data.readUInt32LE(consts.ENDOFF);
|
626 |
|
627 | this.commentLength = data.readUInt16LE(consts.ENDCOM);
|
628 | };
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 | var CentralDirectoryLoc64Header = function() {
|
635 | };
|
636 |
|
637 | CentralDirectoryLoc64Header.prototype.read = function(data) {
|
638 | if (data.length != consts.ENDL64HDR || data.readUInt32LE(0) != consts.ENDL64SIG)
|
639 | throw new Error('Invalid zip64 central directory locator');
|
640 |
|
641 | this.headerOffset = Util.readUInt64LE(data, consts.ENDSUB);
|
642 | };
|
643 |
|
644 |
|
645 |
|
646 |
|
647 |
|
648 | var CentralDirectoryZip64Header = function() {
|
649 | };
|
650 |
|
651 | CentralDirectoryZip64Header.prototype.read = function(data) {
|
652 | if (data.length != consts.END64HDR || data.readUInt32LE(0) != consts.END64SIG)
|
653 | throw new Error('Invalid central directory');
|
654 |
|
655 | this.volumeEntries = Util.readUInt64LE(data, consts.END64SUB);
|
656 |
|
657 | this.totalEntries = Util.readUInt64LE(data, consts.END64TOT);
|
658 |
|
659 | this.size = Util.readUInt64LE(data, consts.END64SIZ);
|
660 |
|
661 | this.offset = Util.readUInt64LE(data, consts.END64OFF);
|
662 | };
|
663 |
|
664 |
|
665 |
|
666 |
|
667 |
|
668 | var ZipEntry = function() {
|
669 | };
|
670 |
|
671 | function toBits(dec, size) {
|
672 | var b = (dec >>> 0).toString(2);
|
673 | while (b.length < size)
|
674 | b = '0' + b;
|
675 | return b.split('');
|
676 | }
|
677 |
|
678 | function parseZipTime(timebytes, datebytes) {
|
679 | var timebits = toBits(timebytes, 16);
|
680 | var datebits = toBits(datebytes, 16);
|
681 |
|
682 | var mt = {
|
683 | h: parseInt(timebits.slice(0,5).join(''), 2),
|
684 | m: parseInt(timebits.slice(5,11).join(''), 2),
|
685 | s: parseInt(timebits.slice(11,16).join(''), 2) * 2,
|
686 | Y: parseInt(datebits.slice(0,7).join(''), 2) + 1980,
|
687 | M: parseInt(datebits.slice(7,11).join(''), 2),
|
688 | D: parseInt(datebits.slice(11,16).join(''), 2),
|
689 | };
|
690 | var dt_str = [mt.Y, mt.M, mt.D].join('-') + ' ' + [mt.h, mt.m, mt.s].join(':') + ' GMT+0';
|
691 | return new Date(dt_str).getTime();
|
692 | }
|
693 |
|
694 | ZipEntry.prototype.readHeader = function(data, offset) {
|
695 |
|
696 | if (data.length < offset + consts.CENHDR || data.readUInt32LE(offset) != consts.CENSIG) {
|
697 | throw new Error('Invalid entry header');
|
698 | }
|
699 |
|
700 | this.verMade = data.readUInt16LE(offset + consts.CENVEM);
|
701 |
|
702 | this.version = data.readUInt16LE(offset + consts.CENVER);
|
703 |
|
704 | this.flags = data.readUInt16LE(offset + consts.CENFLG);
|
705 |
|
706 | this.method = data.readUInt16LE(offset + consts.CENHOW);
|
707 |
|
708 | var timebytes = data.readUInt16LE(offset + consts.CENTIM);
|
709 | var datebytes = data.readUInt16LE(offset + consts.CENTIM + 2);
|
710 | this.time = parseZipTime(timebytes, datebytes);
|
711 |
|
712 |
|
713 | this.crc = data.readUInt32LE(offset + consts.CENCRC);
|
714 |
|
715 | this.compressedSize = data.readUInt32LE(offset + consts.CENSIZ);
|
716 |
|
717 | this.size = data.readUInt32LE(offset + consts.CENLEN);
|
718 |
|
719 | this.fnameLen = data.readUInt16LE(offset + consts.CENNAM);
|
720 |
|
721 | this.extraLen = data.readUInt16LE(offset + consts.CENEXT);
|
722 |
|
723 | this.comLen = data.readUInt16LE(offset + consts.CENCOM);
|
724 |
|
725 | this.diskStart = data.readUInt16LE(offset + consts.CENDSK);
|
726 |
|
727 | this.inattr = data.readUInt16LE(offset + consts.CENATT);
|
728 |
|
729 | this.attr = data.readUInt32LE(offset + consts.CENATX);
|
730 |
|
731 | this.offset = data.readUInt32LE(offset + consts.CENOFF);
|
732 | };
|
733 |
|
734 | ZipEntry.prototype.readDataHeader = function(data) {
|
735 |
|
736 | if (data.readUInt32LE(0) != consts.LOCSIG) {
|
737 | throw new Error('Invalid local header');
|
738 | }
|
739 |
|
740 | this.version = data.readUInt16LE(consts.LOCVER);
|
741 |
|
742 | this.flags = data.readUInt16LE(consts.LOCFLG);
|
743 |
|
744 | this.method = data.readUInt16LE(consts.LOCHOW);
|
745 |
|
746 | var timebytes = data.readUInt16LE(consts.LOCTIM);
|
747 | var datebytes = data.readUInt16LE(consts.LOCTIM + 2);
|
748 | this.time = parseZipTime(timebytes, datebytes);
|
749 |
|
750 |
|
751 | this.crc = data.readUInt32LE(consts.LOCCRC) || this.crc;
|
752 |
|
753 | var compressedSize = data.readUInt32LE(consts.LOCSIZ);
|
754 | if (compressedSize && compressedSize !== consts.EF_ZIP64_OR_32) {
|
755 | this.compressedSize = compressedSize;
|
756 | }
|
757 |
|
758 | var size = data.readUInt32LE(consts.LOCLEN);
|
759 | if (size && size !== consts.EF_ZIP64_OR_32) {
|
760 | this.size = size;
|
761 | }
|
762 |
|
763 | this.fnameLen = data.readUInt16LE(consts.LOCNAM);
|
764 |
|
765 | this.extraLen = data.readUInt16LE(consts.LOCEXT);
|
766 | };
|
767 |
|
768 | ZipEntry.prototype.read = function(data, offset) {
|
769 | this.name = data.slice(offset, offset += this.fnameLen).toString();
|
770 | var lastChar = data[offset - 1];
|
771 | this.isDirectory = (lastChar == 47) || (lastChar == 92);
|
772 |
|
773 | if (this.extraLen) {
|
774 | this.readExtra(data, offset);
|
775 | offset += this.extraLen;
|
776 | }
|
777 | this.comment = this.comLen ? data.slice(offset, offset + this.comLen).toString() : null;
|
778 | };
|
779 |
|
780 | ZipEntry.prototype.validateName = function() {
|
781 | if (/\\|^\w+:|^\/|(^|\/)\.\.(\/|$)/.test(this.name)) {
|
782 | throw new Error('Malicious entry: ' + this.name);
|
783 | }
|
784 | };
|
785 |
|
786 | ZipEntry.prototype.readExtra = function(data, offset) {
|
787 | var signature, size, maxPos = offset + this.extraLen;
|
788 | while (offset < maxPos) {
|
789 | signature = data.readUInt16LE(offset);
|
790 | offset += 2;
|
791 | size = data.readUInt16LE(offset);
|
792 | offset += 2;
|
793 | if (consts.ID_ZIP64 === signature) {
|
794 | this.parseZip64Extra(data, offset, size);
|
795 | }
|
796 | offset += size;
|
797 | }
|
798 | };
|
799 |
|
800 | ZipEntry.prototype.parseZip64Extra = function(data, offset, length) {
|
801 | if (length >= 8 && this.size === consts.EF_ZIP64_OR_32) {
|
802 | this.size = Util.readUInt64LE(data, offset);
|
803 | offset += 8; length -= 8;
|
804 | }
|
805 | if (length >= 8 && this.compressedSize === consts.EF_ZIP64_OR_32) {
|
806 | this.compressedSize = Util.readUInt64LE(data, offset);
|
807 | offset += 8; length -= 8;
|
808 | }
|
809 | if (length >= 8 && this.offset === consts.EF_ZIP64_OR_32) {
|
810 | this.offset = Util.readUInt64LE(data, offset);
|
811 | offset += 8; length -= 8;
|
812 | }
|
813 | if (length >= 4 && this.diskStart === consts.EF_ZIP64_OR_16) {
|
814 | this.diskStart = data.readUInt32LE(offset);
|
815 |
|
816 | }
|
817 | };
|
818 |
|
819 | Object.defineProperty(ZipEntry.prototype, 'encrypted', {
|
820 | get: function() { return (this.flags & consts.FLG_ENTRY_ENC) == consts.FLG_ENTRY_ENC; }
|
821 | });
|
822 |
|
823 | Object.defineProperty(ZipEntry.prototype, 'isFile', {
|
824 | get: function() { return !this.isDirectory; }
|
825 | });
|
826 |
|
827 |
|
828 |
|
829 |
|
830 |
|
831 | var FsRead = function(fd, buffer, offset, length, position, callback) {
|
832 | this.fd = fd;
|
833 | this.buffer = buffer;
|
834 | this.offset = offset;
|
835 | this.length = length;
|
836 | this.position = position;
|
837 | this.callback = callback;
|
838 | this.bytesRead = 0;
|
839 | this.waiting = false;
|
840 | };
|
841 |
|
842 | FsRead.prototype.read = function(sync) {
|
843 | if (StreamZip.debug) {
|
844 | console.log('read', this.position, this.bytesRead, this.length, this.offset);
|
845 | }
|
846 | this.waiting = true;
|
847 | var err;
|
848 | if (sync) {
|
849 | try {
|
850 | var bytesRead = fs.readSync(this.fd, this.buffer, this.offset + this.bytesRead,
|
851 | this.length - this.bytesRead, this.position + this.bytesRead);
|
852 | } catch (e) {
|
853 | err = e;
|
854 | }
|
855 | this.readCallback(sync, err, err ? bytesRead : null);
|
856 | } else {
|
857 | fs.read(this.fd, this.buffer, this.offset + this.bytesRead,
|
858 | this.length - this.bytesRead, this.position + this.bytesRead,
|
859 | this.readCallback.bind(this, sync));
|
860 | }
|
861 | };
|
862 |
|
863 | FsRead.prototype.readCallback = function(sync, err, bytesRead) {
|
864 | if (typeof bytesRead === 'number')
|
865 | this.bytesRead += bytesRead;
|
866 | if (err || !bytesRead || this.bytesRead === this.length) {
|
867 | this.waiting = false;
|
868 | return this.callback(err, this.bytesRead);
|
869 | } else {
|
870 | this.read(sync);
|
871 | }
|
872 | };
|
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 | var FileWindowBuffer = function(fd) {
|
879 | this.position = 0;
|
880 | this.buffer = Buffer.alloc(0);
|
881 |
|
882 | var fsOp = null;
|
883 |
|
884 | this.checkOp = function() {
|
885 | if (fsOp && fsOp.waiting)
|
886 | throw new Error('Operation in progress');
|
887 | };
|
888 |
|
889 | this.read = function(pos, length, callback) {
|
890 | this.checkOp();
|
891 | if (this.buffer.length < length)
|
892 | this.buffer = Buffer.alloc(length);
|
893 | this.position = pos;
|
894 | fsOp = new FsRead(fd, this.buffer, 0, length, this.position, callback).read();
|
895 | };
|
896 |
|
897 | this.expandLeft = function(length, callback) {
|
898 | this.checkOp();
|
899 | this.buffer = Buffer.concat([Buffer.alloc(length), this.buffer]);
|
900 | this.position -= length;
|
901 | if (this.position < 0)
|
902 | this.position = 0;
|
903 | fsOp = new FsRead(fd, this.buffer, 0, length, this.position, callback).read();
|
904 | };
|
905 |
|
906 | this.expandRight = function(length, callback) {
|
907 | this.checkOp();
|
908 | var offset = this.buffer.length;
|
909 | this.buffer = Buffer.concat([this.buffer, Buffer.alloc(length)]);
|
910 | fsOp = new FsRead(fd, this.buffer, offset, length, this.position + offset, callback).read();
|
911 | };
|
912 |
|
913 | this.moveRight = function(length, callback, shift) {
|
914 | this.checkOp();
|
915 | if (shift) {
|
916 | this.buffer.copy(this.buffer, 0, shift);
|
917 | } else {
|
918 | shift = 0;
|
919 | }
|
920 | this.position += shift;
|
921 | fsOp = new FsRead(fd, this.buffer, this.buffer.length - shift, shift, this.position + this.buffer.length - shift, callback).read();
|
922 | };
|
923 | };
|
924 |
|
925 |
|
926 |
|
927 |
|
928 |
|
929 | var EntryDataReaderStream = function(fd, offset, length) {
|
930 | stream.Readable.prototype.constructor.call(this);
|
931 | this.fd = fd;
|
932 | this.offset = offset;
|
933 | this.length = length;
|
934 | this.pos = 0;
|
935 | this.readCallback = this.readCallback.bind(this);
|
936 | };
|
937 |
|
938 | util.inherits(EntryDataReaderStream, stream.Readable);
|
939 |
|
940 | EntryDataReaderStream.prototype._read = function(n) {
|
941 | var buffer = Buffer.alloc(Math.min(n, this.length - this.pos));
|
942 | if (buffer.length) {
|
943 | fs.read(this.fd, buffer, 0, buffer.length, this.offset + this.pos, this.readCallback);
|
944 | } else {
|
945 | this.push(null);
|
946 | }
|
947 | };
|
948 |
|
949 | EntryDataReaderStream.prototype.readCallback = function(err, bytesRead, buffer) {
|
950 | this.pos += bytesRead;
|
951 | if (err) {
|
952 | this.emit('error', err);
|
953 | this.push(null);
|
954 | } else if (!bytesRead) {
|
955 | this.push(null);
|
956 | } else {
|
957 | if (bytesRead !== buffer.length)
|
958 | buffer = buffer.slice(0, bytesRead);
|
959 | this.push(buffer);
|
960 | }
|
961 | };
|
962 |
|
963 |
|
964 |
|
965 |
|
966 |
|
967 | var EntryVerifyStream = function(baseStm, crc, size) {
|
968 | stream.Transform.prototype.constructor.call(this);
|
969 | this.verify = new CrcVerify(crc, size);
|
970 | var that = this;
|
971 | baseStm.on('error', function(e) {
|
972 | that.emit('error', e);
|
973 | });
|
974 | };
|
975 |
|
976 | util.inherits(EntryVerifyStream, stream.Transform);
|
977 |
|
978 | EntryVerifyStream.prototype._transform = function(data, encoding, callback) {
|
979 | var err;
|
980 | try {
|
981 | this.verify.data(data);
|
982 | } catch (e) {
|
983 | err = e;
|
984 | }
|
985 | callback(err, data);
|
986 | };
|
987 |
|
988 |
|
989 |
|
990 |
|
991 |
|
992 | var CrcVerify = function(crc, size) {
|
993 | this.crc = crc;
|
994 | this.size = size;
|
995 | this.state = {
|
996 | crc: ~0,
|
997 | size: 0
|
998 | };
|
999 | };
|
1000 |
|
1001 | CrcVerify.prototype.data = function(data) {
|
1002 | var crcTable = CrcVerify.getCrcTable();
|
1003 | var crc = this.state.crc, off = 0, len = data.length;
|
1004 | while (--len >= 0)
|
1005 | crc = crcTable[(crc ^ data[off++]) & 0xff] ^ (crc >>> 8);
|
1006 | this.state.crc = crc;
|
1007 | this.state.size += data.length;
|
1008 | if (this.state.size >= this.size) {
|
1009 | var buf = Buffer.alloc(4);
|
1010 | buf.writeInt32LE(~this.state.crc & 0xffffffff, 0);
|
1011 | crc = buf.readUInt32LE(0);
|
1012 | if (crc !== this.crc)
|
1013 | throw new Error('Invalid CRC');
|
1014 | if (this.state.size !== this.size)
|
1015 | throw new Error('Invalid size');
|
1016 | }
|
1017 | };
|
1018 |
|
1019 | CrcVerify.getCrcTable = function() {
|
1020 | var crcTable = CrcVerify.crcTable;
|
1021 | if (!crcTable) {
|
1022 | CrcVerify.crcTable = crcTable = [];
|
1023 | var b = Buffer.alloc(4);
|
1024 | for (var n = 0; n < 256; n++) {
|
1025 | var c = n;
|
1026 | for (var k = 8; --k >= 0; )
|
1027 | if ((c & 1) != 0) { c = 0xedb88320 ^ (c >>> 1); } else { c = c >>> 1; }
|
1028 | if (c < 0) {
|
1029 | b.writeInt32LE(c, 0);
|
1030 | c = b.readUInt32LE(0);
|
1031 | }
|
1032 | crcTable[n] = c;
|
1033 | }
|
1034 | }
|
1035 | return crcTable;
|
1036 | };
|
1037 |
|
1038 |
|
1039 |
|
1040 |
|
1041 |
|
1042 | var Util = {
|
1043 | readUInt64LE: function(buffer, offset) {
|
1044 | return (buffer.readUInt32LE(offset + 4) * 0x0000000100000000) + buffer.readUInt32LE(offset);
|
1045 | }
|
1046 | };
|
1047 |
|
1048 |
|
1049 |
|
1050 |
|
1051 |
|
1052 | module.exports = StreamZip;
|
1053 |
|
1054 |
|