ru=LambdaX:x.decode ('U8') RP=LambdaX:x.replace ('\\','/') GB=LambdaX:x.decode ('GBK')classZIP:def __init__(s):ImportZIPFILE,NTPATH,LISTDIR,APPUIFW S.ZF,S.NP,S.LD,S.A=ZipFile. ZIPFILE,NTPATH,LISTDIR,APPUIFWdelZIPFILE,NTPATH,LISTDIR,APPUIFW S.list,s.zip=[ru ('Please select a file')],none S.oldbody=s.a.app.body s.a.app.body=s.list=S.a.listbox (s.list) s.menu () S.L=s.a.e32.ao_lock () S.a.app.exit_key_handler=s.l.signal s.l.wait ()defRun (s,arg,dir,files): forIinchFiles:ifS.np.isdir (dir):ifDIR[-1]!=DIR[2]:d ir+=dir[2] ifS.np.isfile (dir+i): S.list.append (RP (dir+i))defZip_co (s): S.path=s.ld.fm (find='dir') S.np.walk (Path,run,'\ n') ZIP=s.zf (path+'1.zip','W', 8) forIinchs.list:i=I[len (PATH):] ph=path1.split ('/') [ -2]+'/' PrintRu'being processed:'+ph+i)Try: Zip.write (path+i,ph+i)except:Print 'Error'; Breakzip.close ()PrintRu'The operation is complete! ') defDezip (S,ph,run): w=S.ZF (ph,'R') NL=w.namelist () forIinchNl:run (w,i) w.close ();returnNLdefZip_de (s):defRun (w,i): S.path='e:/cai/'+I R=W.read (i)PrintRu'being processed:'+i) ph=s.np.split (path) [0]if nots.np.exists (ph): os.makedirs (ph) open (path,'W'). Write (R) S.path=s.ld.fm (find='file', ext=['. zip']) s.dezip (s.path,run)PrintRu'The operation is complete! ') defZip_del (s):defRun (w,i): Index=s.list.current () R=W.read (i)Try: ii=ru (i)except: ii=GB (i)ifii!=S.nl2[index]:zipf.writestr (W.getinfo (i), R) Zipf=s.zf (s.path+'_1.zip','W') S.dezip (s.path,run)defZip_open (s): S.path=s.ld.fm (find='file', ext=['. zip']) S.zip=S.zf (S.path)Try: S.nl2=[ru (i) forIinchs.zip.namelist ()]except: S.NL2=[GB (i) forIinchs.zip.namelist ()] S.list.set_list (S.NL2) s.menu ()defmenu (s):ifs.zip:s.a.app.menu=[(Ru ('Unzip'), S.zip_de), (Ru ('Extract'), S.zip_de), (Ru ('Delete'), S.zip_del), (Ru ('return'), S.__init__)] Else: s.a.app.menu=[(Ru ('Open'), S.zip_open), (Ru ('Compression'), S.zip_co)]zip=zip ()
Python Zip compression