/** * 最小 ZIP 打包器 —— docx / xlsx 就是 ZIP 包着几份 XML,写它才写得出真文件。 * * 只做 **store(不压缩,method 0)**:合法的 ZIP,Word / Excel / macOS 归档工具都认。 * 为什么不压:压缩要 deflate + 一套长度对账,而这些 XML 至多几十 KB —— * 省下来的那几 KB 换一处能错的地方,不划算。将来真要压,把 `method` 与数据段一起换即可。 * * 读的那一侧在 `upload-extract.ts`(`readZip`),两边合起来能自证: * 造出来的包读得回来,读回来的内容与进去的一模一样。 */ /** CRC-32(IEEE 多项式 0xEDB88320)—— ZIP 每条目都要校验和,写错了解压工具会报损坏 */ const CRC_TABLE = (() => { const table = new Uint32Array(256); for (let i = 0; i < 256; i += 1) { let c = i; for (let k = 0; k < 8; k += 1) c = (c & 1) === 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; table[i] = c >>> 0; } return table; })(); export function crc32Of(bytes: Uint8Array): number { let c = 0xffffffff; for (const b of bytes) c = (CRC_TABLE[(c ^ b) & 0xff] ?? 0) ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; } type Entry = readonly [name: string, bytes: Uint8Array]; /** * 打包。条目顺序即包内顺序 —— `[Content_Types].xml` 习惯放第一条(不少读取器认这个)。 * * 时间戳一律写 1980-01-01(DOS 时间的下限):同样的入参出同样的字节, * 这样「文件变没变」就是内容的事,不掺一个每次都不同的时间 —— 也让测试断得住。 */ export function zipStore(entries: readonly Entry[]): Uint8Array { const enc = new TextEncoder(); const locals: Uint8Array[] = []; const centrals: Uint8Array[] = []; let offset = 0; for (const [name, data] of entries) { const nameBytes = enc.encode(name); const crc = crc32Of(data); const local = new Uint8Array(30 + nameBytes.length + data.length); const lv = new DataView(local.buffer); lv.setUint32(0, 0x04034b50, true); // 本地头签名 lv.setUint16(4, 20, true); // 需要 2.0 才能解 lv.setUint16(6, 0x0800, true); // 位标志:文件名是 UTF-8 lv.setUint16(8, 0, true); // method 0 = store lv.setUint16(10, 0, true); // 时:0 lv.setUint16(12, 33, true); // 日:1980-01-01 lv.setUint32(14, crc, true); lv.setUint32(18, data.length, true); // 压缩后大小 = 原大小(store) lv.setUint32(22, data.length, true); lv.setUint16(26, nameBytes.length, true); lv.setUint16(28, 0, true); // 无 extra local.set(nameBytes, 30); local.set(data, 30 + nameBytes.length); locals.push(local); const central = new Uint8Array(46 + nameBytes.length); const cv = new DataView(central.buffer); cv.setUint32(0, 0x02014b50, true); // 中央目录签名 cv.setUint16(4, 20, true); // 造它的版本 cv.setUint16(6, 20, true); cv.setUint16(8, 0x0800, true); cv.setUint16(10, 0, true); cv.setUint16(12, 0, true); cv.setUint16(14, 33, true); cv.setUint32(16, crc, true); cv.setUint32(20, data.length, true); cv.setUint32(24, data.length, true); cv.setUint16(28, nameBytes.length, true); cv.setUint32(42, offset, true); // 本地头在包里的偏移 central.set(nameBytes, 46); centrals.push(central); offset += local.length; } const centralSize = centrals.reduce((n, c) => n + c.length, 0); const eocd = new Uint8Array(22); const ev = new DataView(eocd.buffer); ev.setUint32(0, 0x06054b50, true); ev.setUint16(8, entries.length, true); // 本卷条目数 ev.setUint16(10, entries.length, true); // 总条目数 ev.setUint32(12, centralSize, true); ev.setUint32(16, offset, true); // 中央目录起点 const total = offset + centralSize + eocd.length; const out = new Uint8Array(total); let p = 0; for (const part of [...locals, ...centrals, eocd]) { out.set(part, p); p += part.length; } return out; }