Jas*_*mbs 75
根据Python文档:
class zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])
Open a ZIP file, where file can be either a path to a file (a string) or a file-like object.
Run Code Online (Sandbox Code Playgroud)
因此,要在内存中打开文件,只需创建一个类文件对象(可能使用BytesIO).
file_like_object = io.BytesIO(my_zip_data)
zipfile_ob = zipfile.ZipFile(file_like_object)
Run Code Online (Sandbox Code Playgroud)
Jus*_*ier 42
下面是我从2008年5月开始使用Python压缩内存的帖子,自Posterous关闭后重新发布.
我最近注意到有一个for-pay组件可用于在内存中使用Python压缩文件.考虑到这是应该是免费的,我把以下代码汇总在一起.它只进行了非常基本的测试,所以如果有人发现任何错误,请告诉我,我会更新.
import zipfile
import StringIO
class InMemoryZip(object):
def __init__(self):
# Create the in-memory file-like object
self.in_memory_zip = StringIO.StringIO()
def append(self, filename_in_zip, file_contents):
'''Appends a file with name filename_in_zip and contents of
file_contents to the in-memory zip.'''
# Get a handle to the in-memory zip in append mode
zf = zipfile.ZipFile(self.in_memory_zip, "a", zipfile.ZIP_DEFLATED, False)
# Write the file to the in-memory zip
zf.writestr(filename_in_zip, file_contents)
# Mark the files as having been created on Windows so that
# Unix permissions are not inferred as 0000
for zfile in zf.filelist:
zfile.create_system = 0
return self
def read(self):
'''Returns a string with the contents of the in-memory zip.'''
self.in_memory_zip.seek(0)
return self.in_memory_zip.read()
def writetofile(self, filename):
'''Writes the in-memory zip to a file.'''
f = file(filename, "w")
f.write(self.read())
f.close()
if __name__ == "__main__":
# Run a test
imz = InMemoryZip()
imz.append("test.txt", "Another test").append("test2.txt", "Still another")
imz.writetofile("test.zip")
Run Code Online (Sandbox Code Playgroud)
Ant*_*hon 20
Ethier提供的示例有几个问题,其中一些主要问题:
InMemoryZip属性如果您安装了ruamel.std.zipfile(我是作者),则可以使用更新版本.后
pip install ruamel.std.zipfile
Run Code Online (Sandbox Code Playgroud)
或者从这里包括类的代码,你可以这样做:
import ruamel.std.zipfile as zipfile
# Run a test
zipfile.InMemoryZipFile()
imz.append("test.txt", "Another test").append("test2.txt", "Still another")
imz.writetofile("test.zip")
Run Code Online (Sandbox Code Playgroud)
您也可以将内容写入imz.data您需要的任何地方.
您也可以使用该with语句,如果您提供文件名,则会在离开该上下文时写入ZIP的内容:
with zipfile.InMemoryZipFile('test.zip') as imz:
imz.append("test.txt", "Another test").append("test2.txt", "Still another")
Run Code Online (Sandbox Code Playgroud)
由于写入光盘延迟,您实际上可以从test.zip该上下文中的旧版本中读取.
Vla*_*mir 14
import io
import zipfile
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
for file_name, data in [('1.txt', io.BytesIO(b'111')), ('2.txt', io.BytesIO(b'222'))]:
zip_file.writestr(file_name, data.getvalue())
with open('C:/1.zip', 'wb') as f:
f.write(zip_buffer.getvalue())
Run Code Online (Sandbox Code Playgroud)
Mol*_*sus 10
我正在使用 Flask 创建一个内存中的 zip 文件并将其作为下载返回。基于弗拉基米尔上面的示例。他们seek(0)花了一段时间才弄清楚。
import io
import zipfile
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
for file_name, data in [('1.txt', io.BytesIO(b'111')), ('2.txt', io.BytesIO(b'222'))]:
zip_file.writestr(file_name, data.getvalue())
zip_buffer.seek(0)
return send_file(zip_buffer, attachment_filename='filename.zip', as_attachment=True)
Run Code Online (Sandbox Code Playgroud)