export type ZipEntryInput = { path: string; data: string | Uint8Array; modifiedAt?: Date; contentType?: string; }; type NormalizedZipEntry = { path: string; data: Uint8Array; crc32: number; modifiedAt: Date; }; const UTF8_FLAG = 1 << 11; const STORE_METHOD = 0; const ZIP_VERSION_NEEDED = 20; const ZIP_VERSION_MADE_BY = 20; export function createStoredZipBuffer(entries: readonly ZipEntryInput[]): Buffer { if (entries.length === 0) { throw new Error("ZIP bundle requires at least one entry."); } const normalized = entries.map(normalizeEntry); const seen = new Set(); for (const entry of normalized) { if (seen.has(entry.path)) { throw new Error(`Duplicate ZIP entry path: ${entry.path}`); } seen.add(entry.path); } const localParts: Buffer[] = []; const centralParts: Buffer[] = []; let offset = 0; for (const entry of normalized) { const fileName = Buffer.from(entry.path, "utf8"); const localHeader = buildLocalFileHeader(entry, fileName); localParts.push(localHeader, Buffer.from(entry.data)); centralParts.push(buildCentralDirectoryHeader(entry, fileName, offset)); offset += localHeader.byteLength + entry.data.byteLength; } const centralDirectory = Buffer.concat(centralParts); const end = buildEndOfCentralDirectory(normalized.length, centralDirectory.byteLength, offset); return Buffer.concat([...localParts, centralDirectory, end]); } function normalizeEntry(entry: ZipEntryInput): NormalizedZipEntry { const path = normalizeZipPath(entry.path); const data = typeof entry.data === "string" ? new TextEncoder().encode(entry.data) : entry.data; return { path, data, crc32: crc32(data), modifiedAt: entry.modifiedAt ?? new Date(), }; } function normalizeZipPath(path: string): string { const normalized = path.replace(/\\/g, "/").replace(/^\/+/, "").replace(/\/+/g, "/").trim(); if (!normalized || normalized.endsWith("/") || normalized.split("/").some((part) => part === "." || part === ".." || part.length === 0)) { throw new Error(`Invalid ZIP entry path: ${path}`); } return normalized; } function buildLocalFileHeader(entry: NormalizedZipEntry, fileName: Buffer): Buffer { const header = Buffer.alloc(30); const { dosTime, dosDate } = toDosDateTime(entry.modifiedAt); header.writeUInt32LE(0x04034b50, 0); header.writeUInt16LE(ZIP_VERSION_NEEDED, 4); header.writeUInt16LE(UTF8_FLAG, 6); header.writeUInt16LE(STORE_METHOD, 8); header.writeUInt16LE(dosTime, 10); header.writeUInt16LE(dosDate, 12); header.writeUInt32LE(entry.crc32, 14); header.writeUInt32LE(entry.data.byteLength, 18); header.writeUInt32LE(entry.data.byteLength, 22); header.writeUInt16LE(fileName.byteLength, 26); header.writeUInt16LE(0, 28); return Buffer.concat([header, fileName]); } function buildCentralDirectoryHeader(entry: NormalizedZipEntry, fileName: Buffer, localHeaderOffset: number): Buffer { const header = Buffer.alloc(46); const { dosTime, dosDate } = toDosDateTime(entry.modifiedAt); header.writeUInt32LE(0x02014b50, 0); header.writeUInt16LE(ZIP_VERSION_MADE_BY, 4); header.writeUInt16LE(ZIP_VERSION_NEEDED, 6); header.writeUInt16LE(UTF8_FLAG, 8); header.writeUInt16LE(STORE_METHOD, 10); header.writeUInt16LE(dosTime, 12); header.writeUInt16LE(dosDate, 14); header.writeUInt32LE(entry.crc32, 16); header.writeUInt32LE(entry.data.byteLength, 20); header.writeUInt32LE(entry.data.byteLength, 24); header.writeUInt16LE(fileName.byteLength, 28); header.writeUInt16LE(0, 30); header.writeUInt16LE(0, 32); header.writeUInt16LE(0, 34); header.writeUInt16LE(0, 36); header.writeUInt32LE(0, 38); header.writeUInt32LE(localHeaderOffset, 42); return Buffer.concat([header, fileName]); } function buildEndOfCentralDirectory(entryCount: number, centralDirectorySize: number, centralDirectoryOffset: number): Buffer { const end = Buffer.alloc(22); end.writeUInt32LE(0x06054b50, 0); end.writeUInt16LE(0, 4); end.writeUInt16LE(0, 6); end.writeUInt16LE(entryCount, 8); end.writeUInt16LE(entryCount, 10); end.writeUInt32LE(centralDirectorySize, 12); end.writeUInt32LE(centralDirectoryOffset, 16); end.writeUInt16LE(0, 20); return end; } function toDosDateTime(date: Date): { dosTime: number; dosDate: number } { const year = Math.max(1980, date.getUTCFullYear()); const month = date.getUTCMonth() + 1; const day = date.getUTCDate(); const hours = date.getUTCHours(); const minutes = date.getUTCMinutes(); const seconds = Math.floor(date.getUTCSeconds() / 2); return { dosTime: (hours << 11) | (minutes << 5) | seconds, dosDate: ((year - 1980) << 9) | (month << 5) | day, }; } function crc32(data: Uint8Array): number { let crc = 0xffffffff; for (const byte of data) { crc = (crc >>> 8) ^ CRC32_TABLE[(crc ^ byte) & 0xff]; } return (crc ^ 0xffffffff) >>> 0; } const CRC32_TABLE = new Uint32Array( Array.from({ length: 256 }, (_, index) => { let value = index; for (let bit = 0; bit < 8; bit += 1) { value = value & 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1; } return value >>> 0; }), );