[Zope] Zip za Zope (CMF or Plone folder objects)

David Pratt fairwinds at eastlink.ca
Fri Dec 3 15:11:30 EST 2004


Hi Tino.  Thank you for your reply.  The script almost works except 
resulting file cannot be read properly.  I have tried the following.

def zipfolder(self,filename='download.zip'):

	response=self.REQUEST.RESPONSE
	response.setHeader('Content-Type','application/download')
	response.setHeader('Content-Disposition','attachment; filename=%s' % 
filename)
	
	zf=zipfile.ZipFile(response,'w', zipfile.ZIP_DEFLATED)

	for name,obj in self.objectItems(['Portal File','Portal Image']):
		zf.writestr(name,str(obj))
		name_list.append(name)				
	zf.close()

I get a zip file and is about 13k for about 4 files so it has contents 
but the result cannot be read by zip without error.  Three of the files 
are images and one is a regular file.  Maybe there are problems because 
the first three is binary and other text?  Any suggestions?

If I wanted to save the zip file to the original folder, I assume I am 
going to have to use a manage_add method somewhere along the line.

Regards,

Dave


On Friday, December 3, 2004, at 05:09 AM, Tino Wildenhain wrote:

> Hi,
>
> On Thu, 2004-12-02 at 21:30 -0400, David Pratt wrote:
>> Hi.  I would like to zip folder content in a couple of different ways
>> in CMF and Plone:
>>
>> 1) zip in place (zip file made inside folder)
>> 2) zip on filesystem (for email with link to download on apache)
>>
>> I believe this is possible using an external method with the StringIO
>> and zipfile modules.  I can't really find anything other than
>> references to the zipfile module.  Has anyone got something like this
>> working in CMF or Plone before I begin spending time on this?
>>
>> I guess the big thing for me is getting a handle on the files.  My
>> thinking is to make a python script to get the contents of the current
>> folder (and put this in my skin along with changes for links in my
>> folder view templates), make a dictionary so I have the object id and
>> data for each object and pass it to the external method.  Then create
>
> You can have the external method do this, reducing complexity
> (and perhaps memory footprint if you code carefully)
>
>> an empty string file object using StringIO and build the content of 
>> the
>> zip file by iterating over the object ids and data from the dictionary
>> I passed. Once I get the content of the zip file together, the other
>> thing is to write it into the folder I obtained the data from. I want
>> to have another version that would write it into an apache directory
>> and then send the user an email with a download link. Is this a
>> reasonable way to go or is there something better?
>
> I'd generate the zip files on the fly. Just an external method like
> this:
>
>
>
> import zipfile
>
> def zipfolder(self,filename='download.zip'):
> 	response=self.REQUEST.RESPONSE
>         response.setHeader('Content-Type','application/zip')
>         response.setHeader('Content-Disposition','attachment; 
> filename=%
> s' % filename)
>         zf=zipfile.ZipFile(response,'w',zipefile.ZIP_DEFLATED)
>
>         for name,obj in self.objectItems(['File','Image']):
> 		zf.writestr(name,str(obj))
>         zf.close()
>
>
>
> You can call this in the context of the folder where you stored
> the files. Adjust the argument to objectItems accordingly if
> you want to handle other object types too.
>
> HTH
> Tino
>
> 	
>



More information about the Zope mailing list