[Zope] Returning a zip file (downloaded by user)??
douwe@oberon.nl
douwe@oberon.nl
Thu, 1 Aug 2002 17:09:20 +0200
Hi,
I was sort of working on the same problem and thought it might be
interesting
for the list to see my solution, based on this thread:
def zipFile( self, RESPONSE, anId='', fileList=None ):
"""Returns the contents of the current directory as a zip file"""
if anId=='': anId='download'
sfile=StringIO()
zip=ZipFile(sfile,mode='w', compression=ZIP_DEFLATED)
for f in fileList:
fd = open(f, 'rb')
data = fd.read()
fd.close()
path=f[len(self.basepath):]
zi=ZipInfo(path)
zi.compress_type=ZIP_DEFLATED
zip.writestr( zi, data)
zip.close()
data=sfile.getvalue()
RESPONSE.setHeader('Content-Type', 'application/download')
RESPONSE.setHeader('Content-Length', len(data))
RESPONSE.setHeader('Content-Disposition','attachment;filename=%s.zip' %
anId)
return data
You put this method in your product, in this case LocalFS (or rewrite it to
be an
external method). Pass it a RESPONSE object, anID and a list of files on
your disk (full
paths) and it will return a zip file to be downloaded by the user, including
the relative
path (self.basepath is used for this, if you don't have it, delete that
line)
Douwe
> -----Original Message-----
> From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Chris
> Meyers
> Sent: Tuesday, July 30, 2002 10:44 PM
> To: Aseem Mohanty; zope@zope.org
> Subject: Re: [Zope] Returning a zip file (downloaded by user)??
>
>
> On Tue, Jul 30, 2002 at 01:01:03AM -0700, Aseem Mohanty wrote:
> > Hi,
> > I have a zip file that I create on the fly which is to be returned to
> > the user. The process goes like this:
> >
> > 1. User clicks on a link.
> > 2. External method creates a zip file and puts it in a temp
> folder( file
> > system folder not Zope temp)
> > 3. External method (same one) returns the zip file as part of response
> > as type application/x-zip
> >
> > The following is what I do in to get it done:(part 3 i.e.)
> >
> > zip_size = stat(zip_file_name).st_size
> > zip_file = open(zip_file_name)
> > zip_data = zip_file.read()
> > zip_file.close()
> >
> > REQUEST.RESPONSE.setStatus('OK')
> > REQUEST.RESPONSE.setHeader('Content-Type','application/x-zip')
> > REQUEST.RESPONSE.setHeader('Content-Length',zip_size)
> > REQUEST.RESPONSE.write(zip_data)
> >
> > but when I try it out the following happens:
> > - the zip file is created
> > - zip_data has the data (of course)
> > - i am prompted to download something of type
> application/x-unknown-content
> > - if i save the file it has zero length
> >
> > so what am I doing wrong or not doing here, that needs to be
> done to get
> > this right??
> > TIA
> > AM
> >
> > --
>
> I am doing something similar to this except I am setting up my
> zip file in an External Method, and then returning the read in
> zip file to a python script which looks something like this:
>
> data = context.externalMethodReturningZipFile()
> RESPONSE.setHeader('Content-Type', 'application/download')
> RESPONSE.setHeader('Content-Length', len(data))
> RESPONSE.setHeader('Content-Disposition','attachment;filename=Some
> File.zip')
>
> return data
>
> HTH,
> Chris
>
> > ==================================================================
> > Aseem Mohanty
> > Neurobehavioral Systems Inc, 828 San Pablo Ave, Albany, CA 94706
> > (R) 510 7696011 (M) 510 3014871 (O) 510 5279231
> >
> ============================================================
> ======
> > "I saw `cout' being shifted "Hello world" times to the left and
> > stopped right there!!" -- Steve Gonedes
> > ==================================================================
> >
> >
> >
> > _______________________________________________
> > Zope maillist - Zope@zope.org
> > http://lists.zope.org/mailman/listinfo/zope
> > ** No cross posts or HTML encoding! **
> > (Related lists -
> > http://lists.zope.org/mailman/listinfo/zope-announce
> > http://lists.zope.org/mailman/listinfo/zope-dev )
>
> --
> Chris Meyers
> Huttleston Data Design
> 7941 Tree Lane Suite 200
> Madison WI 53717
>
> _______________________________________________
> Zope maillist - Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> ** No cross posts or HTML encoding! **
> (Related lists -
> http://lists.zope.org/mailman/listinfo/zope-announce
> http://lists.zope.org/mailman/listinfo/zope-dev )
>