TOC

Python zipfile

import zipfile
import os

zip_path = "/path/to/zip/file.zip"
extract_path = "/path/to/extract/directory"

if not os.path.exists(extract_path):
    os.makedirs(extract_path)

with zipfile.ZipFile(zip_path, 'r') as fp:
    # 直接解压
    zip_ref.extractall(target_dir)

    # 逐个解压
    for filename in fp.namelist():

        # 如果文件存在就先移除,避免 FileExistsError
        extract_file_path = os.path.join(extract_path, filename)
        if os.path.exists(extract_file_path):
            # print("跳过解压缩文件:", filename)
            # continue
            os.remove(extract_file_path)

        # 解压,会自动创建目录结构
        fp.extract(filename, extract_path)