// Minimal ZIP reader for the Play financial report exports (earnings_YYYYMM.zip, salesreport_YYYYMM.zip: one CSV // inside). Reads the central directory, supports stored (0) and deflate (8) entries; no ZIP64, no encryption. Uses // node:zlib (built into Bun), so storebound keeps zero runtime dependencies. Pure. import { inflateRawSync } from 'node:zlib'; export interface ZipEntry { name: string; data: Uint8Array } export function unzip(buf: Uint8Array): ZipEntry[] { const dv = new DataView(buf.buffer, buf.byteOffset, buf.byteLength); // end of central directory: signature 0x06054b50 within the last 64 KiB + 22 bytes let eocd = -1; for (let i = buf.length - 22; i >= Math.max(0, buf.length - 65_557); i--) if (dv.getUint32(i, true) === 0x06054b50) { eocd = i; break; } if (eocd < 0) throw new Error('not a zip file (no end of central directory)'); const count = dv.getUint16(eocd + 10, true); let p = dv.getUint32(eocd + 16, true); const out: ZipEntry[] = []; const dec = new TextDecoder(); for (let i = 0; i < count; i++) { if (dv.getUint32(p, true) !== 0x02014b50) throw new Error('zip: bad central directory entry'); const method = dv.getUint16(p + 10, true), size = dv.getUint32(p + 20, true), nameLen = dv.getUint16(p + 28, true); const extraLen = dv.getUint16(p + 30, true), commentLen = dv.getUint16(p + 32, true), local = dv.getUint32(p + 42, true); const name = dec.decode(buf.subarray(p + 46, p + 46 + nameLen)); if (dv.getUint32(local, true) !== 0x04034b50) throw new Error(`zip: bad local header for ${name}`); const start = local + 30 + dv.getUint16(local + 26, true) + dv.getUint16(local + 28, true); const raw = buf.subarray(start, start + size); if (method === 0) out.push({ name, data: raw }); else if (method === 8) out.push({ name, data: new Uint8Array(inflateRawSync(raw)) }); else throw new Error(`zip: ${name} uses compression method ${method}`); p += 46 + nameLen + extraLen + commentLen; } return out; }