[Zope] dynamically creating zip file, returning to user

Tino Wildenhain tino at wildenhain.de
Thu Jan 5 10:28:44 EST 2006


John Toews schrieb:
> Zope 2.8.0, Python 2.3.5
> 
> I'm having a heck of a time figuring out how to zip up some files in my
> zope instance and return them to the user. I can sucessfully create a
> zip file on the local file system, but if I try to pass it back to the
> user it is corrupted. Of course I'd rather not create this tmp3.zip
> file, so if there's a way around that (which I'm sure there is!) please
> do let me know.
> 
>         filename = 'test.zip'
>         response = self.REQUEST.RESPONSE
>         response.setHeader('Content-Type','application/zip')
>         response.setHeader('Content-Disposition','attachment;
> filename=%s' % filename)
>         # tried zf = zipfile.ZipFile( response, 'w' ) but get error,
> ZHTTP object doesn't have tell method
>         zf = zipfile.ZipFile( '/tmp3.zip', 'w' )
>         zf.writestr( 'testfilename', str( self._getOb( testfileid ) ) )
>         zf.close()
>         f = open('/tmp3.zip')
>         return f.read()
> 
Try with zipefile.ZipFile(response,"a"),
this should avoid the use of tell (untestet)
otoh, If you'd use the tempfile module and
dont return f.read() but use FileStreamIterator
with it, you even have a win performance-wise.

HTH
Tino


More information about the Zope mailing list