【问题标题】:Stream unZIP archive流式解压缩存档
【发布时间】:2021-05-16 08:27:39
【问题描述】:

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

如何在 Python 中做到这一点?

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

【问题讨论】:

  • 这能回答你的问题吗? Python unzipping stream of bytes?
  • @AntonCurmanschii 我不这么认为:虽然这个问题的标题是“zip”,但我认为内容更多的是关于 gzip?

标签: python python-3.x http stream zip


【解决方案1】:

可以在 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)

【讨论】:

    【解决方案2】:

    通过在 Python 中调用 funzip,使用 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)
    

    【讨论】:

      猜你喜欢
      • 2011-01-20
      • 2022-12-21
      • 2010-10-13
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 2010-11-12
      • 2012-06-11
      相关资源
      最近更新 更多