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

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

问题描述

是否存在一个Python库,该库允许在不使用实际磁盘文件的情况下操纵内存中的zip存档?

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天全站免登陆