// Minimal store-only (no compression) ZIP writer, enough for small text bundles. const CRC_TABLE = (() => { const t = new Uint32Array(256); for (let n = 0; n < 256; n++) { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; t[n] = c >>> 0; } return t; })(); function crc32(data: Uint8Array): number { let c = 0xffffffff; for (let i = 0; i < data.length; i++) c = CRC_TABLE[(c ^ data[i]) & 0xff] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; } export function makeZip(files: { path: string; data: Uint8Array }[]): Uint8Array { const enc = new TextEncoder(); const chunks: Uint8Array[] = []; const central: Uint8Array[] = []; let offset = 0; for (const f of files) { const name = enc.encode(f.path); const crc = crc32(f.data); const local = new DataView(new ArrayBuffer(30)); local.setUint32(0, 0x04034b50, true); local.setUint16(4, 20, true); local.setUint16(6, 0x0800, true); // UTF-8 names local.setUint16(8, 0, true); // stored local.setUint16(10, 0, true); local.setUint16(12, 0x21, true); // 1980-01-01 local.setUint32(14, crc, true); local.setUint32(18, f.data.length, true); local.setUint32(22, f.data.length, true); local.setUint16(26, name.length, true); local.setUint16(28, 0, true); chunks.push(new Uint8Array(local.buffer), name, f.data); const cd = new DataView(new ArrayBuffer(46)); cd.setUint32(0, 0x02014b50, true); cd.setUint16(4, 20, true); cd.setUint16(6, 20, true); cd.setUint16(8, 0x0800, true); cd.setUint16(10, 0, true); cd.setUint16(12, 0, true); cd.setUint16(14, 0x21, true); cd.setUint32(16, crc, true); cd.setUint32(20, f.data.length, true); cd.setUint32(24, f.data.length, true); cd.setUint16(28, name.length, true); cd.setUint32(38, 0o100644 << 16, true); cd.setUint32(42, offset, true); central.push(new Uint8Array(cd.buffer), name); offset += 30 + name.length + f.data.length; } const cdSize = central.reduce((n, c) => n + c.length, 0); const end = new DataView(new ArrayBuffer(22)); end.setUint32(0, 0x06054b50, true); end.setUint16(8, files.length, true); end.setUint16(10, files.length, true); end.setUint32(12, cdSize, true); end.setUint32(16, offset, true); const all = [...chunks, ...central, new Uint8Array(end.buffer)]; const out = new Uint8Array(all.reduce((n, c) => n + c.length, 0)); let p = 0; for (const c of all) { out.set(c, p); p += c.length; } return out; }