Python ZIP压缩
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python ZIP压缩相关的知识,希望对你有一定的参考价值。
ru=lambda x:x.decode(‘u8‘) rp=lambda x:x.replace(‘\\‘,‘/‘) gb=lambda x:x.decode(‘gbk‘) class ZIP: def __init__(s): import zipfile,ntpath,listdir,appuifw s.ZF,s.NP,s.LD,s.A=zipfile.ZipFile,ntpath,listdir,appuifw del zipfile,ntpath,listdir,appuifw s.list,s.zip=[ru(‘请选择文件‘)],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() def run(s,arg,dir,files): for i in files: if s.NP.isdir(dir): if dir[-1]!=dir[2]:dir+=dir[2] if s.NP.isfile(dir+i):s.list.append(rp(dir+i)) def zip_co(s): s.path=s.LD.fm(find=‘dir‘) s.NP.walk(path,run,‘\n‘) ZIP=s.ZF(path+‘1.zip‘,‘w‘,8) for i in s.list: i=i[len(path):] ph=path1.split(‘/‘)[-2]+‘/‘ print ru(‘正在处理:‘+ph+i) try:ZIP.write(path+i,ph+i) except:print ‘error‘;break ZIP.close() print ru(‘操作完成!‘) def dezip(s,ph,run): w=s.ZF(ph,‘r‘) nl=w.namelist() for i in nl:run(w,i) w.close();return nl def zip_de(s): def run(w,i): s.path=‘e:/cai/‘+i r=w.read(i) print ru(‘正在处理:‘+i) ph=s.NP.split(path)[0] if not s.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) print ru(‘操作完成!‘) def zip_del(s): def run(w,i): index=s.LIST.current() r=w.read(i) try:ii=ru(i) except:ii=gb(i) if ii!=s.nl2[index]:zipf.writestr(w.getinfo(i), r) zipf=s.ZF(s.path+‘_1.zip‘,‘w‘) s.dezip(s.path,run) def zip_open(s): s.path=s.LD.fm(find=‘file‘,ext=[‘.zip‘]) s.zip=s.ZF(s.path) try:s.nl2=[ru(i) for i in s.zip.namelist()] except:s.nl2=[gb(i) for i in s.zip.namelist()] s.LIST.set_list(s.nl2) s.menu() def menu(s): if s.zip:s.A.app.menu=[(ru(‘解压‘),s.zip_de),(ru(‘提取‘),s.zip_de),(ru(‘删除‘),s.zip_del),(ru(‘返回‘),s.__init__)] else:s.A.app.menu=[(ru(‘打开‘),s.zip_open),(ru(‘压缩‘),s.zip_co)] zip=ZIP()
以上是关于Python ZIP压缩的主要内容,如果未能解决你的问题,请参考以下文章