Joe Joe - 1 year ago 105
Python Question

Adding a file-like object to a Zip file in Python

The Python ZipFile API seems to allow the passing of a file path to

or a byte string to
but nothing in between. I would like to be able to pass a file like object, in this case a
but any file-like object in principle. At the moment I think I'm going to have to either save the file to disk, or read it into memory. Neither of these is perfect.

Answer Source

You are correct, those are the only two choices. If your DefaultStorage object is large, you may want to go with saving it to disk first; otherwise, I would use:

zipped = ZipFile(...)

If default_storage_object is a StringIO object, it can use default_storage_object.getvalue().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download