(files: {[name: string]: string})
| 38 | } |
| 39 | |
| 40 | export function zip(files: {[name: string]: string}): Blob { |
| 41 | let records: Uint8Array[] = []; |
| 42 | let cdrs: Uint8Array[] = []; |
| 43 | let te = new TextEncoder(); |
| 44 | |
| 45 | let offset = 0; |
| 46 | let cdSz = 0; |
| 47 | |
| 48 | for (let name in files) { |
| 49 | let fh = new Uint8Array(30 + name.length); |
| 50 | let fname = te.encode(name); |
| 51 | let data = te.encode(files[name]); |
| 52 | let chksum = crc32(data); |
| 53 | |
| 54 | putUint32s(fh, 0, 0x04034b50); |
| 55 | putUint32s(fh, 14, chksum, data.length, data.length); |
| 56 | putUint16s(fh, 26, name.length); |
| 57 | |
| 58 | fh.set(fname, 30); |
| 59 | |
| 60 | records.push(fh); |
| 61 | records.push(data); |
| 62 | |
| 63 | // Create CD records pending flush at the end... |
| 64 | let cdr = new Uint8Array(46 + name.length); |
| 65 | |
| 66 | putUint32s(cdr, 0, 0x02014b50); |
| 67 | putUint32s(cdr, 16, chksum, data.length, data.length); |
| 68 | putUint16s(cdr, 28, name.length); |
| 69 | putUint32s(cdr, 42, offset); |
| 70 | |
| 71 | cdr.set(fname, 46); |
| 72 | cdrs.push(cdr); |
| 73 | |
| 74 | cdSz += cdr.length; |
| 75 | offset += fh.length + data.length; |
| 76 | } |
| 77 | |
| 78 | // Push all accrued CD records.. |
| 79 | records.push(...cdrs); |
| 80 | |
| 81 | // Add EOCD record... |
| 82 | let eocd = new Uint8Array(22); |
| 83 | putUint32s(eocd, 0, 0x06054b50); |
| 84 | putUint16s(eocd, 8, cdrs.length, cdrs.length); |
| 85 | putUint32s(eocd, 12, cdSz, offset); |
| 86 | records.push(eocd); |
| 87 | |
| 88 | return new Blob(records as BlobPart[], {type: 'application/zip'}); |
| 89 | } |
no test coverage detected