[Zope] Re: dynamically creating zip file, returning to user

2006-01-06 Thread Tres Seaver
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Tino Wildenhain wrote:
 John Toews schrieb:
 
Thanks Tino, I'll definately give that a try too. The quick fix seemed
to be opening the file for read in binary mode... that should have been
obvious. Posting another dumb question to the list now. ;)
 
 
 no, dot use a regular file! Use the tempfile module!
 And dont reopen but seek to the start - f.seek(0)
 You just open it for rw.

Don't forget the 'b' when doing zipfiles!


Tres.
- --
===
Tres Seaver  +1 202-558-7113  [EMAIL PROTECTED]
Palladion Software   Excellence by Designhttp://palladion.com
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDvoj3+gerLs4ltQ4RAvpXAJ0Zcs5oTulmzdEwPnJ4nRoe/v8DyACePGum
N0GbYQ63ky/MKtrSNHa1LA0=
=OIXo
-END PGP SIGNATURE-

___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


[Zope] Re: dynamically creating zip file, returning to user

2006-01-04 Thread Tres Seaver
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

John Toews wrote:
 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()
 
 Any thoughts on what fairly obvious thing I'm doing wrong? Thanks!

A couple of thoughts:

 - Be sure you open the file in binary mode ('wb' rather than 'w');
   this won't help unless you are running Zope on Windows, however,
   because Windows is the only platform where the C runtime will mangle
   the line endings for files it thinks are text, rather than
   binary.

 - You could use a StringIO instance, rather than a tempfile;  YMMV.


Tres.
- --
===
Tres Seaver  +1 202-558-7113  [EMAIL PROTECTED]
Palladion Software   Excellence by Designhttp://palladion.com
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDvI6r+gerLs4ltQ4RAvrMAJ9oH0gtorWuW6NeowfVCfZ6PbyYSgCfbCHd
8VhNtnUrBeEiIp8sSLSVQOg=
=f+qw
-END PGP SIGNATURE-

___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )