标签:alt ref bz2 usr exp class copy env www.
递归解压文件夹中的所有压缩包到指定文件夹
python解压rar文件需要安装依赖库 (python-unrar)
Windows:
Linux:
$ make lib
$ make install-lib
代码实现
1 #!/usr/bin/env python3 2 # .zip .rar .tar .tgz .tar.gz .tar.bz2 .tar.bz .tar.tgz 3 import os 4 import zlib 5 import unrar 6 import shutil 7 import zipfile 8 import tarfile 9 from time import sleep 10 # from unrar import rarfile 11 12 filepath = "./dirname" #relative path 13 14 class BaseTool(object): 15 def __init__(self, path): 16 self.path = path 17 self.compress = [".tar.gz",".tar.bz2",".tar.bz",".tar.tgz",".tar",".tgz",".zip",".rar"] 18 19 def iszip(self, file): 20 for z in self.compress: 21 if file.endswith(z): 22 return z 23 24 def zip_to_path(self, file): 25 for i in self.compress: 26 file = file.replace(i,"") 27 return file 28 29 def error_record(self, info): 30 with open("error.txt","a+") as r: 31 r.write(info+"\n") 32 33 def un_zip(self, src, dst): 34 """ src : aa/asdf.zip 35 dst : unzip/aa/asdf.zip 36 """ 37 try: 38 zip_file = zipfile.ZipFile(src) 39 uz_path = self.zip_to_path(dst) 40 if not os.path.exists(uz_path): 41 os.makedirs(uz_path) 42 for name in zip_file.namelist(): 43 zip_file.extract(name, uz_path) 44 zip_file.close() 45 except zipfile.BadZipfile: 46 pass 47 except zlib.error: 48 print("zlib error : "+src) 49 self.error_record("zlib error : "+src) 50 51 def un_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 except unrar.rarfile.BadRarFile: 57 pass 58 except Exception as e: 59 print(e) 60 self.error_record(str(e)+src) 61 62 def un_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 except tarfile.ReadError: 68 pass 69 except Exception as e: 70 print(e) 71 self.error_record(str(e)+src) 72 73 74 class UnZip(BaseTool): 75 """ UnZip files """ 76 def __init__(self, path): 77 super(UnZip, self).__init__(self) 78 self.path = path 79 self.output = "unzip/" 80 self.current_path = os.getcwd()+"/" 81 82 def recursive_unzip(self, repath): 83 """recursive unzip file 84 """ 85 for (root, dirs, files) in os.walk(repath): 86 for filename in files: 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 import rarfile 96 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 def main_unzip(self): 109 for (root, dirs, files) in os.walk(self.path): 110 for filename in files: 111 zippath = os.path.join(self.output,root) 112 if not os.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 import rarfile 121 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 except OSError as e: 130 print(str(e)) 131 self.error_record(str(e)) 132 133 self.recursive_unzip(self.output+self.path) 134 135 136 def main(): 137 z = UnZip(filepath) #relative path 138 z.main_unzip() 139 140 if __name__ == ‘__main__‘: 141 main()
标签:alt ref bz2 usr exp class copy env www.
原文地址:https://www.cnblogs.com/trojan-z/p/10043408.html