pythonmemoryziparchive

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.


Solution

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