Files
SupplierSystem/src/utils/zipStore.ts
T
Ubuntu 5fa2bbfd55
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(audit-20261019): T1-T9 审计修复、存储归属与审计日志列宽
- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14)
- T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9)
- T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4)
- T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8)
- T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15)
- T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8)
- T7 前后端测试与 lint 门禁清零
- T8 应付卡片按关联订单日(口径 A)
- T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
2026-10-19 16:16:51 +08:00

125 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
function crc32(data: Uint8Array): number {
let c = 0xffffffff;
for (let i = 0; i < data.length; i += 1) {
c ^= data[i];
for (let k = 0; k < 8; k += 1) {
c = (c >>> 1) ^ (0xedb88320 & -(c & 1));
}
}
return (c ^ 0xffffffff) >>> 0;
}
function u16(n: number): Uint8Array {
const b = new Uint8Array(2);
new DataView(b.buffer).setUint16(0, n, true);
return b;
}
function u32(n: number): Uint8Array {
const b = new Uint8Array(4);
new DataView(b.buffer).setUint32(0, n, true);
return b;
}
function concat(parts: Uint8Array[]): Uint8Array {
const total = parts.reduce((n, p) => n + p.length, 0);
const out = new Uint8Array(total);
let offset = 0;
for (const p of parts) {
out.set(p, offset);
offset += p.length;
}
return out;
}
export function sanitizeZipEntryName(name: string): string {
const base = String(name || 'invoice')
.replace(/[\\/:*?"<>|]+/g, '_')
.replace(/\s+/g, '_')
.slice(0, 80);
return base || 'invoice';
}
/** 不压缩 zip(STORE)。下载后解压即相当于一个文件夹。 */
export function buildZipStore(entries: Array<{ name: string; data: Uint8Array }>): Blob {
const used = new Set<string>();
const files = entries.map((entry, index) => {
let name = sanitizeZipEntryName(entry.name);
if (used.has(name)) name = `${index}_${name}`;
used.add(name);
const nameBytes = new TextEncoder().encode(name);
const crc = crc32(entry.data);
return { nameBytes, data: entry.data, crc };
});
const locals: Uint8Array[] = [];
const centrals: Uint8Array[] = [];
let offset = 0;
for (const file of files) {
const local = concat([
new Uint8Array([0x50, 0x4b, 0x03, 0x04]),
u16(20),
u16(0x0800),
u16(0),
u16(0),
u16(0),
u32(file.crc),
u32(file.data.length),
u32(file.data.length),
u16(file.nameBytes.length),
u16(0),
file.nameBytes,
file.data,
]);
const central = concat([
new Uint8Array([0x50, 0x4b, 0x01, 0x02]),
// BIZ-15:central header `version made by` = Unix(3) + spec 3.0(0x031E),
// 配合 flags 0x0800(UTF-8 文件名)让 unzip 用 UTF-8 解中文名;
// 下面 `version needed` 仍保持 20(不改,避免破坏 zipStore.test.ts 的字节断言)。
u16(0x031e),
u16(20),
u16(0x0800),
u16(0),
u16(0),
u16(0),
u32(file.crc),
u32(file.data.length),
u32(file.data.length),
u16(file.nameBytes.length),
u16(0),
u16(0),
u16(0),
u16(0),
// 外部属性:version made by 声明 Unix(3) 后,unzip 会按高 16 位当 Unix mode;
// 给 0100644(普通文件 rw-r--r--),否则解出的文件 mode=0000 会 EACCES 读不了。
u32(0x81a40000),
u32(offset),
file.nameBytes,
]);
locals.push(local);
centrals.push(central);
offset += local.length;
}
const centralDir = concat(centrals);
const eocd = concat([
new Uint8Array([0x50, 0x4b, 0x05, 0x06]),
u16(0),
u16(0),
u16(files.length),
u16(files.length),
u32(centralDir.length),
u32(offset),
u16(0),
]);
return new Blob([concat([...locals, centralDir, eocd]) as unknown as BlobPart], { type: 'application/zip' });
}
export function triggerBlobDownload(blob: Blob, filename: string) {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
window.setTimeout(() => URL.revokeObjectURL(url), 1000);
}