# -*- coding: utf-8 -*- """ 评估"预生成每型号 zip"方案的收益与代价。 扫描 web 发布目录的 step/,统计原始总体积 vs zip 后体积。 """ import os, zipfile, io, random WEB = r'D:/开发/OnebotCatalog/release/web_2026.08/step' if not os.path.isdir(WEB): raise SystemExit('找不到 ' + WEB) files = [f for f in os.listdir(WEB) if f.lower().endswith(('.step', '.stp'))] print(f'step 文件数: {len(files)}') tot = sum(os.path.getsize(os.path.join(WEB, f)) for f in files) print(f'原始总体积: {tot/1073741824:.2f} GB') # 抽样测压缩率(避免全量压缩耗时) random.seed(42) sample = random.sample(files, min(40, len(files))) raw = zipped = 0 for f in sample: p = os.path.join(WEB, f) n = os.path.getsize(p) buf = io.BytesIO() with zipfile.ZipFile(buf, 'w', zipfile.ZIP_DEFLATED, compresslevel=6) as z: z.write(p, f[:-4] + '.step' if f.lower().endswith('.stp') else f) raw += n zipped += buf.tell() print(f'\n抽样 {len(sample)} 个:') print(f' 原始 {raw/1048576:.1f} MB -> zip {zipped/1048576:.1f} MB (省 {(1-zipped/raw)*100:.1f}%)') print(f'\n推算全量:') print(f' stepz 目录约 {(tot*(zipped/raw))/1073741824:.2f} GB') print(f' 部署总目录 = step {tot/1073741824:.2f}GB + stepz {(tot*(zipped/raw))/1073741824:.2f}GB' f' = {(tot*(1+zipped/raw))/1073741824:.2f} GB') print(f'\n购物车场景收益 (选 20 个型号, 平均 {tot/len(files)/1048576:.1f}MB/个):') per = tot / len(files) print(f' 现状: 传 {20*per/1048576:.0f} MB 原始文件') print(f' 改后: 传 {20*per*(zipped/raw)/1048576:.0f} MB 压缩文件')