node-stream-zip 1.4.2 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/node_stream_zip.js +50 -17
- package/package.json +1 -1
package/node_stream_zip.js
CHANGED
|
@@ -371,7 +371,7 @@ var StreamZip = function(config) {
|
|
|
371
371
|
};
|
|
372
372
|
|
|
373
373
|
this.stream = function(entry, callback) {
|
|
374
|
-
return openEntry(entry, function(err, entry) {
|
|
374
|
+
return this.openEntry(entry, function(err, entry) {
|
|
375
375
|
if (err)
|
|
376
376
|
return callback(err);
|
|
377
377
|
var offset = dataOffset(entry);
|
|
@@ -390,14 +390,14 @@ var StreamZip = function(config) {
|
|
|
390
390
|
|
|
391
391
|
this.entryDataSync = function(entry) {
|
|
392
392
|
var err = null;
|
|
393
|
-
openEntry(entry, function(e, en) {
|
|
393
|
+
this.openEntry(entry, function(e, en) {
|
|
394
394
|
err = e;
|
|
395
395
|
entry = en;
|
|
396
396
|
}, true);
|
|
397
397
|
if (err)
|
|
398
398
|
throw err;
|
|
399
399
|
var
|
|
400
|
-
data =
|
|
400
|
+
data = Buffer.alloc(entry.compressedSize),
|
|
401
401
|
bytesRead;
|
|
402
402
|
new FsRead(fd, data, 0, entry.compressedSize, dataOffset(entry), function(e, br) {
|
|
403
403
|
err = e;
|
|
@@ -420,7 +420,7 @@ var StreamZip = function(config) {
|
|
|
420
420
|
return data;
|
|
421
421
|
};
|
|
422
422
|
|
|
423
|
-
function
|
|
423
|
+
this.openEntry = function(entry, callback, sync) {
|
|
424
424
|
if (typeof entry === 'string') {
|
|
425
425
|
checkEntriesExist();
|
|
426
426
|
entry = entries[entry];
|
|
@@ -431,7 +431,7 @@ var StreamZip = function(config) {
|
|
|
431
431
|
return callback('Entry is not file');
|
|
432
432
|
if (!fd)
|
|
433
433
|
return callback('Archive closed');
|
|
434
|
-
var buffer =
|
|
434
|
+
var buffer = Buffer.alloc(consts.LOCHDR);
|
|
435
435
|
new FsRead(fd, buffer, 0, buffer.length, entry.offset, function(err) {
|
|
436
436
|
if (err)
|
|
437
437
|
return callback(err);
|
|
@@ -446,7 +446,7 @@ var StreamZip = function(config) {
|
|
|
446
446
|
}
|
|
447
447
|
callback(readEx, entry);
|
|
448
448
|
}).read(sync);
|
|
449
|
-
}
|
|
449
|
+
};
|
|
450
450
|
|
|
451
451
|
function dataOffset(entry) {
|
|
452
452
|
return entry.offset + consts.LOCHDR + entry.fnameLen + entry.extraLen;
|
|
@@ -576,11 +576,15 @@ var StreamZip = function(config) {
|
|
|
576
576
|
}
|
|
577
577
|
};
|
|
578
578
|
|
|
579
|
-
this.close = function() {
|
|
579
|
+
this.close = function(callback) {
|
|
580
580
|
if (fd) {
|
|
581
|
-
fs.close(fd, function() {
|
|
581
|
+
fs.close(fd, function(err) {
|
|
582
582
|
fd = null;
|
|
583
|
+
if (callback)
|
|
584
|
+
callback(err);
|
|
583
585
|
});
|
|
586
|
+
} else if (callback) {
|
|
587
|
+
callback();
|
|
584
588
|
}
|
|
585
589
|
};
|
|
586
590
|
};
|
|
@@ -654,6 +658,29 @@ CentralDirectoryZip64Header.prototype.read = function(data) {
|
|
|
654
658
|
var ZipEntry = function() {
|
|
655
659
|
};
|
|
656
660
|
|
|
661
|
+
function toBits(dec, size) {
|
|
662
|
+
var b = (dec >>> 0).toString(2);
|
|
663
|
+
while (b.length < size)
|
|
664
|
+
b = '0' + b;
|
|
665
|
+
return b.split('');
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function parseZipTime(timebytes, datebytes) {
|
|
669
|
+
var timebits = toBits(timebytes, 16);
|
|
670
|
+
var datebits = toBits(datebytes, 16);
|
|
671
|
+
|
|
672
|
+
var mt = {
|
|
673
|
+
h: parseInt(timebits.slice(0,5).join(''), 2),
|
|
674
|
+
m: parseInt(timebits.slice(5,11).join(''), 2),
|
|
675
|
+
s: parseInt(timebits.slice(11,16).join(''), 2) * 2,
|
|
676
|
+
Y: parseInt(datebits.slice(0,7).join(''), 2) + 1980,
|
|
677
|
+
M: parseInt(datebits.slice(7,11).join(''), 2),
|
|
678
|
+
D: parseInt(datebits.slice(11,16).join(''), 2),
|
|
679
|
+
};
|
|
680
|
+
var dt_str = [mt.Y, mt.M, mt.D].join('-') + ' ' + [mt.h, mt.m, mt.s].join(':') + ' GMT+0';
|
|
681
|
+
return new Date(dt_str).getTime();
|
|
682
|
+
}
|
|
683
|
+
|
|
657
684
|
ZipEntry.prototype.readHeader = function(data, offset) {
|
|
658
685
|
// data should be 46 bytes and start with "PK 01 02"
|
|
659
686
|
if (data.length < offset + consts.CENHDR || data.readUInt32LE(offset) != consts.CENSIG) {
|
|
@@ -668,7 +695,10 @@ ZipEntry.prototype.readHeader = function(data, offset) {
|
|
|
668
695
|
// compression method
|
|
669
696
|
this.method = data.readUInt16LE(offset + consts.CENHOW);
|
|
670
697
|
// modification time (2 bytes time, 2 bytes date)
|
|
671
|
-
|
|
698
|
+
var timebytes = data.readUInt16LE(offset + consts.CENTIM);
|
|
699
|
+
var datebytes = data.readUInt16LE(offset + consts.CENTIM + 2);
|
|
700
|
+
this.time = parseZipTime(timebytes, datebytes);
|
|
701
|
+
|
|
672
702
|
// uncompressed file crc-32 value
|
|
673
703
|
this.crc = data.readUInt32LE(offset + consts.CENCRC);
|
|
674
704
|
// compressed size
|
|
@@ -703,7 +733,10 @@ ZipEntry.prototype.readDataHeader = function(data) {
|
|
|
703
733
|
// compression method
|
|
704
734
|
this.method = data.readUInt16LE(consts.LOCHOW);
|
|
705
735
|
// modification time (2 bytes time ; 2 bytes date)
|
|
706
|
-
|
|
736
|
+
var timebytes = data.readUInt16LE(consts.LOCTIM);
|
|
737
|
+
var datebytes = data.readUInt16LE(consts.LOCTIM + 2);
|
|
738
|
+
this.time = parseZipTime(timebytes, datebytes);
|
|
739
|
+
|
|
707
740
|
// uncompressed file crc-32 value
|
|
708
741
|
this.crc = data.readUInt32LE(consts.LOCCRC) || this.crc;
|
|
709
742
|
// compressed size
|
|
@@ -834,7 +867,7 @@ FsRead.prototype.readCallback = function(sync, err, bytesRead) {
|
|
|
834
867
|
|
|
835
868
|
var FileWindowBuffer = function(fd) {
|
|
836
869
|
this.position = 0;
|
|
837
|
-
this.buffer =
|
|
870
|
+
this.buffer = Buffer.alloc(0);
|
|
838
871
|
|
|
839
872
|
var fsOp = null;
|
|
840
873
|
|
|
@@ -846,14 +879,14 @@ var FileWindowBuffer = function(fd) {
|
|
|
846
879
|
this.read = function(pos, length, callback) {
|
|
847
880
|
this.checkOp();
|
|
848
881
|
if (this.buffer.length < length)
|
|
849
|
-
this.buffer =
|
|
882
|
+
this.buffer = Buffer.alloc(length);
|
|
850
883
|
this.position = pos;
|
|
851
884
|
fsOp = new FsRead(fd, this.buffer, 0, length, this.position, callback).read();
|
|
852
885
|
};
|
|
853
886
|
|
|
854
887
|
this.expandLeft = function(length, callback) {
|
|
855
888
|
this.checkOp();
|
|
856
|
-
this.buffer = Buffer.concat([
|
|
889
|
+
this.buffer = Buffer.concat([Buffer.alloc(length), this.buffer]);
|
|
857
890
|
this.position -= length;
|
|
858
891
|
if (this.position < 0)
|
|
859
892
|
this.position = 0;
|
|
@@ -863,7 +896,7 @@ var FileWindowBuffer = function(fd) {
|
|
|
863
896
|
this.expandRight = function(length, callback) {
|
|
864
897
|
this.checkOp();
|
|
865
898
|
var offset = this.buffer.length;
|
|
866
|
-
this.buffer = Buffer.concat([this.buffer,
|
|
899
|
+
this.buffer = Buffer.concat([this.buffer, Buffer.alloc(length)]);
|
|
867
900
|
fsOp = new FsRead(fd, this.buffer, offset, length, this.position + offset, callback).read();
|
|
868
901
|
};
|
|
869
902
|
|
|
@@ -895,7 +928,7 @@ var EntryDataReaderStream = function(fd, offset, length) {
|
|
|
895
928
|
util.inherits(EntryDataReaderStream, stream.Readable);
|
|
896
929
|
|
|
897
930
|
EntryDataReaderStream.prototype._read = function(n) {
|
|
898
|
-
var buffer =
|
|
931
|
+
var buffer = Buffer.alloc(Math.min(n, this.length - this.pos));
|
|
899
932
|
if (buffer.length) {
|
|
900
933
|
fs.read(this.fd, buffer, 0, buffer.length, this.offset + this.pos, this.readCallback);
|
|
901
934
|
} else {
|
|
@@ -963,7 +996,7 @@ CrcVerify.prototype.data = function(data) {
|
|
|
963
996
|
this.state.crc = crc;
|
|
964
997
|
this.state.size += data.length;
|
|
965
998
|
if (this.state.size >= this.size) {
|
|
966
|
-
var buf =
|
|
999
|
+
var buf = Buffer.alloc(4);
|
|
967
1000
|
buf.writeInt32LE(~this.state.crc & 0xffffffff, 0);
|
|
968
1001
|
crc = buf.readUInt32LE(0);
|
|
969
1002
|
if (crc !== this.crc)
|
|
@@ -977,7 +1010,7 @@ CrcVerify.getCrcTable = function() {
|
|
|
977
1010
|
var crcTable = CrcVerify.crcTable;
|
|
978
1011
|
if (!crcTable) {
|
|
979
1012
|
CrcVerify.crcTable = crcTable = [];
|
|
980
|
-
var b =
|
|
1013
|
+
var b = Buffer.alloc(4);
|
|
981
1014
|
for (var n = 0; n < 256; n++) {
|
|
982
1015
|
var c = n;
|
|
983
1016
|
for (var k = 8; --k >= 0; )
|