Return a dynamically created zip file in your HttpResponse.

import zipfile
from cStringIO import StringIO
from django.utils.httpwrappers import HttpResponse

def view_that_returns_zipped_file(request):
    response = HttpResponse(mimetype='application/zip')
    response['Content-Disposition'] = ''
    #first assemble your files
    files = []
    for thing in Thing.objects.all():
        files.append(("%s.pdf" % (,), thing.biggish_file()))
    #now add them to a zip file
    #note the zip only exist in memory as you add to it
    buffer = StringIO()
    zip = zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED)
    for name, f in files:
        zip.writestr(name, f)
    #the import detail--we return the content of the buffer
    ret_zip = buffer.getvalue()
    return response
Last modified 16 years ago Last modified on 08/21/06 12:11:18
Back to Top