-
Notifications
You must be signed in to change notification settings - Fork 28
/
zip-archive-output-stream.js
371 lines (355 loc) · 10.4 KB
/
zip-archive-output-stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import { inherits } from "util";
import crc32 from "crc-32";
import { CRC32Stream, DeflateCRC32Stream } from "crc32-stream";
import ArchiveOutputStream from "../archive-output-stream.js";
import ZipArchiveEntry from "./zip-archive-entry.js";
import GeneralPurposeBit from "./general-purpose-bit.js";
import {
LONG_ZERO,
METHOD_DEFLATED,
METHOD_STORED,
MIN_VERSION_DATA_DESCRIPTOR,
MIN_VERSION_ZIP64,
SHORT_ZERO,
SIG_EOCD,
SIG_DD,
SIG_CFH,
SIG_LFH,
SIG_ZIP64_EOCD,
SIG_ZIP64_EOCD_LOC,
VERSION_MADEBY,
ZIP64_EXTRA_ID,
ZIP64_MAGIC,
ZIP64_MAGIC_SHORT,
ZLIB_BEST_SPEED,
} from "./constants.js";
import { getEightBytes, getLongBytes, getShortBytes } from "./util.js";
function _defaults(o) {
if (typeof o !== "object") {
o = {};
}
if (typeof o.zlib !== "object") {
o.zlib = {};
}
if (typeof o.zlib.level !== "number") {
o.zlib.level = ZLIB_BEST_SPEED;
}
o.forceZip64 = !!o.forceZip64;
o.forceLocalTime = !!o.forceLocalTime;
return o;
}
export default class ZipArchiveOutputStream extends ArchiveOutputStream {
constructor(options) {
const _options = _defaults(options);
super(_options);
this.options = _options;
this._entry = null;
this._entries = [];
this._archive = {
centralLength: 0,
centralOffset: 0,
comment: "",
finish: false,
finished: false,
processing: false,
forceZip64: _options.forceZip64,
forceLocalTime: _options.forceLocalTime,
};
}
_afterAppend(ae) {
this._entries.push(ae);
if (ae.getGeneralPurposeBit().usesDataDescriptor()) {
this._writeDataDescriptor(ae);
}
this._archive.processing = false;
this._entry = null;
if (this._archive.finish && !this._archive.finished) {
this._finish();
}
}
_appendBuffer(ae, source, callback) {
if (source.length === 0) {
ae.setMethod(METHOD_STORED);
}
var method = ae.getMethod();
if (method === METHOD_STORED) {
ae.setSize(source.length);
ae.setCompressedSize(source.length);
ae.setCrc(crc32.buf(source) >>> 0);
}
this._writeLocalFileHeader(ae);
if (method === METHOD_STORED) {
this.write(source);
this._afterAppend(ae);
callback(null, ae);
return;
} else if (method === METHOD_DEFLATED) {
this._smartStream(ae, callback).end(source);
return;
} else {
callback(new Error("compression method " + method + " not implemented"));
return;
}
}
_appendStream(ae, source, callback) {
ae.getGeneralPurposeBit().useDataDescriptor(true);
ae.setVersionNeededToExtract(MIN_VERSION_DATA_DESCRIPTOR);
this._writeLocalFileHeader(ae);
var smart = this._smartStream(ae, callback);
source.once("error", function (err) {
smart.emit("error", err);
smart.end();
});
source.pipe(smart);
}
_finish() {
this._archive.centralOffset = this.offset;
this._entries.forEach(
function (ae) {
this._writeCentralFileHeader(ae);
}.bind(this),
);
this._archive.centralLength = this.offset - this._archive.centralOffset;
if (this.isZip64()) {
this._writeCentralDirectoryZip64();
}
this._writeCentralDirectoryEnd();
this._archive.processing = false;
this._archive.finish = true;
this._archive.finished = true;
this.end();
}
_normalizeEntry(ae) {
if (ae.getMethod() === -1) {
ae.setMethod(METHOD_DEFLATED);
}
if (ae.getMethod() === METHOD_DEFLATED) {
ae.getGeneralPurposeBit().useDataDescriptor(true);
ae.setVersionNeededToExtract(MIN_VERSION_DATA_DESCRIPTOR);
}
if (ae.getTime() === -1) {
ae.setTime(new Date(), this._archive.forceLocalTime);
}
ae._offsets = {
file: 0,
data: 0,
contents: 0,
};
}
_smartStream(ae, callback) {
var deflate = ae.getMethod() === METHOD_DEFLATED;
var process = deflate
? new DeflateCRC32Stream(this.options.zlib)
: new CRC32Stream();
var error = null;
function handleStuff() {
var digest = process.digest().readUInt32BE(0);
ae.setCrc(digest);
ae.setSize(process.size());
ae.setCompressedSize(process.size(true));
this._afterAppend(ae);
callback(error, ae);
}
process.once("end", handleStuff.bind(this));
process.once("error", function (err) {
error = err;
});
process.pipe(this, { end: false });
return process;
}
_writeCentralDirectoryEnd() {
var records = this._entries.length;
var size = this._archive.centralLength;
var offset = this._archive.centralOffset;
if (this.isZip64()) {
records = ZIP64_MAGIC_SHORT;
size = ZIP64_MAGIC;
offset = ZIP64_MAGIC;
}
// signature
this.write(getLongBytes(SIG_EOCD));
// disk numbers
this.write(SHORT_ZERO);
this.write(SHORT_ZERO);
// number of entries
this.write(getShortBytes(records));
this.write(getShortBytes(records));
// length and location of CD
this.write(getLongBytes(size));
this.write(getLongBytes(offset));
// archive comment
var comment = this.getComment();
var commentLength = Buffer.byteLength(comment);
this.write(getShortBytes(commentLength));
this.write(comment);
}
_writeCentralDirectoryZip64() {
// signature
this.write(getLongBytes(SIG_ZIP64_EOCD));
// size of the ZIP64 EOCD record
this.write(getEightBytes(44));
// version made by
this.write(getShortBytes(MIN_VERSION_ZIP64));
// version to extract
this.write(getShortBytes(MIN_VERSION_ZIP64));
// disk numbers
this.write(LONG_ZERO);
this.write(LONG_ZERO);
// number of entries
this.write(getEightBytes(this._entries.length));
this.write(getEightBytes(this._entries.length));
// length and location of CD
this.write(getEightBytes(this._archive.centralLength));
this.write(getEightBytes(this._archive.centralOffset));
// extensible data sector
// not implemented at this time
// end of central directory locator
this.write(getLongBytes(SIG_ZIP64_EOCD_LOC));
// disk number holding the ZIP64 EOCD record
this.write(LONG_ZERO);
// relative offset of the ZIP64 EOCD record
this.write(
getEightBytes(this._archive.centralOffset + this._archive.centralLength),
);
// total number of disks
this.write(getLongBytes(1));
}
_writeCentralFileHeader(ae) {
var gpb = ae.getGeneralPurposeBit();
var method = ae.getMethod();
var fileOffset = ae._offsets.file;
var size = ae.getSize();
var compressedSize = ae.getCompressedSize();
if (ae.isZip64() || fileOffset > ZIP64_MAGIC) {
size = ZIP64_MAGIC;
compressedSize = ZIP64_MAGIC;
fileOffset = ZIP64_MAGIC;
ae.setVersionNeededToExtract(MIN_VERSION_ZIP64);
var extraBuf = Buffer.concat(
[
getShortBytes(ZIP64_EXTRA_ID),
getShortBytes(24),
getEightBytes(ae.getSize()),
getEightBytes(ae.getCompressedSize()),
getEightBytes(ae._offsets.file),
],
28,
);
ae.setExtra(extraBuf);
}
// signature
this.write(getLongBytes(SIG_CFH));
// version made by
this.write(getShortBytes((ae.getPlatform() << 8) | VERSION_MADEBY));
// version to extract and general bit flag
this.write(getShortBytes(ae.getVersionNeededToExtract()));
this.write(gpb.encode());
// compression method
this.write(getShortBytes(method));
// datetime
this.write(getLongBytes(ae.getTimeDos()));
// crc32 checksum
this.write(getLongBytes(ae.getCrc()));
// sizes
this.write(getLongBytes(compressedSize));
this.write(getLongBytes(size));
var name = ae.getName();
var comment = ae.getComment();
var extra = ae.getCentralDirectoryExtra();
if (gpb.usesUTF8ForNames()) {
name = Buffer.from(name);
comment = Buffer.from(comment);
}
// name length
this.write(getShortBytes(name.length));
// extra length
this.write(getShortBytes(extra.length));
// comments length
this.write(getShortBytes(comment.length));
// disk number start
this.write(SHORT_ZERO);
// internal attributes
this.write(getShortBytes(ae.getInternalAttributes()));
// external attributes
this.write(getLongBytes(ae.getExternalAttributes()));
// relative offset of LFH
this.write(getLongBytes(fileOffset));
// name
this.write(name);
// extra
this.write(extra);
// comment
this.write(comment);
}
_writeDataDescriptor(ae) {
// signature
this.write(getLongBytes(SIG_DD));
// crc32 checksum
this.write(getLongBytes(ae.getCrc()));
// sizes
if (ae.isZip64()) {
this.write(getEightBytes(ae.getCompressedSize()));
this.write(getEightBytes(ae.getSize()));
} else {
this.write(getLongBytes(ae.getCompressedSize()));
this.write(getLongBytes(ae.getSize()));
}
}
_writeLocalFileHeader(ae) {
var gpb = ae.getGeneralPurposeBit();
var method = ae.getMethod();
var name = ae.getName();
var extra = ae.getLocalFileDataExtra();
if (ae.isZip64()) {
gpb.useDataDescriptor(true);
ae.setVersionNeededToExtract(MIN_VERSION_ZIP64);
}
if (gpb.usesUTF8ForNames()) {
name = Buffer.from(name);
}
ae._offsets.file = this.offset;
// signature
this.write(getLongBytes(SIG_LFH));
// version to extract and general bit flag
this.write(getShortBytes(ae.getVersionNeededToExtract()));
this.write(gpb.encode());
// compression method
this.write(getShortBytes(method));
// datetime
this.write(getLongBytes(ae.getTimeDos()));
ae._offsets.data = this.offset;
// crc32 checksum and sizes
if (gpb.usesDataDescriptor()) {
this.write(LONG_ZERO);
this.write(LONG_ZERO);
this.write(LONG_ZERO);
} else {
this.write(getLongBytes(ae.getCrc()));
this.write(getLongBytes(ae.getCompressedSize()));
this.write(getLongBytes(ae.getSize()));
}
// name length
this.write(getShortBytes(name.length));
// extra length
this.write(getShortBytes(extra.length));
// name
this.write(name);
// extra
this.write(extra);
ae._offsets.contents = this.offset;
}
getComment(comment) {
return this._archive.comment !== null ? this._archive.comment : "";
}
isZip64() {
return (
this._archive.forceZip64 ||
this._entries.length > ZIP64_MAGIC_SHORT ||
this._archive.centralLength > ZIP64_MAGIC ||
this._archive.centralOffset > ZIP64_MAGIC
);
}
setComment(comment) {
this._archive.comment = comment;
}
}