I have a reporting function on my Web Application that has been subjected to Feature Creep -- instead of providing a PDF, it must now provide a .zip file that contains a variety of documents.
Generating the documents is fine. Adding documents to the Zipfile is the issue.
Until now, the various documents to be added to the archive have existed as a mix of cStringIO
, StringIO
or tempfile.SpooledTemporaryFile
objects.
Digging into the zipfile
library docs, it appears that the module's [write][1]
function will only work with Strings
or paths to physical files on the machine; it does not work with file-like objects.
Just to be clear: zipfile
can read/write from a file-like object as the archive itself (zipfile.ZipFile
), however when adding an element to the archive, the library only supports a pathname or raw string data.
I found an online blog posting suggesting a possible workaround, but I'm not eager to use it on my production machine. http://swl10.blogspot.com/2012/12/writing-stream-to-zipfile-in-python.html
Does anyone have other strategies for handling this? It looks like I have to either save everything to disk and take a hit on I/O , or handle everything as a string and take a hit on memory. Neither are ideal.