【问题标题】:Python in-memory zip libraryPython 内存 zip 库
【发布时间】:2011-01-28 15:47:15
【问题描述】:

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

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

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

【问题讨论】:

标签: python memory zip archive


【解决方案1】:

来自In-Memory Zip in Python的文章:

以下是我在 2008 年 5 月发表的一篇关于使用 Python 压缩内存的帖子,自 Posterous 关闭后重新发布。

我最近注意到有一个付费组件可用于使用 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")

【讨论】:

  • 有用的链接 - 这是一个很好的例子,说明如何以 Jason 的回答所描述的方式使用 ZipFile 对象。谢谢
  • 没问题,很高兴你发现它有用。
  • 注意在这里总结一下链接的内容,如果它死了,你的答案也会死
  • @IvoFlipse - 好点。为了以防万一,我将所有这些内容都添加到了这篇文章中。
  • 在 Windows 或 Python 3.X 下无法正常工作,请参阅我的答案以获取代码更新。
【解决方案2】:

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())

【讨论】:

  • Link 到文档。 data 可以是字节或字符串,这在 Ubuntu 和 Python 3.6 上完美运行
【解决方案3】:

Ethier 提供的示例有几个问题,其中一些很严重:

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

如果您安装ruamel.std.zipfile(我是其中的作者),则可以获得更新版本。之后

pip install ruamel.std.zipfile

或者包含here中的类代码,你可以这样做:

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")  

您也可以使用imz.data 将内容写入您需要的任何地方。

您也可以使用with 语句,如果您提供文件名,ZIP 的内容将在离开该上下文时写入:

with zipfile.InMemoryZipFile('test.zip') as imz:
    imz.append("test.txt", "Another test").append("test2.txt", "Still another")

由于延迟写入磁盘,您实际上可以在该上下文中从旧的test.zip 读取。

【讨论】:

  • 为什么不在 python 2 中使用 io.BytesIO?
  • @boxed 除了您应该检查 2.7 上的 BytesIO 是否使用更快的底层 C 实现,并且不是仅调用 StringIO(而不是 CStringIO)的 Python 兼容层之外,没有特别的原因
  • 这真的应该至少包括你为实际回答问题而编写的任何代码的骨架,而不是仅仅告诉人们安装一个模块。如果没有别的,至少链接到模块的主页。
  • 对于 python 2.7 案例,我建议在传递给 writestr() 函数之前将 unicode 字符串转换为 utf8 字符串。更多详情stackoverflow.com/a/67111639/565525.
【解决方案4】:

根据Python docs

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. 

所以,要在内存中打开文件,只需创建一个类似文件的对象(可能使用BytesIO)。

file_like_object = io.BytesIO(my_zip_data)
zipfile_ob = zipfile.ZipFile(file_like_object)

【讨论】:

    猜你喜欢
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多