流式解压缩存档

Stream unZIP archive

我有一个大的 zip 文件,我想解压缩,而不将其所有字节加载到内存中(与通过 http 请求获取压缩字节同时完成)

如何从 Python 完成此操作?

注意:我特意问的是zip格式,不是gzip。诸如 Python unzipping stream of bytes? 之类的问题,虽然经常使用“zip”一词,但似乎与 gzip 有关。

通过调用 funzip from within Python, which be done using iterable-subprocess [免责声明:由我编写],您可以解压 ZIP 存档中的第一个文件:

from iterable_subprocess import iterable_subprocess
import httpx

def zipped_chunks():
    with httpx.stream('GET', 'https://www.example.com/my.zip') as r:
        yield from r.iter_bytes()

for chunk in iterable_subprocess(['funzip'], zipped_chunks()):
    print(chunk)

可以在 Python 中执行此操作,而无需调用外部进程, 并且 它可以处理 zip 中的所有文件,而不仅仅是首先.

这可以通过使用 stream-unzip [免责声明:由我编写]来完成。

from stream_unzip import stream_unzip
import httpx

def zipped_chunks():
    with httpx.stream('GET', 'https://www.example.com/my.zip') as r:
        yield from r.iter_bytes()

for file_name, file_size, file_chunks in stream_unzip(zipped_chunks()):
    for chunk in file_chunks:
        print(chunk)