// Minimal uncompressed ("stored") zip writer and reader. Enough for dataset files: // audio is already raw float samples and would barely compress anyway. 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; })(); export 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 interface ZipEntry { name: string; data: Uint8Array; } export function writeZip(entries: ZipEntry[]): Blob { const enc = new TextEncoder(); const now = new Date(); const dosTime = (now.getHours() << 11) | (now.getMinutes() << 5) | (now.getSeconds() >> 1); const dosDate = ((now.getFullYear() - 1980) << 9) | ((now.getMonth() + 1) << 5) | now.getDate(); const parts: BlobPart[] = []; const central: Uint8Array[] = []; let offset = 0; for (const e of entries) { const name = enc.encode(e.name); const crc = crc32(e.data); const local = new DataView(new ArrayBuffer(30)); local.setUint32(0, 0x04034b50, true); local.setUint16(4, 20, true); // version needed local.setUint16(6, 0x0800, true); // UTF-8 names local.setUint16(8, 0, true); // stored local.setUint16(10, dosTime, true); local.setUint16(12, dosDate, true); local.setUint32(14, crc, true); local.setUint32(18, e.data.length, true); local.setUint32(22, e.data.length, true); local.setUint16(26, name.length, true); local.setUint16(28, 0, true); parts.push(local.buffer, name as Uint8Array, e.data as Uint8Array); const c = new DataView(new ArrayBuffer(46 + name.length)); c.setUint32(0, 0x02014b50, true); c.setUint16(4, 20, true); c.setUint16(6, 20, true); c.setUint16(8, 0x0800, true); c.setUint16(10, 0, true); c.setUint16(12, dosTime, true); c.setUint16(14, dosDate, true); c.setUint32(16, crc, true); c.setUint32(20, e.data.length, true); c.setUint32(24, e.data.length, true); c.setUint16(28, name.length, true); c.setUint32(42, offset, true); new Uint8Array(c.buffer).set(name, 46); central.push(new Uint8Array(c.buffer)); offset += 30 + name.length + e.data.length; } const cdSize = central.reduce((s, c) => s + c.length, 0); const end = new DataView(new ArrayBuffer(22)); end.setUint32(0, 0x06054b50, true); end.setUint16(8, entries.length, true); end.setUint16(10, entries.length, true); end.setUint32(12, cdSize, true); end.setUint32(16, offset, true); return new Blob([...parts, ...(central as Uint8Array[]), end.buffer], { type: "application/zip" }); } // Reads stored (uncompressed) entries; throws on anything else. export function readZip(buf: ArrayBuffer): Map { const v = new DataView(buf); const bytes = new Uint8Array(buf); const dec = new TextDecoder(); let eocd = -1; for (let i = buf.byteLength - 22; i >= Math.max(0, buf.byteLength - 22 - 65535); i--) { if (v.getUint32(i, true) === 0x06054b50) { eocd = i; break; } } if (eocd < 0) throw new Error("Not a zip file."); const count = v.getUint16(eocd + 10, true); let p = v.getUint32(eocd + 16, true); const out = new Map(); for (let i = 0; i < count; i++) { if (v.getUint32(p, true) !== 0x02014b50) throw new Error("Damaged zip file."); const method = v.getUint16(p + 10, true); const crc = v.getUint32(p + 16, true); const size = v.getUint32(p + 20, true); const nameLen = v.getUint16(p + 28, true); const extraLen = v.getUint16(p + 30, true); const commentLen = v.getUint16(p + 32, true); const local = v.getUint32(p + 42, true); const name = dec.decode(bytes.subarray(p + 46, p + 46 + nameLen)); p += 46 + nameLen + extraLen + commentLen; if (name.endsWith("/")) continue; if (method !== 0) throw new Error(`“${name}” is compressed. Use a dataset file exported by this app, without re-zipping it.`); const start = local + 30 + v.getUint16(local + 26, true) + v.getUint16(local + 28, true); const data = bytes.subarray(start, start + size); if (crc32(data) !== crc) throw new Error(`“${name}” is damaged (checksum mismatch).`); out.set(name, data); } return out; }