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