Python 内存 zip 库 [英] Python in-memory zip library

查看:25
本文介绍了Python 内存 zip 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a Python library that allows manipulation of zip archives in memory, without having to use actual disk files?

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

The ZipFile library does not allow you to update the archive. The only way seems to be to extract it to a directory, make your changes, and create a new zip from that directory. I want to modify zip archives without disk access, because I'll be downloading them, making changes, and uploading them again, so I have no reason to store them.

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

Something similar to Java's ZipInputStream/ZipOutputStream would do the trick, although any interface at all that avoids disk access would be fine.

推荐答案

根据 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. 

因此,要在内存中打开文件,只需创建一个类似文件的对象(也许使用 BytesIO).

So, to open the file in memory, just create a file-like object (perhaps using BytesIO).

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

这篇关于Python 内存 zip 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆