将zipfile提取到内存中? [英] Extracting a zipfile to memory?
本文介绍了将zipfile提取到内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将压缩文件提取到内存中?
How do I extract a zip to memory?
我的尝试(在.getvalue()
上返回None
):
My attempt (returning None
on .getvalue()
):
from zipfile import ZipFile
from StringIO import StringIO
def extract_zip(input_zip):
return StringIO(ZipFile(input_zip).extractall())
推荐答案
extractall
提取到文件系统,因此您将无法获得所需的内容.要提取内存中的文件,请使用 ZipFile.read()
方法.
extractall
extracts to the file system, so you won't get what you want. To extract a file in memory, use the ZipFile.read()
method.
如果您确实需要内存中的全部内容,则可以执行以下操作:
If you really need the full content in memory, you could do something like:
def extract_zip(input_zip):
input_zip=ZipFile(input_zip)
return {name: input_zip.read(name) for name in input_zip.namelist()}
这篇关于将zipfile提取到内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文