import rarfile,zipfile,os,shutilfrompathlib import Path
basePath="d:/basePath"outPath="d:/outPath"passlist=[]
with open("pass.txt","r") asf:for line inf.readlines():
passlist.append(line.rstrip())for root,dirs,fs inos.walk(basePath):for f infs:
filename=os.path.join(root,f)
type=os.path.splitext(filename)[-1][1:]if type=="rar":
fileget=rarfile.RarFile(filename)
with filegetasrf:ifrf.needs_password():#判断是否需要密码for pwds inpasslist:try:
fileget.extractall(outPath,pwd=pwds.encode())#不要直接用pwds,要编码一下
print(filename+":"+pwds)
except:
passelse:
fileget.extractall(outPath)
elif type=="zip":
with zipfile.ZipFile(filename,"r") aszf:for info inzf.infolist():try:
newname=info.filename.encode("cp437").decode("gbk");
except:try:
newname=info.filename.encode("cp437").decode("utf-8");
except:
newname=info.filename
outname=newname.split("/")
l=len(outname)if outname[l-1]!="":#如果是文件if info.flag_bits & 0x01:#如果文件有密码for pwd inpasslist:try:
body=zf.read(info,pwd=pwd.encode())
print("pass:"+pwd)
with open(outPath+"/"+outname[l-1],"wb") asoutfile:
outfile.write(body)
except:
passelse:
with open(outPath+"/"+outname[l-1],"wb") asoutfile:#要把中文的zip解压出中文,就不要用extract了,在新位置创建中文名文件,然后把读取出来的数据写进去就可以。
outfile.write(zf.read(info))else:#如果是文件,直接复制到新位置
shutil.copy(filename,outPath+"\"+f)