zip.js
15.7 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
goog.provide('Zlib.Zip');
goog.require('USE_TYPEDARRAY');
goog.require('Zlib.RawDeflate');
goog.require('Zlib.CRC32');
goog.scope(function() {
/**
* @param {Object=} opt_params options.
* @constructor
*/
Zlib.Zip = function(opt_params) {
opt_params = opt_params || {};
/** @type {Array.<{
* buffer: !(Array.<number>|Uint8Array),
* option: Object,
* compressed: boolean,
* encrypted: boolean,
* size: number,
* crc32: number
* }>} */
this.files = [];
/** @type {(Array.<number>|Uint8Array)} */
this.comment = opt_params['comment'];
/** @type {(Array.<number>|Uint8Array)} */
this.password;
};
/**
* @enum {number}
*/
Zlib.Zip.CompressionMethod = {
STORE: 0,
DEFLATE: 8
};
/**
* @enum {number}
*/
Zlib.Zip.OperatingSystem = {
MSDOS: 0,
UNIX: 3,
MACINTOSH: 7
};
/**
* @enum {number}
*/
Zlib.Zip.Flags = {
ENCRYPT: 0x0001,
DESCRIPTOR: 0x0008,
UTF8: 0x0800
};
/**
* @type {Array.<number>}
* @const
*/
Zlib.Zip.FileHeaderSignature = [0x50, 0x4b, 0x01, 0x02];
/**
* @type {Array.<number>}
* @const
*/
Zlib.Zip.LocalFileHeaderSignature = [0x50, 0x4b, 0x03, 0x04];
/**
* @type {Array.<number>}
* @const
*/
Zlib.Zip.CentralDirectorySignature = [0x50, 0x4b, 0x05, 0x06];
/**
* @param {Array.<number>|Uint8Array} input
* @param {Object=} opt_params options.
*/
Zlib.Zip.prototype.addFile = function(input, opt_params) {
opt_params = opt_params || {};
/** @type {string} */
var filename = '' || opt_params['filename'];
/** @type {boolean} */
var compressed;
/** @type {number} */
var size = input.length;
/** @type {number} */
var crc32 = 0;
if (USE_TYPEDARRAY && input instanceof Array) {
input = new Uint8Array(input);
}
// default
if (typeof opt_params['compressionMethod'] !== 'number') {
opt_params['compressionMethod'] = Zlib.Zip.CompressionMethod.DEFLATE;
}
// その場で圧縮する場合
if (opt_params['compress']) {
switch (opt_params['compressionMethod']) {
case Zlib.Zip.CompressionMethod.STORE:
break;
case Zlib.Zip.CompressionMethod.DEFLATE:
crc32 = Zlib.CRC32.calc(input);
input = this.deflateWithOption(input, opt_params);
compressed = true;
break;
default:
throw new Error('unknown compression method:' + opt_params['compressionMethod']);
}
}
this.files.push({
buffer: input,
option: opt_params,
compressed: compressed,
encrypted: false,
size: size,
crc32: crc32
});
};
/**
* @param {(Array.<number>|Uint8Array)} password
*/
Zlib.Zip.prototype.setPassword = function(password) {
this.password = password;
};
Zlib.Zip.prototype.compress = function() {
/** @type {Array.<{
* buffer: !(Array.<number>|Uint8Array),
* option: Object,
* compressed: boolean,
* encrypted: boolean,
* size: number,
* crc32: number
* }>} */
var files = this.files;
/** @type {{
* buffer: !(Array.<number>|Uint8Array),
* option: Object,
* compressed: boolean,
* encrypted: boolean,
* size: number,
* crc32: number
* }} */
var file;
/** @type {!(Array.<number>|Uint8Array)} */
var output;
/** @type {number} */
var op1;
/** @type {number} */
var op2;
/** @type {number} */
var op3;
/** @type {number} */
var localFileSize = 0;
/** @type {number} */
var centralDirectorySize = 0;
/** @type {number} */
var endOfCentralDirectorySize;
/** @type {number} */
var offset;
/** @type {number} */
var needVersion;
/** @type {number} */
var flags;
/** @type {Zlib.Zip.CompressionMethod} */
var compressionMethod;
/** @type {Date} */
var date;
/** @type {number} */
var crc32;
/** @type {number} */
var size;
/** @type {number} */
var plainSize;
/** @type {number} */
var filenameLength;
/** @type {number} */
var extraFieldLength;
/** @type {number} */
var commentLength;
/** @type {(Array.<number>|Uint8Array)} */
var filename;
/** @type {(Array.<number>|Uint8Array)} */
var extraField;
/** @type {(Array.<number>|Uint8Array)} */
var comment;
/** @type {(Array.<number>|Uint8Array)} */
var buffer;
/** @type {*} */
var tmp;
/** @type {Array.<number>|Uint32Array|Object} */
var key;
/** @type {number} */
var i;
/** @type {number} */
var il;
/** @type {number} */
var j;
/** @type {number} */
var jl;
// ファイルの圧縮
for (i = 0, il = files.length; i < il; ++i) {
file = files[i];
filenameLength =
(file.option['filename']) ? file.option['filename'].length : 0;
extraFieldLength =
(file.option['extraField']) ? file.option['extraField'].length : 0;
commentLength =
(file.option['comment']) ? file.option['comment'].length : 0;
// 圧縮されていなかったら圧縮
if (!file.compressed) {
// 圧縮前に CRC32 の計算をしておく
file.crc32 = Zlib.CRC32.calc(file.buffer);
switch (file.option['compressionMethod']) {
case Zlib.Zip.CompressionMethod.STORE:
break;
case Zlib.Zip.CompressionMethod.DEFLATE:
file.buffer = this.deflateWithOption(file.buffer, file.option);
file.compressed = true;
break;
default:
throw new Error('unknown compression method:' + file.option['compressionMethod']);
}
}
// encryption
if (file.option['password'] !== void 0|| this.password !== void 0) {
// init encryption
key = this.createEncryptionKey(file.option['password'] || this.password);
// add header
buffer = file.buffer;
if (USE_TYPEDARRAY) {
tmp = new Uint8Array(buffer.length + 12);
tmp.set(buffer, 12);
buffer = tmp;
} else {
buffer.unshift(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
for (j = 0; j < 12; ++j) {
buffer[j] = this.encode(
key,
i === 11 ? (file.crc32 & 0xff) : (Math.random() * 256 | 0)
);
}
// data encryption
for (jl = buffer.length; j < jl; ++j) {
buffer[j] = this.encode(key, buffer[j]);
}
file.buffer = buffer;
}
// 必要バッファサイズの計算
localFileSize +=
// local file header
30 + filenameLength +
// file data
file.buffer.length;
centralDirectorySize +=
// file header
46 + filenameLength + commentLength;
}
// end of central directory
endOfCentralDirectorySize = 46 + (this.comment ? this.comment.length : 0);
output = new (USE_TYPEDARRAY ? Uint8Array : Array)(
localFileSize + centralDirectorySize + endOfCentralDirectorySize
);
op1 = 0;
op2 = localFileSize;
op3 = op2 + centralDirectorySize;
// ファイルの圧縮
for (i = 0, il = files.length; i < il; ++i) {
file = files[i];
filenameLength =
file.option['filename'] ? file.option['filename'].length : 0;
extraFieldLength = 0; // TODO
commentLength =
file.option['comment'] ? file.option['comment'].length : 0;
//-------------------------------------------------------------------------
// local file header & file header
//-------------------------------------------------------------------------
offset = op1;
// signature
// local file header
output[op1++] = Zlib.Zip.LocalFileHeaderSignature[0];
output[op1++] = Zlib.Zip.LocalFileHeaderSignature[1];
output[op1++] = Zlib.Zip.LocalFileHeaderSignature[2];
output[op1++] = Zlib.Zip.LocalFileHeaderSignature[3];
// file header
output[op2++] = Zlib.Zip.FileHeaderSignature[0];
output[op2++] = Zlib.Zip.FileHeaderSignature[1];
output[op2++] = Zlib.Zip.FileHeaderSignature[2];
output[op2++] = Zlib.Zip.FileHeaderSignature[3];
// compressor info
needVersion = 20;
output[op2++] = needVersion & 0xff;
output[op2++] =
/** @type {Zlib.Zip.OperatingSystem} */
(file.option['os']) ||
Zlib.Zip.OperatingSystem.MSDOS;
// need version
output[op1++] = output[op2++] = needVersion & 0xff;
output[op1++] = output[op2++] = (needVersion >> 8) & 0xff;
// general purpose bit flag
flags = 0;
if (file.option['password'] || this.password) {
flags |= Zlib.Zip.Flags.ENCRYPT;
}
output[op1++] = output[op2++] = flags & 0xff;
output[op1++] = output[op2++] = (flags >> 8) & 0xff;
// compression method
compressionMethod =
/** @type {Zlib.Zip.CompressionMethod} */
(file.option['compressionMethod']);
output[op1++] = output[op2++] = compressionMethod & 0xff;
output[op1++] = output[op2++] = (compressionMethod >> 8) & 0xff;
// date
date = /** @type {(Date|undefined)} */(file.option['date']) || new Date();
output[op1++] = output[op2++] =
((date.getMinutes() & 0x7) << 5) |
(date.getSeconds() / 2 | 0);
output[op1++] = output[op2++] =
(date.getHours() << 3) |
(date.getMinutes() >> 3);
//
output[op1++] = output[op2++] =
((date.getMonth() + 1 & 0x7) << 5) |
(date.getDate());
output[op1++] = output[op2++] =
((date.getFullYear() - 1980 & 0x7f) << 1) |
(date.getMonth() + 1 >> 3);
// CRC-32
crc32 = file.crc32;
output[op1++] = output[op2++] = crc32 & 0xff;
output[op1++] = output[op2++] = (crc32 >> 8) & 0xff;
output[op1++] = output[op2++] = (crc32 >> 16) & 0xff;
output[op1++] = output[op2++] = (crc32 >> 24) & 0xff;
// compressed size
size = file.buffer.length;
output[op1++] = output[op2++] = size & 0xff;
output[op1++] = output[op2++] = (size >> 8) & 0xff;
output[op1++] = output[op2++] = (size >> 16) & 0xff;
output[op1++] = output[op2++] = (size >> 24) & 0xff;
// uncompressed size
plainSize = file.size;
output[op1++] = output[op2++] = plainSize & 0xff;
output[op1++] = output[op2++] = (plainSize >> 8) & 0xff;
output[op1++] = output[op2++] = (plainSize >> 16) & 0xff;
output[op1++] = output[op2++] = (plainSize >> 24) & 0xff;
// filename length
output[op1++] = output[op2++] = filenameLength & 0xff;
output[op1++] = output[op2++] = (filenameLength >> 8) & 0xff;
// extra field length
output[op1++] = output[op2++] = extraFieldLength & 0xff;
output[op1++] = output[op2++] = (extraFieldLength >> 8) & 0xff;
// file comment length
output[op2++] = commentLength & 0xff;
output[op2++] = (commentLength >> 8) & 0xff;
// disk number start
output[op2++] = 0;
output[op2++] = 0;
// internal file attributes
output[op2++] = 0;
output[op2++] = 0;
// external file attributes
output[op2++] = 0;
output[op2++] = 0;
output[op2++] = 0;
output[op2++] = 0;
// relative offset of local header
output[op2++] = offset & 0xff;
output[op2++] = (offset >> 8) & 0xff;
output[op2++] = (offset >> 16) & 0xff;
output[op2++] = (offset >> 24) & 0xff;
// filename
filename = file.option['filename'];
if (filename) {
if (USE_TYPEDARRAY) {
output.set(filename, op1);
output.set(filename, op2);
op1 += filenameLength;
op2 += filenameLength;
} else {
for (j = 0; j < filenameLength; ++j) {
output[op1++] = output[op2++] = filename[j];
}
}
}
// extra field
extraField = file.option['extraField'];
if (extraField) {
if (USE_TYPEDARRAY) {
output.set(extraField, op1);
output.set(extraField, op2);
op1 += extraFieldLength;
op2 += extraFieldLength;
} else {
for (j = 0; j < commentLength; ++j) {
output[op1++] = output[op2++] = extraField[j];
}
}
}
// comment
comment = file.option['comment'];
if (comment) {
if (USE_TYPEDARRAY) {
output.set(comment, op2);
op2 += commentLength;
} else {
for (j = 0; j < commentLength; ++j) {
output[op2++] = comment[j];
}
}
}
//-------------------------------------------------------------------------
// file data
//-------------------------------------------------------------------------
if (USE_TYPEDARRAY) {
output.set(file.buffer, op1);
op1 += file.buffer.length;
} else {
for (j = 0, jl = file.buffer.length; j < jl; ++j) {
output[op1++] = file.buffer[j];
}
}
}
//-------------------------------------------------------------------------
// end of central directory
//-------------------------------------------------------------------------
// signature
output[op3++] = Zlib.Zip.CentralDirectorySignature[0];
output[op3++] = Zlib.Zip.CentralDirectorySignature[1];
output[op3++] = Zlib.Zip.CentralDirectorySignature[2];
output[op3++] = Zlib.Zip.CentralDirectorySignature[3];
// number of this disk
output[op3++] = 0;
output[op3++] = 0;
// number of the disk with the start of the central directory
output[op3++] = 0;
output[op3++] = 0;
// total number of entries in the central directory on this disk
output[op3++] = il & 0xff;
output[op3++] = (il >> 8) & 0xff;
// total number of entries in the central directory
output[op3++] = il & 0xff;
output[op3++] = (il >> 8) & 0xff;
// size of the central directory
output[op3++] = centralDirectorySize & 0xff;
output[op3++] = (centralDirectorySize >> 8) & 0xff;
output[op3++] = (centralDirectorySize >> 16) & 0xff;
output[op3++] = (centralDirectorySize >> 24) & 0xff;
// offset of start of central directory with respect to the starting disk number
output[op3++] = localFileSize & 0xff;
output[op3++] = (localFileSize >> 8) & 0xff;
output[op3++] = (localFileSize >> 16) & 0xff;
output[op3++] = (localFileSize >> 24) & 0xff;
// .ZIP file comment length
commentLength = this.comment ? this.comment.length : 0;
output[op3++] = commentLength & 0xff;
output[op3++] = (commentLength >> 8) & 0xff;
// .ZIP file comment
if (this.comment) {
if (USE_TYPEDARRAY) {
output.set(this.comment, op3);
op3 += commentLength;
} else {
for (j = 0, jl = commentLength; j < jl; ++j) {
output[op3++] = this.comment[j];
}
}
}
return output;
};
/**
* @param {!(Array.<number>|Uint8Array)} input
* @param {Object=} opt_params options.
* @return {!(Array.<number>|Uint8Array)}
*/
Zlib.Zip.prototype.deflateWithOption = function(input, opt_params) {
/** @type {Zlib.RawDeflate} */
var deflator = new Zlib.RawDeflate(input, opt_params['deflateOption']);
return deflator.compress();
};
/**
* @param {(Array.<number>|Uint32Array)} key
* @return {number}
*/
Zlib.Zip.prototype.getByte = function(key) {
/** @type {number} */
var tmp = ((key[2] & 0xffff) | 2);
return ((tmp * (tmp ^ 1)) >> 8) & 0xff;
};
/**
* @param {(Array.<number>|Uint32Array|Object)} key
* @param {number} n
* @return {number}
*/
Zlib.Zip.prototype.encode = function(key, n) {
/** @type {number} */
var tmp = this.getByte(/** @type {(Array.<number>|Uint32Array)} */(key));
this.updateKeys(/** @type {(Array.<number>|Uint32Array)} */(key), n);
return tmp ^ n;
};
/**
* @param {(Array.<number>|Uint32Array)} key
* @param {number} n
*/
Zlib.Zip.prototype.updateKeys = function(key, n) {
key[0] = Zlib.CRC32.single(key[0], n);
key[1] =
(((((key[1] + (key[0] & 0xff)) * 20173 >>> 0) * 6681) >>> 0) + 1) >>> 0;
key[2] = Zlib.CRC32.single(key[2], key[1] >>> 24);
};
/**
* @param {(Array.<number>|Uint8Array)} password
* @return {!(Array.<number>|Uint32Array|Object)}
*/
Zlib.Zip.prototype.createEncryptionKey = function(password) {
/** @type {!(Array.<number>|Uint32Array)} */
var key = [305419896, 591751049, 878082192];
/** @type {number} */
var i;
/** @type {number} */
var il;
if (USE_TYPEDARRAY) {
key = new Uint32Array(key);
}
for (i = 0, il = password.length; i < il; ++i) {
this.updateKeys(key, password[i] & 0xff);
}
return key;
};
});