-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
389 lines (349 loc) · 12.7 KB
/
index.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
var Promise = global.Promise || require('es6-promise');
var zlib = require('zlib'), stream = require('stream');
exports.ZipFile = ZipFile;
exports.ZipEntry = ZipEntry;
exports.create = function (map) {
var ret = new ZipFile();
if (map && typeof map === 'object') {
for (var keys = Object.keys(map), i = 0, L = keys.length; i < L; i++) {
ret.add(keys[i], map[keys[i]]);
}
}
return ret
};
function ZipFile() {
}
/**
* Adds zip entry
*
* @param{string} name file name, must be valid path name
* @param{Buffer} buffer
* @param{Object} options
*/
ZipFile.prototype.add = function (name, buffer, options) {
name = String(name);
if (!/^[^/]+(?:\/[^\/]+)*/.test(name)) {
throw new Error('Bad entry name: ' + name);
}
addDirEntry(this, name);
var entry = this[name] = new ZipEntry(name, options);
entry.buffer = buffer;
entry.originalSize = buffer.length;
entry.crc32 = crc32(buffer);
if (entry.noCompress) {
entry.compressed = buffer;
}
};
function addDirEntry(file, name) {
var idx = name.lastIndexOf('/') + 1;
if (!idx) return;
var parent = name.substr(0, idx);
if (parent in file) return;
var entry = file[parent] = new ZipEntry(parent);
entry.isDir = true;
addDirEntry(file, parent.substr(0, idx - 1));
}
ZipFile.prototype.zip = function () {
var self = this;
var names = Object.keys(this);
return Promise.all(names.map(function (name) {
try {
return self[name]._compress();
} catch (e) {
console.error(e.message, name, self[name]);
throw e;
}
})).then(function (zippedEntries) {
// TODO build zip file
var fileLen = 0;
var nameBufs = [];
for (var i = 0, L = names.length; i < L; i++) {
var nameBuf = nameBufs[i] = new Buffer(names[i]), zippedBuf = zippedEntries[i];
fileLen += (nameBuf.length << 1) + (zippedBuf ? zippedBuf.length : 0); // CONSTANT
}
fileLen += L * 76 + 22;
var dest = new Buffer(fileLen), offset = 0;
// write entries
for (i = 0; i < L; i++) {
var entry = self[names[i]];
var options = entry.options;
nameBuf = nameBufs[i];
zippedBuf = zippedEntries[i];
entry.offset = offset;
offset = dest.writeUInt32BE(0x504B0304, offset, true); // 'PK',03,04
offset = dest.writeUInt32BE(0, offset, true); // version needed to extract the file | flags
offset = dest.writeUInt16LE(entry.noCompress ? 0 : 8, offset, true); // compress method
offset = dest.writeUInt32LE(0, offset, true); // date and time
offset = dest.writeUInt32LE(entry.crc32, offset, true); // crc32
offset = dest.writeUInt32LE(entry.isDir ? 0 : zippedBuf.length, offset, true); // compressed size
offset = dest.writeUInt32LE(entry.originalSize, offset, true); // uncompressed size
offset = dest.writeUInt16LE(nameBuf.length, offset, true); // filename len
offset = dest.writeUInt16LE(0, offset, true); // extra field length
nameBuf.copy(dest, offset);
offset += nameBuf.length;
if (zippedBuf) {
zippedBuf.copy(dest, offset);
offset += zippedBuf.length;
}
}
var directoryOffset = offset;
// write central directory
for (i = 0; i < L; i++) {
entry = self[names[i]];
options = entry.options;
nameBuf = nameBufs[i];
zippedBuf = zippedEntries[i];
offset = dest.writeUInt32BE(0x504B0102, offset, true); // 'PK',01,02
offset = dest.writeUInt32BE(0, offset, true); // version made by | version needed to extract
offset = dest.writeUInt16LE(0, offset, true); // flags
offset = dest.writeUInt16LE(entry.noCompress ? 0 : 8, offset, true); // compress method
offset = dest.writeUInt32LE(0, offset, true); // date and time
offset = dest.writeUInt32LE(entry.crc32, offset, true); // crc32
offset = dest.writeUInt32LE(zippedBuf ? zippedBuf.length : 0, offset, true); // compressed size
offset = dest.writeUInt32LE(entry.originalSize, offset, true); // uncompressed size
offset = dest.writeUInt16LE(nameBuf.length, offset, true); // filename len
offset = dest.writeUInt32LE(0, offset, true); // extra field length | file comment length
offset = dest.writeUInt32LE(0, offset, true); // disk number | internal attribute
offset = dest.writeUInt32LE(0, offset, true); // external attributes
offset = dest.writeUInt32LE(entry.offset, offset, true); // entry offset
nameBuf.copy(dest, offset);
offset += nameBuf.length;
}
// end of central directory
var directoryLen = offset - directoryOffset;
offset = dest.writeUInt32BE(0x504B0506, offset, true); // 'PK',05,06
offset = dest.writeUInt32LE(0, offset, true);
offset = dest.writeUInt16LE(L, offset, true);
offset = dest.writeUInt16LE(L, offset, true); // number of central directory records
offset = dest.writeUInt32LE(directoryLen, offset, true); // number of central directory records
offset = dest.writeUInt32LE(directoryOffset, offset, true); // number of central directory records
offset = dest.writeUInt16LE(0, offset, true); // comment length
return dest;
});
};
ZipFile.prototype.entries = function () {
var keys = Object.keys(this), cursor = 0, len = keys.length, self = this;
var ret = {
next: function () {
if (cursor === len) {
return {done: true}
} else {
var key = keys[cursor++];
return {done: false, value: [key, self[key]]}
}
}
};
if (typeof Symbol === 'function' && Symbol.iterator) {
ret[Symbol.iterator] = function () {
return ret
}
}
return ret;
};
if (typeof Symbol === 'function' && Symbol.iterator) {
ZipFile.prototype[Symbol.iterator] = function () {
var keys = Object.keys(this), cursor = 0, len = keys.length, self = this;
return {
next: function () {
if (cursor === len) {
return {done: true}
} else {
var key = keys[cursor++];
return {done: false, value: self[key]}
}
}
};
}
}
/**
* default close method, do nothing
*/
ZipFile.prototype.close = function () {
};
ZipFile.prototype._closeHook = function (cb) {
var _close = this.close;
Object.defineProperty(this, 'close', {
enumurable: false,
configurable: true,
value: function () {
cb.call(this);
_close.call(this);
}
});
return this;
};
function ZipEntry(name, options) {
this.name = name;
this.options = options;
if (this.noCompress = options && options.compressed === false) {
options.level = 0;
}
this.buffer = this.compressed = null;
this.originalSize = this.crc32 = 0;
this._deflatePending = this._inflatePending = null;
this.isDir = false;
}
ZipEntry.prototype.inflate = function () {
if (this.buffer) { // uncompressed
return Promise.resolve(this.buffer);
}
if (this._inflatePending) {
return this._inflatePending;
}
var self = this;
return this._inflatePending = new Promise(function (resolve) {
var bufs = [];
self.toReadStream().on('data', bufs.push.bind(bufs)).on('end', function () {
self._inflatePending = null;
resolve(Buffer.concat(bufs))
});
})
};
ZipEntry.prototype.toReadStream = function () {
if (this.buffer) { // uncompressed
var ret = new stream.Readable;
ret.push(this.buffer);
ret.push(null);
return ret;
}
ret = zlib.createInflateRaw();
this.pipe(ret);
return ret;
};
ZipEntry.prototype._read = function () {
};
ZipEntry.prototype.pipe = function (writable) {
var buf;
if (this.compressed && !this.noCompress) {
buf = this.compressed;
} else if (this.buffer) { // from uncompressed
var transform = zlib.createDeflateRaw(this.options);
transform.pipe(writable);
buf = this.buffer;
writable = transform;
} else {
buf = this.compressed = this._read();
delete this._read;
}
writable.write(buf);
writable.end();
};
ZipEntry.prototype._compress = function () {
if (this.isDir) {
return null;
}
var compressed = this.compressed;
if (compressed) {
return Promise.resolve(compressed);
}
if (this._deflatePending) {
return this._deflatePending;
}
var self = this;
return this._deflatePending = new Promise(function (resolve, reject) {
zlib.deflateRaw(self.buffer, self.options, function (err, buf) {
if (err) {
reject(err);
} else {
self._deflatePending = null;
resolve(self.compressed = buf);
}
});
});
};
// .zip file format parser
// Supply a reader method to read from supplied buffer
function zipFile(fileLength, read, readAll) {
var buf = read(fileLength - 22, 22);
// read tail chunk
if (buf.readUInt32BE(0) !== 0x504B0506) {
throw new Error("Bad zip file format");
}
var dirsLength = buf.readUInt32LE(12, true),
dirsOffset = buf.readUInt32LE(16, true);
buf = read(dirsOffset, dirsLength);
var ret = new ZipFile();
for (var pos = 0, nextPos; pos < dirsLength; pos = nextPos) {
if (buf.readUInt32BE(pos) !== 0x504B0102) { // read dir
throw new Error("Bad zip file chunk");
}
var nameLen = buf.readUInt16LE(pos + 28);
var name = buf.slice(pos + 46, pos + 46 + nameLen).toString(),
isDir = name[name.length - 1] === '/',
extraLen = buf.readUInt16LE(pos + 30),
commentLen = buf.readUInt16LE(pos + 32);
nextPos = pos + 46 + nameLen + extraLen + commentLen;
//if (isDir) continue;
var compressed = buf[pos + 10] !== 0;
var entry = ret[name] = new ZipEntry(name, compressed ? null : {
compressed: false
});
if (isDir) {
entry.isDir = true;
entry.crc32 = 0;
entry.originalSize = 0;
} else {
entry.crc32 = buf.readUInt32LE(pos + 16);
entry.originalSize = buf.readUInt32LE(pos + 24);
var offset = buf.readUInt32LE(pos + 42) + 28,
length = buf.readUInt32LE(pos + 20);
offset += 2 + nameLen + read(offset, 2).readUInt16LE(0, true);
if (readAll) {
entry.compressed = read(offset, length);
if (!compressed) {
entry.buffer = entry.compressed;
}
} else {
entry._read = read.bind(null, offset, length);
}
}
}
return ret;
}
var fs = require('fs');
/**
*
* @param file a filename, or a fd, or a buffer
* @returns {*}
*/
exports.unzip = function (file) {
if (typeof file === 'string') {
file = fs.openSync(file, 'r');
}
if (typeof file === 'number') {
return zipFile(fs.fstatSync(file).size, function (offset, length) {
var buf = new Buffer(length);
for (var red = 0; red < length;) {
red += fs.readSync(file, buf, red, length - red, offset + red);
}
return buf;
}, false)._closeHook(function () {
fs.closeSync(file);
});
} else if (Buffer.isBuffer(file)) {
return zipFile(file.length, function (offset, length) {
return file.slice(offset, offset + length)
}, true);
} else {
throw new Error('Expected file path, fd, or a buffer');
}
};
var crc_table = new Uint32Array(256);
for (var i = 0; i < 256; i++) {
var c = i;
for (var j = 0; j < 8; j++) {
var cr = c & 1;
c = c >> 1 & 0x7FFFFFFF;
if (cr) {
c ^= 0xedb88320;
}
}
crc_table[i] = c;
}
function crc32(arr) {
var crc = -1;
for (var i = 0, end = arr.length; i < end; i++) {
crc = crc_table[crc & 0xFF ^ arr[i]] ^ (crc >> 8 & 0xFFFFFF);
}
return ~crc;
}