python递归解压文件_python递归解压文件夹中所有压缩包

1 #!/usr/bin/env python3

2 #.zip .rar .tar .tgz .tar.gz .tar.bz2 .tar.bz .tar.tgz

3 importos4 importzlib5 importunrar6 importshutil7 importzipfile8 importtarfile9 from time importsleep10 #from unrar import rarfile

11

12 filepath = "./dirname" #relative path

13

14 classBaseTool(object):15 def __init__(self, path):16 self.path =path17 self.compress = [".tar.gz",".tar.bz2",".tar.bz",".tar.tgz",".tar",".tgz",".zip",".rar"]18

19 defiszip(self, file):20 for z inself.compress:21 iffile.endswith(z):22 returnz23

24 defzip_to_path(self, file):25 for i inself.compress:26 file = file.replace(i,"")27 returnfile28

29 deferror_record(self, info):30 with open("error.txt","a+") as r:31 r.write(info+"\n")32

33 defun_zip(self, src, dst):34 """src : aa/asdf.zip35 dst : unzip/aa/asdf.zip36 """

37 try:38 zip_file =zipfile.ZipFile(src)39 uz_path =self.zip_to_path(dst)40 if notos.path.exists(uz_path):41 os.makedirs(uz_path)42 for name inzip_file.namelist():43 zip_file.extract(name, uz_path)44 zip_file.close()45 exceptzipfile.BadZipfile:46 pass

47 exceptzlib.error:48 print("zlib error :"+src)49 self.error_record("zlib error :"+src)50

51 defun_rar(self, src, dst):52 try:53 rar =unrar.rarfile.RarFile(src)54 uz_path =self.zip_to_path(dst)55 rar.extractall(uz_path)56 exceptunrar.rarfile.BadRarFile:57 pass

58 exceptException as e:59 print(e)60 self.error_record(str(e)+src)61

62 defun_tar(self, src, dst):63 try:64 tar =tarfile.open(src)65 uz_path =self.zip_to_path(dst)66 tar.extractall(path =uz_path)67 excepttarfile.ReadError:68 pass

69 exceptException as e:70 print(e)71 self.error_record(str(e)+src)72

73

74 classUnZip(BaseTool):75 """UnZip files"""

76 def __init__(self, path):77 super(UnZip, self).__init__(self)78 self.path =path79 self.output = "unzip/"

80 self.current_path = os.getcwd()+"/"

81

82 defrecursive_unzip(self, repath):83 """recursive unzip file84 """

85 for (root, dirs, files) inos.walk(repath):86 for filename infiles:87 src =os.path.join(root,filename)88 if self.iszip(src) == ".zip":89 print("[+] child unzip:"+src)90 self.un_zip(src, src)91 os.remove(src)92 self.recursive_unzip(self.zip_to_path(src))93 sleep(0.1)94 if self.iszip(src) == ".rar":95 from unrar importrarfile96 print("[+] child unrar :"+src)97 self.un_rar(src,src)98 os.remove(src)99 self.recursive_unzip(self.zip_to_path(src))100 sleep(0.1)101 if self.iszip(src) in (".tar.gz",".tar.bz2",".tar.bz",".tar.tgz",".tar",".tgz"):102 print("[+] child untar :"+src)103 self.un_tar(src,src)104 os.remove(src)105 self.recursive_unzip(self.zip_to_path(src))106 sleep(0.1)107

108 defmain_unzip(self):109 for (root, dirs, files) inos.walk(self.path):110 for filename infiles:111 zippath =os.path.join(self.output,root)112 if notos.path.exists(zippath):113 os.makedirs(zippath)114 src =os.path.join(root,filename)115 dst =os.path.join(self.output,root,filename)116 if self.iszip(src) == ".zip":117 print("[+] main unzip :"+src)118 self.un_zip(src,dst)119 if self.iszip(src) == ".rar":120 from unrar importrarfile121 print("[+] main unrar :"+src)122 self.un_rar(src,dst)123 if self.iszip(src) in (".tar.gz",".tar.bz2",".tar.bz",".tar.tgz",".tar",".tgz"):124 print("[+] main untar :"+src)125 self.un_tar(src,dst)126 else:127 try:128 shutil.copyfile(src,dst)129 exceptOSError as e:130 print(str(e))131 self.error_record(str(e))132

133 self.recursive_unzip(self.output+self.path)134

135

136 defmain():137 z = UnZip(filepath) #relative path

138 z.main_unzip()139

140 if __name__ == ‘__main__‘:141 main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值