zip.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* jshint esversion: 6 */
  2. /* global pako */
  3. var zip = zip || {};
  4. zip.Archive = class {
  5. constructor(buffer) {
  6. this._entries = [];
  7. if (buffer.length < 4 || buffer[0] != 0x50 || buffer[1] != 0x4B) {
  8. throw new zip.Error('Invalid Zip archive.');
  9. }
  10. let reader = null;
  11. for (let i = buffer.length - 4; i >= 0; i--) {
  12. if (buffer[i] === 0x50 && buffer[i + 1] === 0x4B && buffer[i + 2] === 0x05 && buffer[i + 3] === 0x06) {
  13. reader = new zip.Reader(buffer, i + 4, buffer.length);
  14. break;
  15. }
  16. }
  17. if (!reader) {
  18. throw new zip.Error('End of central directory not found.');
  19. }
  20. reader.skip(12);
  21. reader.position = reader.uint32(); // central directory offset
  22. while (reader.match([ 0x50, 0x4B, 0x01, 0x02 ])) {
  23. this._entries.push(new zip.Entry(reader));
  24. }
  25. }
  26. get entries() {
  27. return this._entries;
  28. }
  29. };
  30. zip.Entry = class {
  31. constructor(reader) {
  32. reader.uint16(); // version made by
  33. reader.skip(2); // version needed to extract
  34. this._flags = reader.uint16();
  35. if ((this._flags & 1) == 1) {
  36. throw new zip.Error('Encrypted entries not supported.');
  37. }
  38. this._compressionMethod = reader.uint16();
  39. reader.uint32(); // date
  40. reader.uint32(); // crc32
  41. this._compressedSize = reader.uint32();
  42. this._size = reader.uint32();
  43. let nameLength = reader.uint16(); // file name length
  44. let extraDataLength = reader.uint16();
  45. const commentLength = reader.uint16();
  46. reader.uint16(); // disk number start
  47. reader.uint16(); // internal file attributes
  48. reader.uint32(); // external file attributes
  49. const localHeaderOffset = reader.uint32();
  50. reader.skip(nameLength);
  51. reader.skip(extraDataLength);
  52. reader.bytes(commentLength); // comment
  53. const position = reader.position;
  54. reader.position = localHeaderOffset;
  55. if (!reader.match([ 0x50, 0x4B, 0x03, 0x04 ])) {
  56. throw new zip.Error('Invalid local file header signature.');
  57. }
  58. reader.skip(22);
  59. nameLength = reader.uint16();
  60. extraDataLength = reader.uint16();
  61. const nameBuffer = reader.bytes(nameLength);
  62. this._name = '';
  63. for (const c of nameBuffer) {
  64. this._name += String.fromCharCode(c);
  65. }
  66. reader.skip(extraDataLength);
  67. this._compressedData = reader.bytes(this._compressedSize);
  68. reader.position = position;
  69. }
  70. get name() {
  71. return this._name;
  72. }
  73. get data() {
  74. if (!this._data) {
  75. switch (this._compressionMethod) {
  76. case 0: // Stored
  77. if (this._size != this._compressedSize) {
  78. throw new zip.Error('Invalid compression size.');
  79. }
  80. this._data = new Uint8Array(this._compressedData.length);
  81. this._data.set(this._compressedData);
  82. break;
  83. case 8: // Deflate
  84. this._data = new zip.Inflater().inflateRaw(this._compressedData);
  85. if (this._size != this._data.length) {
  86. throw new zip.Error('Invalid uncompressed size.');
  87. }
  88. break;
  89. default:
  90. throw new zip.Error('Invalid compression method.');
  91. }
  92. delete this._size;
  93. delete this._compressedData;
  94. }
  95. return this._data;
  96. }
  97. };
  98. zip.HuffmanTree = class {
  99. constructor() {
  100. this.table = new Uint16Array(16);
  101. this.symbol = new Uint16Array(288);
  102. zip.HuffmanTree._offsets = zip.HuffmanTree._offsets || new Uint16Array(16);
  103. }
  104. build(lengths, offset, count) {
  105. for (let i = 0; i < 16; ++i) {
  106. this.table[i] = 0;
  107. }
  108. for (let i = 0; i < count; ++i) {
  109. this.table[lengths[offset + i]]++;
  110. }
  111. this.table[0] = 0;
  112. let sum = 0;
  113. for (let i = 0; i < 16; i++) {
  114. zip.HuffmanTree._offsets[i] = sum;
  115. sum += this.table[i];
  116. }
  117. for (let i = 0; i < count; i++) {
  118. if (lengths[offset + i]) {
  119. this.symbol[zip.HuffmanTree._offsets[lengths[offset + i]]++] = i;
  120. }
  121. }
  122. }
  123. static initialize() {
  124. if (!zip.HuffmanTree.staticLiteralLengthTree) {
  125. zip.HuffmanTree.staticLiteralLengthTree = new zip.HuffmanTree();
  126. zip.HuffmanTree.staticLiteralLengthTree.table = new Uint8Array([ 0, 0, 0, 0, 0, 0, 0, 24, 152, 112, 0, 0, 0, 0, 0, 0 ]);
  127. for (let i = 0; i < 24; ++i) {
  128. zip.HuffmanTree.staticLiteralLengthTree.symbol[i] = 256 + i;
  129. }
  130. for (let i = 0; i < 144; ++i) {
  131. zip.HuffmanTree.staticLiteralLengthTree.symbol[24 + i] = i;
  132. }
  133. for (let i = 0; i < 8; ++i) {
  134. zip.HuffmanTree.staticLiteralLengthTree.symbol[24 + 144 + i] = 280 + i;
  135. }
  136. for (let i = 0; i < 112; ++i) {
  137. zip.HuffmanTree.staticLiteralLengthTree.symbol[24 + 144 + 8 + i] = 144 + i;
  138. }
  139. zip.HuffmanTree.staticDistanceTree = new zip.HuffmanTree();
  140. zip.HuffmanTree.staticDistanceTree.table = new Uint8Array([ 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]);
  141. zip.HuffmanTree.staticDistanceTree.symbol = new Uint8Array([ 0, 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 ]);
  142. }
  143. }
  144. };
  145. zip.Inflater = class {
  146. inflate(data) {
  147. if (typeof process === 'object' && typeof process.versions == 'object' && typeof process.versions.node !== 'undefined') {
  148. return require('zlib').inflateSync(data);
  149. }
  150. if (typeof pako !== 'undefined') {
  151. return pako.inflate(data);
  152. }
  153. throw new zip.Error("zlib inflate not supported.");
  154. }
  155. inflateRaw(data) {
  156. if (typeof process === 'object' && typeof process.versions == 'object' && typeof process.versions.node !== 'undefined') {
  157. return require('zlib').inflateRawSync(data);
  158. }
  159. if (typeof pako !== 'undefined') {
  160. return pako.inflateRaw(data);
  161. }
  162. zip.Inflater.initilize();
  163. zip.HuffmanTree.initialize();
  164. const reader = new zip.BitReader(data);
  165. const output = new zip.Ouptut();
  166. const literalLengthTree = new zip.HuffmanTree();
  167. const distanceTree = new zip.HuffmanTree();
  168. let type;
  169. do {
  170. type = reader.bits(3);
  171. switch (type >>> 1) {
  172. case 0: // uncompressed block
  173. this._inflateUncompressedBlock(reader, output);
  174. break;
  175. case 1: // block with fixed huffman trees
  176. this._inflateBlockData(reader, output, zip.HuffmanTree.staticLiteralLengthTree, zip.HuffmanTree.staticDistanceTree);
  177. break;
  178. case 2: // block with dynamic huffman trees
  179. this._decodeTrees(reader, literalLengthTree, distanceTree);
  180. this._inflateBlockData(reader, output, literalLengthTree, distanceTree);
  181. break;
  182. default:
  183. throw new zip.Error('Unknown block type.');
  184. }
  185. } while ((type & 1) == 0);
  186. return output.merge();
  187. }
  188. _inflateUncompressedBlock(reader, output) {
  189. while (reader.data > 8) {
  190. reader.position--;
  191. reader.data -= 8;
  192. }
  193. reader.data = 0;
  194. const length = reader.uint16();
  195. const inverseLength = reader.uint16();
  196. if (length !== (~inverseLength & 0x0000ffff)) {
  197. throw new zip.Error('Invalid uncompressed block length.');
  198. }
  199. const block = reader.bytes(length);
  200. output.push(block);
  201. if (length > 32768) {
  202. output.buffer.set(block.subarray(block.length - 32768, block.length), 0);
  203. output.position = 32768;
  204. }
  205. else {
  206. output.reset();
  207. output.buffer.set(block, output.position);
  208. output.position += block.length;
  209. }
  210. }
  211. _decodeTrees(reader, lengthTree, distanceTree) {
  212. const hlit = reader.bits(5) + 257;
  213. const hdist = reader.bits(5) + 1;
  214. const lengthCount = reader.bits(4) + 4;
  215. for (let i = 0; i < 19; i++) {
  216. zip.Inflater._lengths[i] = 0;
  217. }
  218. for (let j = 0; j < lengthCount; j++) {
  219. zip.Inflater._lengths[zip.Inflater._codeOrder[j]] = reader.bits(3);
  220. }
  221. zip.Inflater._codeTree.build(zip.Inflater._lengths, 0, 19);
  222. let length;
  223. for (let position = 0; position < hlit + hdist;) {
  224. const symbol = reader.symbol(zip.Inflater._codeTree);
  225. switch (symbol) {
  226. case 16: {
  227. const prev = zip.Inflater._lengths[position - 1];
  228. for (length = reader.bits(2) + 3; length; length--) {
  229. zip.Inflater._lengths[position++] = prev;
  230. }
  231. break;
  232. }
  233. case 17: {
  234. for (length = reader.bits(3) + 3; length; length--) {
  235. zip.Inflater._lengths[position++] = 0;
  236. }
  237. break;
  238. }
  239. case 18: {
  240. for (length = reader.bits(7) + 11; length; length--) {
  241. zip.Inflater._lengths[position++] = 0;
  242. }
  243. break;
  244. }
  245. default: {
  246. zip.Inflater._lengths[position++] = symbol;
  247. break;
  248. }
  249. }
  250. }
  251. lengthTree.build(zip.Inflater._lengths, 0, hlit);
  252. distanceTree.build(zip.Inflater._lengths, hlit, hdist);
  253. }
  254. _inflateBlockData(reader, output, lengthTree, distanceTree) {
  255. const buffer = output.buffer;
  256. let position = output.position;
  257. let start = position;
  258. for (;;) {
  259. if (position > 62464) {
  260. output.position = position;
  261. output.push(new Uint8Array(buffer.subarray(start, position)));
  262. position = output.reset();
  263. start = position;
  264. }
  265. let symbol = reader.symbol(lengthTree);
  266. if (symbol === 256) {
  267. output.position = position;
  268. output.push(new Uint8Array(buffer.subarray(start, output.position)));
  269. output.reset();
  270. return;
  271. }
  272. if (symbol < 256) {
  273. buffer[position++] = symbol;
  274. }
  275. else {
  276. symbol -= 257;
  277. const length = reader.bitsBase(zip.Inflater._lengthBits[symbol], zip.Inflater._lengthBase[symbol]);
  278. const distance = reader.symbol(distanceTree);
  279. let offset = position - reader.bitsBase(zip.Inflater._distanceBits[distance], zip.Inflater._distanceBase[distance]);
  280. for (let i = 0; i < length; i++) {
  281. buffer[position++] = buffer[offset++];
  282. }
  283. }
  284. }
  285. }
  286. static initilize() {
  287. if (zip.HuffmanTree.staticLiteralLengthTree) {
  288. return;
  289. }
  290. zip.Inflater._codeOrder = [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
  291. zip.Inflater._codeTree = new zip.HuffmanTree();
  292. zip.Inflater._lengths = new Uint8Array(288 + 32);
  293. zip.Inflater._lengthBits = [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 6 ];
  294. zip.Inflater._lengthBase = [ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 323 ];
  295. zip.Inflater._distanceBits = [ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 ];
  296. zip.Inflater._distanceBase = [ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 ];
  297. }
  298. };
  299. zip.Ouptut = class {
  300. constructor() {
  301. this._blocks = [];
  302. this.buffer = new Uint8Array(65536);
  303. this.position = 0;
  304. }
  305. reset() {
  306. if (this.position > 32768) {
  307. this.buffer.set(this.buffer.subarray(this.position - 32768, this.position), 0);
  308. this.position = 32768;
  309. }
  310. return this.position;
  311. }
  312. push(block) {
  313. this._blocks.push(block);
  314. }
  315. merge() {
  316. let size = 0;
  317. for (const block1 of this._blocks) {
  318. size += block1.length;
  319. }
  320. const output = new Uint8Array(size);
  321. let offset = 0;
  322. for (const block2 of this._blocks) {
  323. output.set(block2, offset);
  324. offset += block2.length;
  325. }
  326. return output;
  327. }
  328. };
  329. zip.BitReader = class {
  330. constructor(buffer) {
  331. this.buffer = buffer;
  332. this.position = 0;
  333. this.data = 0;
  334. this.value = 0;
  335. }
  336. bits(count) {
  337. while (this.data < 24) {
  338. this.value |= this.buffer[this.position++] << this.data;
  339. this.data += 8;
  340. }
  341. const value = this.value & (0xffff >>> (16 - count));
  342. this.value >>>= count;
  343. this.data -= count;
  344. return value;
  345. }
  346. bitsBase(count, base) {
  347. if (count == 0) {
  348. return base;
  349. }
  350. while (this.data < 24) {
  351. this.value |= this.buffer[this.position++] << this.data;
  352. this.data += 8;
  353. }
  354. const value = this.value & (0xffff >>> (16 - count));
  355. this.value >>>= count;
  356. this.data -= count;
  357. return value + base;
  358. }
  359. bytes(size) {
  360. const value = this.buffer.subarray(this.position, this.position + size);
  361. this.position += size;
  362. return value;
  363. }
  364. uint16() {
  365. const value = this.buffer[this.position] | (this.buffer[this.position + 1] << 8);
  366. this.position += 2;
  367. return value;
  368. }
  369. symbol(tree) {
  370. while (this.data < 24) {
  371. this.value |= this.buffer[this.position++] << this.data;
  372. this.data += 8;
  373. }
  374. let sum = 0;
  375. let current = 0;
  376. let length = 0;
  377. let value = this.value;
  378. const table = tree.table;
  379. do {
  380. current = (current << 1) + (value & 1);
  381. value >>>= 1;
  382. length++;
  383. sum += table[length];
  384. current -= table[length];
  385. } while (current >= 0);
  386. this.value = value;
  387. this.data -= length;
  388. return tree.symbol[sum + current];
  389. }
  390. };
  391. zip.Reader = class {
  392. constructor(buffer, start, end) {
  393. this._buffer = buffer;
  394. this._position = start;
  395. this._end = end;
  396. }
  397. match(signature) {
  398. if (this._position + signature.length <= this._end) {
  399. for (let i = 0; i < signature.length; i++) {
  400. if (this._buffer[this._position + i] != signature[i]) {
  401. return false;
  402. }
  403. }
  404. }
  405. this._position += signature.length;
  406. return true;
  407. }
  408. get position() {
  409. return this._position;
  410. }
  411. set position(value) {
  412. this._position = value >= 0 ? value : this._end + value;
  413. }
  414. peek() {
  415. return this._position < this._end;
  416. }
  417. skip(size) {
  418. if (this._position + size > this._end) {
  419. throw new zip.Error('Data not available.');
  420. }
  421. this._position += size;
  422. }
  423. bytes(size) {
  424. if (this._position + size > this._end) {
  425. throw new zip.Error('Data not available.');
  426. }
  427. size = size === undefined ? this._end : size;
  428. const data = this._buffer.subarray(this._position, this._position + size);
  429. this._position += size;
  430. return data;
  431. }
  432. uint16() {
  433. if (this._position + 2 > this._end) {
  434. throw new zip.Error('Data not available.');
  435. }
  436. const value = this._buffer[this._position] | (this._buffer[this._position + 1] << 8);
  437. this._position += 2;
  438. return value;
  439. }
  440. uint32() {
  441. return this.uint16() | (this.uint16() << 16);
  442. }
  443. };
  444. zip.Error = class extends Error {
  445. constructor(message) {
  446. super(message);
  447. this.name = 'Zip Error';
  448. }
  449. };
  450. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  451. module.exports.Archive = zip.Archive;
  452. module.exports.Inflater = zip.Inflater;
  453. }