我有一个大的zip文件,我想要解压缩,而不需要将它的所有字节加载到内存中(通过http请求同时获取压缩的字节)。
如何从Python中实现这一点?
注意:我专门问的是zip格式,而不是gzip。像Python解压缩字节流?这样的问题,虽然经常使用"zip“这个词,但似乎是关于gzip的。
发布于 2021-05-16 20:19:28
可以在Python内部完成此操作,而无需调用外部进程,而且它可以处理zip中的所有文件,而不仅仅是第一个文件。
这可以通过使用流解压免责声明来完成:由我编写。
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)发布于 2021-05-16 09:36:14
通过从Python内部调用漏斗 (使用我编写的可迭代子过程免责声明:),您可以解压缩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)https://stackoverflow.com/questions/67554520
复制相似问题