'use strict'; /** * 极简 ZIP 打包(deflate,无第三方依赖) * * 为什么不用 PowerShell 的 `Compress-Archive` / `ZipFile::CreateFromDirectory`: * - PowerShell 5.1(.NET Framework)写出来的条目名是**反斜杠**(`src\content.js`), * 而 ZIP 规范要求 `/`,商店上传/解压工具会把它当成带 \ 的怪文件名; * - 走 8.3 短路径(`C:\Users\LFMING~1\...`)时,`FullName.Substring($src.Length + 1)` * 算出来的相对路径还会多一个前导 `/`,manifest.json 就不在根目录了。 * * 这里自己写:条目名一律 `/`、无前导斜杠、UTF-8 文件名、Deflate 压缩。 * 只支持「单文件 < 4GB、总长 < 4GB」的普通包(插件包够用,不需要 ZIP64)。 */ const fs = require('node:fs'); const path = require('node:path'); const zlib = require('node:zlib'); const CRC_TABLE = (() => { const table = new Int32Array(256); for (let index = 0; index < 256; index++) { let value = index; for (let bit = 0; bit < 8; bit++) { value = value & 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1; } table[index] = value; } return table; })(); function crc32(buffer) { let crc = -1; for (let index = 0; index < buffer.length; index++) { crc = CRC_TABLE[(crc ^ buffer[index]) & 0xff] ^ (crc >>> 8); } return (crc ^ -1) >>> 0; } /** 递归列出文件(返回相对路径,统一用 `/`) */ function listFiles(dir, base = dir, files = []) { for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { const full = path.join(dir, entry.name); if (entry.isDirectory()) { listFiles(full, base, files); } else if (entry.isFile()) { files.push({ full, name: path.relative(base, full).split(path.sep).join('/') }); } } return files; } /** 转成 DOS 时间(ZIP 头里是 1980 起的本地时间) */ function toDosTime(date) { const year = Math.max(date.getFullYear(), 1980); return { time: (date.getHours() << 11) | (date.getMinutes() << 5) | (date.getSeconds() >> 1), date: ((year - 1980) << 9) | ((date.getMonth() + 1) << 5) | date.getDate(), }; } /** * 把目录打成 zip * @param {string} dir 源目录 * @param {string} zipPath 目标 zip(不存在会自动创建父目录) * @returns {{ files: number, bytes: number }} 条目数与压缩包大小 */ function zipDirectory(dir, zipPath) { const files = listFiles(dir).sort((a, b) => a.name.localeCompare(b.name)); const localParts = []; const centralParts = []; let offset = 0; for (const file of files) { const data = fs.readFileSync(file.full); const deflated = zlib.deflateRawSync(data, { level: 9 }); const name = Buffer.from(file.name, 'utf8'); const crc = crc32(data); const { time, date } = toDosTime(fs.statSync(file.full).mtime); const local = Buffer.alloc(30); local.writeUInt32LE(0x04034b50, 0); // 局部文件头签名 local.writeUInt16LE(20, 4); // 解压所需版本 local.writeUInt16LE(0x0800, 6); // 文件名 UTF-8 local.writeUInt16LE(8, 8); // Deflate local.writeUInt16LE(time, 10); local.writeUInt16LE(date, 12); local.writeUInt32LE(crc, 14); local.writeUInt32LE(deflated.length, 18); local.writeUInt32LE(data.length, 22); local.writeUInt16LE(name.length, 26); local.writeUInt16LE(0, 28); // 无扩展字段 localParts.push(local, name, deflated); const central = Buffer.alloc(46); central.writeUInt32LE(0x02014b50, 0); // 中央目录签名 central.writeUInt16LE(20, 4); // 打包者版本 central.writeUInt16LE(20, 6); // 解压所需版本 central.writeUInt16LE(0x0800, 8); // 文件名 UTF-8 central.writeUInt16LE(8, 10); // Deflate central.writeUInt16LE(time, 12); central.writeUInt16LE(date, 14); central.writeUInt32LE(crc, 16); central.writeUInt32LE(deflated.length, 20); central.writeUInt32LE(data.length, 24); central.writeUInt16LE(name.length, 28); central.writeUInt16LE(0, 30); // 扩展字段长度 central.writeUInt16LE(0, 32); // 注释长度 central.writeUInt16LE(0, 34); // 起始磁盘号 central.writeUInt16LE(0, 36); // 内部属性 central.writeUInt32LE(0, 38); // 外部属性 central.writeUInt32LE(offset, 42); // 局部头偏移 centralParts.push(central, name); offset += local.length + name.length + deflated.length; } const centralBuffer = Buffer.concat(centralParts); const end = Buffer.alloc(22); end.writeUInt32LE(0x06054b50, 0); // 目录结束签名 end.writeUInt16LE(0, 4); // 本磁盘号 end.writeUInt16LE(0, 6); // 中央目录起始磁盘 end.writeUInt16LE(files.length, 8); end.writeUInt16LE(files.length, 10); end.writeUInt32LE(centralBuffer.length, 12); end.writeUInt32LE(offset, 16); end.writeUInt16LE(0, 20); // 注释长度 fs.mkdirSync(path.dirname(zipPath), { recursive: true }); fs.writeFileSync(zipPath, Buffer.concat([ ...localParts, centralBuffer, end ])); return { files: files.length, bytes: fs.statSync(zipPath).size }; } module.exports = { zipDirectory };