/* Minimal ZIP writer (only "stored", no compression) - no external library loaded for the zip download, consistent with the principle followed throughout the project of not pulling in CDN/third-party dependencies (see the Methoden-Set chapter on the project website). Uncompressed is unproblematic for a manageable amount of text/image files - correctness over file size. */ (function (global) { "use strict"; const CRC_TABLE = (function () { const table = 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; } table[n] = c >>> 0; } return table; })(); function crc32(bytes) { let crc = 0xffffffff; for (let i = 0; i < bytes.length; i++) { crc = CRC_TABLE[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8); } return (crc ^ 0xffffffff) >>> 0; } function dosDateTime(date) { const time = ((date.getHours() & 0x1f) << 11) | ((date.getMinutes() & 0x3f) << 5) | ((date.getSeconds() >> 1) & 0x1f); const dateVal = (((date.getFullYear() - 1980) & 0x7f) << 9) | (((date.getMonth() + 1) & 0xf) << 5) | (date.getDate() & 0x1f); return { time, date: dateVal }; } function utf8Bytes(str) { return new TextEncoder().encode(str); } /* entries: Array<{ path: string, data: Uint8Array }> - path with "/" as the folder separator, relative to the zip root. Returns a Blob. */ function write(entries) { const now = dosDateTime(new Date()); const chunks = []; const central = []; let offset = 0; for (const entry of entries) { const nameBytes = utf8Bytes(entry.path); const data = entry.data; const crc = crc32(data); const local = new DataView(new ArrayBuffer(30)); local.setUint32(0, 0x04034b50, true); local.setUint16(4, 20, true); // Version local.setUint16(6, 0x0800, true); // Flags: UTF-8 filenames local.setUint16(8, 0, true); // Method: 0 = stored local.setUint16(10, now.time, true); local.setUint16(12, now.date, true); local.setUint32(14, crc, true); local.setUint32(18, data.length, true); local.setUint32(22, data.length, true); local.setUint16(26, nameBytes.length, true); local.setUint16(28, 0, true); chunks.push(new Uint8Array(local.buffer), nameBytes, data); const centralHeader = new DataView(new ArrayBuffer(46)); centralHeader.setUint32(0, 0x02014b50, true); centralHeader.setUint16(4, 20, true); centralHeader.setUint16(6, 20, true); centralHeader.setUint16(8, 0x0800, true); centralHeader.setUint16(10, 0, true); centralHeader.setUint16(12, now.time, true); centralHeader.setUint16(14, now.date, true); centralHeader.setUint32(16, crc, true); centralHeader.setUint32(20, data.length, true); centralHeader.setUint32(24, data.length, true); centralHeader.setUint16(28, nameBytes.length, true); centralHeader.setUint16(30, 0, true); centralHeader.setUint16(32, 0, true); centralHeader.setUint16(34, 0, true); centralHeader.setUint16(36, 0, true); centralHeader.setUint32(38, 0, true); centralHeader.setUint32(42, offset, true); central.push(new Uint8Array(centralHeader.buffer), nameBytes); offset += 30 + nameBytes.length + data.length; } const centralStart = offset; let centralSize = 0; for (const part of central) centralSize += part.length; const end = new DataView(new ArrayBuffer(22)); end.setUint32(0, 0x06054b50, true); end.setUint16(4, 0, true); end.setUint16(6, 0, true); end.setUint16(8, entries.length, true); end.setUint16(10, entries.length, true); end.setUint32(12, centralSize, true); end.setUint32(16, centralStart, true); end.setUint16(20, 0, true); return new Blob([...chunks, ...central, new Uint8Array(end.buffer)], { type: "application/zip" }); } global.AblegerZip = { write }; })(window);