/ magland / rip-sound-analyzer
Sign in
magland / rip-sound-analyzer
rip-sound-analyzer / src / storage / zip.ts
112 lines · 4.3 KBCodeBlameHistory
bf56870Export/import the whole dataset; filtered waveform by defaultJeremy Magland 1// Minimal uncompressed ("stored") zip writer and reader. Enough for dataset files:
2// audio is already raw float samples and would barely compress anyway.
4const CRC_TABLE = (() => {
5 const t = new Uint32Array(256);
6 for (let n = 0; n < 256; n++) {
7 let c = n;
8 for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
9 t[n] = c >>> 0;
10 }
11 return t;
12})();
14export function crc32(data: Uint8Array): number {
15 let c = 0xffffffff;
16 for (let i = 0; i < data.length; i++) c = CRC_TABLE[(c ^ data[i]) & 0xff] ^ (c >>> 8);
17 return (c ^ 0xffffffff) >>> 0;
20export interface ZipEntry {
21 name: string;
22 data: Uint8Array;
25export function writeZip(entries: ZipEntry[]): Blob {
26 const enc = new TextEncoder();
27 const now = new Date();
28 const dosTime = (now.getHours() << 11) | (now.getMinutes() << 5) | (now.getSeconds() >> 1);
29 const dosDate = ((now.getFullYear() - 1980) << 9) | ((now.getMonth() + 1) << 5) | now.getDate();
30 const parts: BlobPart[] = [];
31 const central: Uint8Array[] = [];
32 let offset = 0;
33 for (const e of entries) {
34 const name = enc.encode(e.name);
35 const crc = crc32(e.data);
36 const local = new DataView(new ArrayBuffer(30));
37 local.setUint32(0, 0x04034b50, true);
38 local.setUint16(4, 20, true); // version needed
39 local.setUint16(6, 0x0800, true); // UTF-8 names
40 local.setUint16(8, 0, true); // stored
41 local.setUint16(10, dosTime, true);
42 local.setUint16(12, dosDate, true);
43 local.setUint32(14, crc, true);
44 local.setUint32(18, e.data.length, true);
45 local.setUint32(22, e.data.length, true);
46 local.setUint16(26, name.length, true);
47 local.setUint16(28, 0, true);
48 parts.push(local.buffer, name as Uint8Array<ArrayBuffer>, e.data as Uint8Array<ArrayBuffer>);
50 const c = new DataView(new ArrayBuffer(46 + name.length));
51 c.setUint32(0, 0x02014b50, true);
52 c.setUint16(4, 20, true);
53 c.setUint16(6, 20, true);
54 c.setUint16(8, 0x0800, true);
55 c.setUint16(10, 0, true);
56 c.setUint16(12, dosTime, true);
57 c.setUint16(14, dosDate, true);
58 c.setUint32(16, crc, true);
59 c.setUint32(20, e.data.length, true);
60 c.setUint32(24, e.data.length, true);
61 c.setUint16(28, name.length, true);
62 c.setUint32(42, offset, true);
63 new Uint8Array(c.buffer).set(name, 46);
64 central.push(new Uint8Array(c.buffer));
65 offset += 30 + name.length + e.data.length;
66 }
67 const cdSize = central.reduce((s, c) => s + c.length, 0);
68 const end = new DataView(new ArrayBuffer(22));
69 end.setUint32(0, 0x06054b50, true);
70 end.setUint16(8, entries.length, true);
71 end.setUint16(10, entries.length, true);
72 end.setUint32(12, cdSize, true);
73 end.setUint32(16, offset, true);
74 return new Blob([...parts, ...(central as Uint8Array<ArrayBuffer>[]), end.buffer], { type: "application/zip" });
77// Reads stored (uncompressed) entries; throws on anything else.
78export function readZip(buf: ArrayBuffer): Map<string, Uint8Array> {
79 const v = new DataView(buf);
80 const bytes = new Uint8Array(buf);
81 const dec = new TextDecoder();
82 let eocd = -1;
83 for (let i = buf.byteLength - 22; i >= Math.max(0, buf.byteLength - 22 - 65535); i--) {
84 if (v.getUint32(i, true) === 0x06054b50) {
85 eocd = i;
86 break;
87 }
88 }
89 if (eocd < 0) throw new Error("Not a zip file.");
90 const count = v.getUint16(eocd + 10, true);
91 let p = v.getUint32(eocd + 16, true);
92 const out = new Map<string, Uint8Array>();
93 for (let i = 0; i < count; i++) {
94 if (v.getUint32(p, true) !== 0x02014b50) throw new Error("Damaged zip file.");
95 const method = v.getUint16(p + 10, true);
96 const crc = v.getUint32(p + 16, true);
97 const size = v.getUint32(p + 20, true);
98 const nameLen = v.getUint16(p + 28, true);
99 const extraLen = v.getUint16(p + 30, true);
100 const commentLen = v.getUint16(p + 32, true);
101 const local = v.getUint32(p + 42, true);
102 const name = dec.decode(bytes.subarray(p + 46, p + 46 + nameLen));
103 p += 46 + nameLen + extraLen + commentLen;
104 if (name.endsWith("/")) continue;
105 if (method !== 0) throw new Error(`“${name}” is compressed. Use a dataset file exported by this app, without re-zipping it.`);
106 const start = local + 30 + v.getUint16(local + 26, true) + v.getUint16(local + 28, true);
107 const data = bytes.subarray(start, start + size);
108 if (crc32(data) !== crc) throw new Error(`“${name}” is damaged (checksum mismatch).`);
109 out.set(name, data);
110 }
111 return out;
↑↓ move↵openescclose