CGI ZIP

Irmen de Jong irmen at -NOSPAM-REMOVE-THIS-xs4all.nl
Sat Mar 8 11:13:50 EST 2003


Simon Faulkner wrote:
> I would like to read in a .txt file then send it out to a browser as a
> .zip file
> 
> Can the zipfile module only write to a file system?

No. It can write to any file-like object:

import cStringIO
s=cStringIO.StringIO()
z=zipfile.ZipFile(s,"w",zipfile.ZIP_DEFLATED)
z.write("yourfilehere.txt")
z.close()

Now you can do anything you want with the generated zip file that
is contained in s, for instance:

open("test.zip","wb").write(s.getvalue())

In your case you'd probably send it as part of the HTTP response
over a socket to the client.

--Irmen.





More information about the Python-list mailing list