Files
SupplierSystem/src/utils/zipStore.ts
T
Ubuntu deb3e72750
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
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

120 lines
3.0 KiB
TypeScript

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]),
u16(20),
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),
u32(0),
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);
}