/** * 最小 ZIP 读写(docx / xlsx / pptx 本质都是 zip + xml) * ------------------------------------------------ * 为什么不用现成库:插件在引擎进程里跑,模块解析链不经过 dsh-runtime/node_modules, * 裸 import 'fflate' 会直接失败;打包一份依赖又会让安装包变大。 * ZIP 格式本身够简单,这里用 node:zlib 自己实现,零依赖、可离线。 * * 只实现 Office/PDF 会用到的子集: * - 读:stored(0) / deflate(8),不支持加密、ZIP64、分卷 * - 写:全部 deflate,不写 data descriptor(大小已知) */ import zlib from 'node:zlib' const CRC_TABLE = (() => { const table = new Int32Array(256) for (let i = 0; i < 256; i++) { let c = i for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1 table[i] = c } return table })() function crc32 (buf) { let c = -1 for (let i = 0; i < buf.length; i++) c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8) return (c ^ -1) >>> 0 } /** 读 zip:返回 Map<文件名, Buffer>。找不到 EOCD 直接抛,避免把损坏文件当空包。 */ export function unzip (buf) { // EOCD 在文件尾部,最多带 64KB 注释 let eocd = -1 const start = Math.max(0, buf.length - 65557) for (let i = buf.length - 22; i >= start; i--) { if (buf.readUInt32LE(i) === 0x06054b50) { eocd = i; break } } if (eocd < 0) throw new Error('不是有效的 zip/office 文件(找不到目录结尾标记)') const count = buf.readUInt16LE(eocd + 10) let offset = buf.readUInt32LE(eocd + 16) const out = new Map() for (let i = 0; i < count; i++) { if (buf.readUInt32LE(offset) !== 0x02014b50) break const method = buf.readUInt16LE(offset + 10) const compSize = buf.readUInt32LE(offset + 20) const nameLen = buf.readUInt16LE(offset + 28) const extraLen = buf.readUInt16LE(offset + 30) const commentLen = buf.readUInt16LE(offset + 32) const localOffset = buf.readUInt32LE(offset + 42) const name = buf.toString('utf8', offset + 46, offset + 46 + nameLen) offset += 46 + nameLen + extraLen + commentLen if (name.endsWith('/')) continue // 本地头里的 extra 长度可能和中央目录不同,必须重新读 const lNameLen = buf.readUInt16LE(localOffset + 26) const lExtraLen = buf.readUInt16LE(localOffset + 28) const dataStart = localOffset + 30 + lNameLen + lExtraLen const raw = buf.subarray(dataStart, dataStart + compSize) try { out.set(name, method === 0 ? Buffer.from(raw) : zlib.inflateRawSync(raw)) } catch { // 单个部件坏掉不该让整个文件读不了 } } return out } /** 写 zip:entries 可以是 Map 或 [name, content] 数组,内容支持 string / Buffer。 */ export function zip (entries) { const list = entries instanceof Map ? [...entries.entries()] : entries const chunks = [] const central = [] let offset = 0 for (const [name, content] of list) { const data = Buffer.isBuffer(content) ? content : Buffer.from(String(content), 'utf8') const nameBuf = Buffer.from(name, 'utf8') const deflated = zlib.deflateRawSync(data, { level: 9 }) // 压不小就存原文,docx 里的图片之类压缩率经常是 100% const useDeflate = deflated.length < data.length const body = useDeflate ? deflated : data const method = useDeflate ? 8 : 0 const crc = crc32(data) const local = Buffer.alloc(30) local.writeUInt32LE(0x04034b50, 0) local.writeUInt16LE(20, 4) // version needed local.writeUInt16LE(0x0800, 6) // UTF-8 文件名 local.writeUInt16LE(method, 8) local.writeUInt16LE(0, 10) // time local.writeUInt16LE(0x21, 12) // date: 1980-01-01(固定值保证可复现) local.writeUInt32LE(crc, 14) local.writeUInt32LE(body.length, 18) local.writeUInt32LE(data.length, 22) local.writeUInt16LE(nameBuf.length, 26) local.writeUInt16LE(0, 28) chunks.push(local, nameBuf, body) const centralEntry = Buffer.alloc(46) centralEntry.writeUInt32LE(0x02014b50, 0) centralEntry.writeUInt16LE(20, 4) centralEntry.writeUInt16LE(20, 6) centralEntry.writeUInt16LE(0x0800, 8) centralEntry.writeUInt16LE(method, 10) centralEntry.writeUInt16LE(0, 12) centralEntry.writeUInt16LE(0x21, 14) centralEntry.writeUInt32LE(crc, 16) centralEntry.writeUInt32LE(body.length, 20) centralEntry.writeUInt32LE(data.length, 24) centralEntry.writeUInt16LE(nameBuf.length, 28) centralEntry.writeUInt32LE(offset, 42) central.push(centralEntry, nameBuf) offset += local.length + nameBuf.length + body.length } const centralBuf = Buffer.concat(central) const eocd = Buffer.alloc(22) eocd.writeUInt32LE(0x06054b50, 0) eocd.writeUInt16LE(list.length, 8) eocd.writeUInt16LE(list.length, 10) eocd.writeUInt32LE(centralBuf.length, 12) eocd.writeUInt32LE(offset, 16) return Buffer.concat([...chunks, centralBuf, eocd]) }