from pathlib import Path
import tempfile,zipfile,hashlib,json
ROOT=Path(__file__).resolve().parent
names=['index.html','site.css','logo.svg'];source={n:(ROOT/'public'/n).read_bytes() for n in names}
with tempfile.TemporaryDirectory(prefix='mbsky-owned-restore-') as folder:
base=Path(folder).resolve();archive=base/'example.zip';destination=base/'restored';destination.mkdir()
with zipfile.ZipFile(archive,'w',zipfile.ZIP_DEFLATED) as z:
for name,data in source.items():z.writestr(name,data)
with zipfile.ZipFile(archive) as z:
assert z.testzip() is None
for info in z.infolist():
target=(destination/info.filename).resolve();assert target.is_relative_to(destination.resolve()) and not target.exists();target.write_bytes(z.read(info))
assert all((destination/n).read_bytes()==b for n,b in source.items())
print(json.dumps({'restored':[{'file':n,'sha256':hashlib.sha256(b).hexdigest()} for n,b in source.items()],'scope':'owned three-file local restore, not vendor daily backup SLA'},ensure_ascii=False))
本例需在 本地Python3.12/SQLite;PHP8.4.27 -n;回环HTTP 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地Python3.12/SQLite;PHP8.4.27 -n;回环HTTP
入口文件:projects/hosting-lab/backup_restore.py
预期结果与验证条件观察提示
- ZIP CRC正常,三份恢复文件与原字节相同
- 不代表第三方每日备份SLA
请结合实际环境逐项观察。
附件与关联工程包下载
课程关联工程包(登记关联11章) · 含当前章节或实例 · mbskycom202611a516.zip