> I've got an advanced question.

I'm not sure it's *that* advanced :)

> How do i create a zip file that contain multiple files and folders in
> the zip file?

You can use Python's built-in zipfile[1] module.

   import cStringIO as cs
   import zipfile as z
   f = cs.StringIO() # or a file on disk in write-mode
   zf = z.ZipFile(f, 'w', z.ZIP_DEFLATED)
   zf.write('file.html')
   zf.write('file.php')
   zf.write('file.css')
   zf.write('folder/fileinsubfolder.html')
   zf.write('folder/fileinsubfolder.php')
   zf.close()
   # f is now a zip-file containing the desired structure

Given the small size of the resulting zip file you describe, I'd 
build the whole thing in memory.  If you're building a much 
larger file, I'd use an on-disk file and then stream that out 
over the connection.  Just remember that you'd then have files on 
disk that you may need to clean up afterwards.  Also remember 
that multiple processes writing files can trigger file-name 
collisions if you're not careful.

> And the zip file is available for download?

Once you have the resulting file, send the correct mime-type 
header (application/zip) and then just send the file.  You can 
write the whole file (if it's small and in-memory), or you can 
stream the file with a generator (from a source on disk), or if 
you're using a server that supports the "send file" directive, 
you can point it at an on-disk file and let the server do the 
heavy lifting instead of tying up a Django process.

-tim

[1] http://docs.python.org/library/zipfile.html






--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to