Python内存中的zip库

Joh*_*n B 58 python memory zip archive

是否有一个Python库允许在内存中操作zip存档,而不必使用实际的磁盘文件?

ZipFile库不允许您更新存档.唯一的方法似乎是将其解压缩到一个目录,进行更改,并从该目录创建一个新的zip.我想修改zip存档而无需磁盘访问,因为我将下载它们,进行更改并再次上传,所以我没有理由存储它们.

类似于Java的ZipInputStream/ZipOutputStream的东西可以解决这个问题,尽管任何可以避免磁盘访问的接口都可以.

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

Python中的In-Memory Zip文章:

下面是我从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)

  • 小心在这里总结链接的内容,如果它死了,你的答案也会死 (3认同)
  • @IvoFlipse - 好点。我将所有这些内容添加到这篇文章中,以防万一。 (2认同)
  • 在 Windows 或 Python 3.X 下不能真正工作,请参阅我的答案以获取代码更新。 (2认同)

Ant*_*hon 20

Ethier提供的示例有几个问题,其中一些主要问题:

  • 不适用于Windows上的实际数据.ZIP文件是二进制文件,其数据应始终使用打开的文件'wb'编写
  • ZIP文件附加到每个文件,这是低效的.它只能打开并保存为InMemoryZip属性
  • 文档声明应该显式关闭ZIP文件,这不是在append函数中完成的(它可能适用于(例如)因为zf超出范围并关闭ZIP文件)
  • 每次附加文件时,都会为zipfile中的所有文件设置create_system标志,而不是每个文件只放置一次.
  • 在Python <3上,cStringIO比StringIO更有效
  • 不适用于Python 3(原始文章来自3.0发布之前,但到发布代码时3.1已经出了很长时间).

如果您安装了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该上下文中的旧版本中读取.

  • 这确实应该至少包括您为实际回答问题而编写的任何代码的框架,而不是仅仅告诉人们安装模块。如果没有别的事,至少链接到该模块的主页。 (2认同)

Vla*_*mir 14

PYTHON 3

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)

  • [链接](https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.writestr) 到文档。`data` 可以是字节或字符串,这在 Ubuntu 和 Python 3.6 上完美运行 (2认同)

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)

  • 您因指出“seek(0)”而值得获得一枚奖章。 (2认同)