1. 程式人生 > >python遞迴解壓資料夾中所有壓縮包

python遞迴解壓資料夾中所有壓縮包

1. 簡述

    遞迴解壓資料夾中的所有壓縮包到指定資料夾

2. 環境配置

    python解壓rar檔案需要安裝依賴庫 (python-unrar

    Windows:

  1. 在 RARLab 官方下載安裝庫檔案 http://www.rarlab.com/rar/UnRARDLL.exe
  2. 預設路徑偽 C:\Program Files (x86)\UnrarDLL\
  3. 新增環境變數 UNRAR_LIB_PATH 鍵值 C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll,如果是32位就是 C:\Program Files (x86)\UnrarDLL\UnRAR.dll

    Linux:

  1. 下載庫檔案 https://www.rarlab.com/rar/unrarsrc-5.6.8.tar.gz
  2. $ make lib
    $ make install-lib
  3. 新增環境變數  export UNRAR_LIB_PATH=/usr/lib/libunrar.so

3. 實現

    程式碼實現

  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()