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 importargparse10 importtime11 importthreading12 from time importsleep13 from itertools importchain14 from unrar importrarfile15

16

17 filepath = "./filepath" #relative path

18 thread_num = 1

19

20 classBaseTool(object):21 def __init__(self):22 super(BaseTool, self).__init__()23 self.compress = [".tar.gz",".tar.bz2",".tar.bz",".tar.tgz",".tar",".tgz",".zip",".rar"]24

25 def run_threads(self, threads_number: int, target_function: any, *args, **kwargs) ->None:26 """Run function across specified number of threads27 :param int thread_number: number of threads that should be executed28 :param func target_function: function that should be executed accross specified number of threads29 :param any args: args passed to target_function30 :param any kwargs: kwargs passed to target function31 :return None32 """

33

34 threads =[]35 threads_running =threading.Event()36 threads_running.set()37

38 for thread_id inrange(int(threads_number)):39 thread =threading.Thread(40 target=target_function,41 args=chain((threads_running,), args),42 kwargs=kwargs,43 name="thread-{}".format(thread_id),44 )45 threads.append(thread)46

47 #print("{} thread is starting...".format(thread.name))

48 thread.start()49

50 start =time.time()51 try:52 whilethread.isAlive():53 thread.join(1)54

55 exceptKeyboardInterrupt:56 threads_running.clear()57

58 for thread inthreads:59 thread.join()60 #print("{} thread is terminated.".format(thread.name))

61

62 print("Elapsed time: {} seconds".format(time.time() -start))63

64 defiszip(self, file):65 for z inself.compress:66 iffile.endswith(z):67 returnz68

69 defzip_to_path(self, file):70 for i inself.compress:71 file = file.replace(i,"")72 returnfile73

74 deferror_record(self, info):75 with open("error.txt","a+") as w:76 w.write(info+"\n")77

78 defremove(self, filepath):79 if os.path.exists(self.zip_to_path(filepath)) andos.path.exists(filepath):80 os.remove(filepath)81

82 defun_zip(self, src, dst):83 """src : aa/asdf.zip84 dst : unzip/aa/asdf.zip85 """

86 try:87 zip_file =zipfile.ZipFile(src)88 uz_path =self.zip_to_path(dst)89 if notos.path.exists(uz_path):90 os.makedirs(uz_path)91 for name inzip_file.namelist():92 zip_file.extract(name, uz_path)93 zip_file.close()94 exceptzipfile.BadZipfile:95 pass

96 exceptRuntimeError:97 self.error_record("pass required :"+src)98 return "PassRequired"

99 exceptzlib.error:100 print("zlib error :"+src)101 self.error_record("zlib error :"+src)102 exceptException as e:103 print(e)104 self.error_record(str(e)+src)105

106 defun_rar(self, src, dst):107 try:108 rar =unrar.rarfile.RarFile(src)109 uz_path =self.zip_to_path(dst)110 rar.extractall(uz_path)111 exceptunrar.rarfile.BadRarFile:112 pass

113 exceptException as e:114 print(e)115 self.error_record(str(e)+src)116

117 defun_tar(self, src, dst):118 try:119 tar =tarfile.open(src)120 uz_path =self.zip_to_path(dst)121 tar.extractall(path =uz_path)122 excepttarfile.ReadError:123 pass

124 exceptException as e:125 print(e)126 self.error_record(str(e)+src)127

128

129 classLockedIterator(object):130 def __init__(self, it):131 self.lock =threading.Lock()132 self.it = it.__iter__()133

134 def __iter__(self):135 returnself136

137 defnext(self):138 self.lock.acquire()139 try:140 item =next(self.it)141

142 if type(item) istuple:143 return (item[0].strip(), item[1].strip(), item[2].strip())144 elif type(item) isstr:145 returnitem.strip()146

147 returnitem148 finally:149 self.lock.release()150

151

152 classUnZip(BaseTool):153 """UnZip files"""

154 def __init__(self, path):155 super(UnZip, self).__init__()156 self.path =path157 self.threads =thread_num158 self.output = "./unzip/"

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

160 self.parser =argparse.ArgumentParser()161 self.parser.add_argument("-v","--verbose", action="store_true", help="./zipperpro.py -v")162 self.args =self.parser.parse_args()163

164 defrun(self):165 self.main_unzip(self.path)166

167 defrecursive_unzip(self, repath):168 """recursive unzip file169 """

170 task_list =[]171 for (root, dirs, files) inos.walk(repath):172 for filename infiles:173 filename = filename.strip("./")174 src = os.path.join("./"+root,filename)175 data = (src, src, "child")176 task_list.append(data)177 data =LockedIterator(chain(task_list))178 print("[+] child unzip ...")179 self.run_threads(self.threads, self.do_unzip, data)180

181 defmain_unzip(self, mainpath):182 task_list =[]183 print("Initialization......")184 for (root, dirs, files) inos.walk(mainpath):185 for filename infiles:186 zippath =os.path.join(self.output,root)187 if notos.path.exists(zippath):188 os.makedirs(zippath)189 src =os.path.join(root,filename)190 dst =os.path.join(self.output,root,filename)191 if notos.path.exists(self.zip_to_path(dst)):192 data = ((src, dst, "main"))193 task_list.append(data)194 data =LockedIterator(chain(task_list))195 print("[+] main unzip ...")196 self.run_threads(self.threads, self.do_unzip, data)197 self.recursive_unzip(self.output+self.path)198

199 defdo_unzip(self, running, data):200 whilerunning.is_set():201 try:202 (src, dst, flag) =data.next()203 if flag == "main":204 if self.iszip(src) == ".zip":205 ifself.args.verbose:206 print("[+] main unzip :"+src)207 self.un_zip(src,dst)208 elif self.iszip(src) == ".rar":209 ifself.args.verbose:210 print("[+] main unrar :"+src)211 self.un_rar(src,dst)212 elif self.iszip(src) in (".tar.gz",".tar.bz2",".tar.bz",".tar.tgz",".tar",".tgz"):213 ifself.args.verbose:214 print("[+] main untar :"+src)215 self.un_tar(src,dst)216 else:217 try:218 shutil.copyfile(src,dst)219 exceptOSError as e:220 print(str(e))221 self.error_record(str(e))222 elif flag == "child":223 if self.iszip(src) == ".zip":224 ifself.args.verbose:225 print("[+] child unzip:"+src)226 if not self.un_zip(src, src) == "PassRequired":227 self.remove(src)228 self.recursive_unzip(self.zip_to_path(src))229 sleep(0.1)230 elif self.iszip(src) == ".rar":231 ifself.args.verbose:232 print("[+] child unrar :"+src)233 self.un_rar(src,src)234 self.remove(src)235 self.recursive_unzip(self.zip_to_path(src))236 sleep(0.1)237 elif self.iszip(src) in (".tar.gz",".tar.bz2",".tar.bz",".tar.tgz",".tar",".tgz"):238 ifself.args.verbose:239 print("[+] child untar :"+src)240 self.un_tar(src,src)241 self.remove(src)242 self.recursive_unzip(self.zip_to_path(src))243 sleep(0.1)244

245 exceptStopIteration:246 break

247

248

249 defmain():250 z =UnZip(filepath)251 z.run()252

253

254

255 if __name__ == '__main__':256 main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值