普遍从可以是 zip 或文件夹的源路径获取文件?

Universally getting files from a source-Path that can be either a zip or a folder?

提问人:Mitch McMabers 提问时间:6/16/2023 最后编辑:Mitch McMabers 更新时间:6/25/2023 访问量:55

问:

这是一个有点棘手的问题!

给定以下代码,您将如何实现获取文件的通用方法?

from pathlib import Path
import re
import zipfile as zip

def check_if_zip(path: Path) -> bool:
    return path.is_file() and (
        re.search(r"\.zip$", path.name, re.IGNORECASE) is not None
    )

sources: list[Path] = [
    Path("Foo"),  # Directory
    Path("Bar.zip"),  # Zip
]

for source in sources:
    source_is_zip = check_if_zip(source)

    # Is there a way to universally wrap each source
    # as some kind of "streamed I/O object" or class
    # that reads live from either the folder or zip
    # file? I need a storage-medium agnostic way
    # of fetching files from the "source".
    #
    # It needs to use a fast byte-stream or similar,
    # NOT reading the entire source into memory! :)
    # And the stream needs to be seekable so that
    # it works as input for other libraries, for
    # decoding files contained within the folders/zips.

也许在 Python 中无法完成?

我希望有人觉得这个问题很有趣!:)

python-3.x 文件 zip 解压缩

评论


答: 暂无答案