Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python in-memory zip library

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

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.

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

like image 367
John B Avatar asked Mar 17 '10 15:03

John B


People also ask

Is ZIP file included in Python?

Python's zipfile provides convenient classes and functions that allow you to create, read, write, extract, and list the content of your ZIP files. Here are some additional features that zipfile supports: ZIP files greater than 4 GiB (ZIP64 files) Data decryption.

How do I import a ZIP file module in Python?

If you want to import modules and packages from a ZIP file, then you just need the file to appear in Python's module search path. The module search path is a list of directories and ZIP files. It lives in sys.


1 Answers

According to the 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.  

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) 
like image 139
Jason R. Coombs Avatar answered Sep 21 '22 06:09

Jason R. Coombs