views:

172

answers:

2

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.

+2  A: 

Check this out: in-memory-zip-in-python

Justin Ethier
Useful link - this is a good example of how to use the ZipFile object in the way described by Jason's answer. Thanks
No problem, glad you found it useful.
Justin Ethier
+7  A: 

According to the Python 2.6.4 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 StringIO).

file_like_object = StringIO(my_zip_data)
zipfile = zipfile.ZipFile(file_like_object)
Jason R. Coombs