[Tutor] Create zip files on the fly.

Kent Johnson kent37 at tds.net
Thu Jul 7 13:46:41 CEST 2005


Negroup - wrote:
> Hi, in my application I need to generate a zip file (via the great
> zipfile module) and pass it from function A to function B. One way to
> do it is create the object from function A and write it on filesystem
> via close(). Then, function B will obtain the object reading the file
> from disk. After this operation, the file must be deleted from the
> filesystem.
> 
> Now I'd like to know if it is possible to accomplish this task working
> in memory directly without pass for the filesystem. I know that a
> class StringIO.StringIO (memory files) exists, but I need something
> valid for zipfiles.

The ZipFile constructor accepts a "file-like object"; that is, any object that supports the same interface as a file. An instance of cStringIO.StringIO should work fine here. Something like
out = cStringIO.StringIO()
zip = zipfile.ZipFile(out, 'w', zipfile.ZIP_DEFLATED)

Kent



More information about the Tutor mailing list