from pathlib import Path import zipfile,json,hashlib,csv,io,sys,platform root=Path('public/downloads/blog/archive-restore-20261123'); root.mkdir(parents=True,exist_ok=False) lab=Path('.recovery/archive-restore-20261123'); lab.mkdir(parents=True,exist_ok=False) old='item,qty,unit_price,total\nAdapter,12,15000,180000\n' new='item,qty,unit_price,total\nAdapter,15,15000,225000\n' def pack(path,files): with zipfile.ZipFile(path,'x',compression=zipfile.ZIP_DEFLATED) as z: for n,b in files.items(): info=zipfile.ZipInfo(n,(2026,11,23,9,0,0)); info.compress_type=zipfile.ZIP_DEFLATED; z.writestr(info,b.encode('utf-8')) def digest(p): return hashlib.sha256(p.read_bytes()).hexdigest() def read_zip(p): with zipfile.ZipFile(p) as z: return {n:z.read(n).decode('utf-8') for n in z.namelist()} def extract(p,d): d.mkdir() with zipfile.ZipFile(p) as z: z.extractall(d) # Only locally generated fixed filenames are extracted; this is not a safe-unzip implementation for untrusted input. pack(root/'received.zip',{'quote.csv':old,'delivery.txt':'Delivery: Monday\n'}) before=digest(root/'received.zip') extract(root/'received.zip',lab/'working') (lab/'working'/'quote.csv').write_bytes(new.encode('utf-8')) extract(root/'received.zip',lab/'received-again') pack(root/'outgoing.zip',{p.name:p.read_text(encoding='utf-8') for p in sorted((lab/'working').iterdir())}) extract(root/'outgoing.zip',lab/'outgoing-check') original=read_zip(root/'received.zip'); sent=read_zip(root/'outgoing.zip') zip_result={'originalArchiveUnchanged':before==digest(root/'received.zip'),'workingQuantity':15,'originalReextractedQuantity':int(next(csv.DictReader(io.StringIO(original['quote.csv'])))['qty']),'outgoingReextractedQuantity':int(next(csv.DictReader(io.StringIO(sent['quote.csv'])))['qty']),'workingAndOutgoingFileHashEqual':digest(lab/'working'/'quote.csv')==digest(lab/'outgoing-check'/'quote.csv')} assert zip_result['originalArchiveUnchanged'] and zip_result['originalReextractedQuantity']==12 and zip_result['outgoingReextractedQuantity']==15 and zip_result['workingAndOutgoingFileHashEqual'] # Two snapshots, then restoration to empty folders without altering the working originals. a={'meeting.txt':'Decision: send revision A on Monday.\n','quote.csv':old} b={'meeting.txt':'Decision: send revision B on Wednesday.\n','quote.csv':new,'drawing.csv':'part,width_mm,height_mm\nPanel,300,200\n'} pack(root/'backup-before.zip',a); pack(root/'backup-after.zip',b) extract(root/'backup-before.zip',lab/'restore-before');extract(root/'backup-after.zip',lab/'restore-after') restore_result=[] for name,expected in b.items(): earlier=lab/'restore-before'/name; restored=lab/'restore-after'/name restore_result.append({'file':name,'beforePresent':earlier.exists(),'beforeText':earlier.read_text(encoding='utf-8') if earlier.exists() else None,'afterPresent':restored.exists(),'afterText':restored.read_text(encoding='utf-8'),'afterSha256':digest(restored),'matchesExpectedBytes':restored.read_bytes()==expected.encode('utf-8')}) assert all(x['matchesExpectedBytes'] for x in restore_result) report={'editorialDate':'2026-11-23','environment':{'python':sys.version.split()[0],'platform':platform.system(),'archiveLibrary':'Python zipfile'},'scope':'Locally generated fictional UTF-8 text and CSV; no Windows Explorer temporary-file editing, Word recovery, File History, cloud sync or external-drive test.','zipRevision':zip_result,'restoration':restore_result,'archives':[{ 'file':p.name,'bytes':p.stat().st_size,'sha256':digest(p),'members':list(read_zip(p))} for p in sorted(root.glob('*.zip'))]} (root/'observations.json').write_text(json.dumps(report,ensure_ascii=False,indent=2)+'\n',encoding='utf-8') (root/'README.txt').write_text('Fictional samples created 2026-11-23 by ToffeeLab.\nreceived.zip: original quantity 12. outgoing.zip: revised quantity 15.\nbackup-before.zip: 2 files, earlier contents. backup-after.zip: 3 files, revised contents.\nText and CSV files are UTF-8. No real transaction, personal data or executable files.\nSee observations.json for exact results and scope.\n',encoding='utf-8') print(json.dumps(report,ensure_ascii=False))