-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip.js
More file actions
92 lines (82 loc) · 2.9 KB
/
zip.js
File metadata and controls
92 lines (82 loc) · 2.9 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
/* globals Blob, Promise, Response */
;((exports, Uint8Array) => {
// Attach createZip to `window` in non-module context
exports.createZip = (files, opts, next) => {
if (typeof opts == "function") {
next = opts
opts = {}
}
var i = 256, j, k, offset = 0
, crcTable = []
, cd = ""
, out = []
, outLen = 0
, CompressionStream = (exports.window || global).CompressionStream
, now = Date.now()
, push = arr => {
out.push(arr)
outLen += arr.length
}
, dosDate = date => Math.max(2162688, date.getSeconds() >> 1 | date.getMinutes() << 5 | date.getHours() << 11 | date.getDate() << 16 | (date.getMonth() + 1) << 21 | (date.getFullYear() - 1980) << 25)
, le16 = n => String.fromCharCode(n & 0xff, (n >>> 8) & 0xff)
, le32 = n => le16(n) + le16(n >>> 16)
, toUint = str => {
for (var pos = str.length, arr = new Uint8Array(pos); pos--; arr[pos] = str.charCodeAt(pos));
return arr
}
, toUtf8 = str => unescape(encodeURIComponent(str || ""))
, compress = (uint, len, cb) => {
if (opts && opts.deflate) {
var compressed = opts.deflate(uint)
cb(len > compressed.length ? compressed : uint)
} else if (CompressionStream) {
new Response(
new Blob([uint]).stream().pipeThrough(new CompressionStream("deflate"))
).arrayBuffer().then(arr => {
cb(len > arr.byteLength - 6 ? new Uint8Array(arr).subarray(2, -4) : uint)
})
} else cb(uint)
}
, add = resolve => {
k = files[i++]
if (!k) {
k = files.length
name = toUtf8(opts && opts.comment)
push(toUint(cd + "PK\5\6" + le32(0) + le32((k<<16) + k) + le32(cd.length) + le32(offset) + le16(name.length) + name))
file = new Uint8Array(outLen)
for (i = 0, offset = 0; (j = out[i++]); offset += j.length) file.set(j, offset)
return resolve(file)
}
var fileLen
, name = toUtf8(k.name)
, nameLen = name.length
, file = k.content
, crc = -1
if (typeof file === "string") file = toUint(toUtf8(file))
fileLen = file.length
for (j = 0; j < fileLen; ) {
crc = (crc >>> 8) ^ crcTable[(crc ^ file[j++]) & 0xff]
}
compress(file, fileLen, (compressed, method) => {
method = file === compressed ? "\0\0" : "\10\0"
method = le32(20 | 1<<27) + method + le32(dosDate(new Date(k.time > 0 || k.time === 0 ? k.time : now))) + le32(-1^crc >>> 0) + le32(compressed.length) + le32(fileLen) + le32(nameLen)
push(toUint("PK\3\4" + method + name))
push(compressed)
cd += "PK\1\2\0\24" + method + "\0\0" + le32(0) + le32(32) + le32(offset) + name
offset += 30 + compressed.length + nameLen
add(resolve)
})
}
for (; i; crcTable[i] = k) {
k = --i
for (j = 8; j--; ) k = 0xedb88320 * (1&k) ^ k >>> 1
}
if (next) {
add(next.bind(next, null))
} else if (opts && opts.deflate) {
add(file => out = file)
return out
} else return new Promise(add)
}
// this is `exports` in module and `window` in browser
})(this, Uint8Array) // jshint ignore:line