Python递归解压缩多级.zip压缩包
参考如下代码(from:https://stackoverflow.com/questions/36285502/how-to-extract-zip-file-recursively-in-python):
import os import io import zipfile def extract(filename): z = zipfile.ZipFile(filename) for f in z.namelist(): # get directory name from file dirname = os.path.splitext(f)[0] # create new directory os.mkdir(dirname) # read inner zip file into bytes buffer content = io.BytesIO(z.read(f)) zip_file = zipfile.ZipFile(content) for i in zip_file.namelist(): zip_file.extract(i, dirname)
封装到class
import os import shutil from BetaLogAnalysis.settings import ZIP_FILES_DIR, EXTRACT_DIR class ExtractArchive(object): """ 递归解压缩.zip文件""" def __init__(self, zip_file_dir, extract_dir): """ @zip_file_dir: .zip格式的压缩包文件所在目录 @extract_dir: name.zip文件将被提取到extract/name """ self.zip_file_dir = zip_file_dir self.extract_dir = extract_dir def run(self): for root, dirs, files in os.walk(self.zip_file_dir): for filename in files: if filename.endswith(".zip"): # 创建跟压缩包同名的文件夹 os.mkdir(os.path.join(self.extract_dir, filename[:-4])) zip_file = os.path.join(self.zip_file_dir, filename) to_folder = os.path.join(self.extract_dir, filename[:-4]) self.extract_zip(zip_file, to_folder) def extract_zip(self, zip_file, to_folder): """ 递归地提取格式为.zip的压缩包内的所有文件并在提 取后将原文件删除 @zip_file: .zip格式的压缩包文件 @to_folder: 将文件提取到此处 """ # 解压 shutil.unpack_archive(zip_file, to_folder) # 将原文件删除 os.remove(zip_file) # 遍历 to_folder for root, dirs, files in os.walk(to_folder): for filename in files: if filename.endswith(".zip"): os.mkdir(os.path.join(to_folder, filename[:-4])) to_folder = os.path.join(to_folder, filename[:-4]) zip_file = os.path.join(root, filename) self.extract_zip(zip_file, to_folder) if __name__ == '__main__': e = ExtractArchive(ZIP_FILES_DIR, EXTRACT_DIR) e.run()
尚存在的问题:
中文乱码
名字过长报错: FileNotFoundError: [WinError 206] 文件名或扩展名太长。
如何您也在此处遇到问题,欢迎一起参与讨论,如果您能够解决我的问题,赐教!