如何使用Python 3.12 zipfile提取复杂zip文件中的所有数据

问题描述 投票:0回答:2

我正在使用此代码:

with ZipFile(sourceZip, mode="r") as extraction:
    extraction.extractall(extractionPath)
    extraction.close()

发生的情况是它只将sourceZip中的第一层Zip文件提取到extractionPath。

我已阅读“https://docs.python.org/3/library/zipfile.html#”,我需要获取代码来提取所有第二层 zip 文件。

python-zipfile
2个回答
0
投票

Python

zipfile
模块默认仅从压缩存档中提取第一层文件。要提取嵌套的 ZIP 文件,您可以使用递归:

def extract_all(source_zip, extraction_path):
    with zipfile.ZipFile(source_zip, mode="r") as zip_ref:
        for zip_info in zip_ref.infolist():
            extracted_file_path = os.path.join(extraction_path, zip_info.filename)

            if zip_info.is_dir():
                os.makedirs(extracted_file_path, exist_ok=True)
            elif zip_info.filename.endswith(".zip"):
                with zip_ref.open(zip_info.filename) as nested_zip:
                    extract_all(nested_zip, extracted_file_path)
            else:
                zip_ref.extract(zip_info, extraction_path)

0
投票

递归提取存档文件内容,直到提取所有嵌套级别。迭代每个 zip 文件的内容,检测文件是否是 zip 存档,然后提取内容。

import os
import zipfile

def extract_zipfile_recursive(zip_file_path, extract_to):
    """
    Recursively extract the contents of a zip file and any nested zip files.
    zip_file_path: Path to the zip file.
    extract_to: Directory where extracted files should be saved.
    """
    with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
        # Extract all contents of the zip file to the extract_to directory
        zip_ref.extractall(extract_to)

        # Iterate through each extracted item (files and directories)
        for item in zip_ref.infolist():
            # Check if the item is a directory or a file
            if item.is_dir():
                # Recursively extract contents of nested zip files in this directory
                dir_path = os.path.join(extract_to, item.filename)
                extract_zipfile_recursive(os.path.join(extract_to, item.filename), dir_path)
            elif item.filename.lower().endswith(".zip"):
                # The item is a zip file, recursively extract its contents
                nested_zip_path = os.path.join(extract_to, item.filename)
                nested_extract_to = os.path.join(extract_to, os.path.splitext(item.filename)[0])
                extract_zipfile_recursive(nested_zip_path, nested_extract_to)

# Example usage:
zip_file_path = "path/to/your/nested.zip"
extract_to_directory = "path/to/extracted/files"

# Create the directory if it doesn't exist
os.makedirs(extract_to_directory, exist_ok=True)

# Extract contents of the nested zip file recursively
extract_zipfile_recursive(zip_file_path, extract_to_directory)
© www.soinside.com 2019 - 2024. All rights reserved.