Django – Create A Zip of Multiple Files and Make It Downloadable

Question:

Possible Duplicate:
Serving dynamically generated ZIP archives in Django

(Feel free to point me to any potential duplicates if I have missed them)

I have looked at this snippet:
http://djangosnippets.org/snippets/365/

and this answer:

but I wonder how I can tweak them to suit my need: I want multiple files to be zipped and the archive available as a download via a link (or dynamically generated via a view). I am new to Python and Django so I don’t know how to go about it.

Thank in advance!

Asked By: yretuta

||

Answers:

I’ve posted this on the duplicate question which Willy linked to, but since questions with a bounty cannot be closed as a duplicate, might as well copy it here too:

import os
import zipfile
import StringIO

from django.http import HttpResponse


def getfiles(request):
    # Files (local path) to put in the .zip
    # FIXME: Change this (get paths from DB etc)
    filenames = ["/tmp/file1.txt", "/tmp/file2.txt"]

    # Folder name in ZIP archive which contains the above files
    # E.g [thearchive.zip]/somefiles/file2.txt
    # FIXME: Set this to something better
    zip_subdir = "somefiles"
    zip_filename = "%s.zip" % zip_subdir

    # Open StringIO to grab in-memory ZIP contents
    s = StringIO.StringIO()

    # The zip compressor
    zf = zipfile.ZipFile(s, "w")

    for fpath in filenames:
        # Calculate path for file in zip
        fdir, fname = os.path.split(fpath)
        zip_path = os.path.join(zip_subdir, fname)

        # Add file, at correct path
        zf.write(fpath, zip_path)

    # Must close zip for all contents to be written
    zf.close()

    # Grab ZIP file from in-memory, make response with correct MIME-type
    resp = HttpResponse(s.getvalue(), mimetype = "application/x-zip-compressed")
    # ..and correct content-disposition
    resp['Content-Disposition'] = 'attachment; filename=%s' % zip_filename

    return resp
Answered By: dbr

So as I understand your problem is not how to generate dynamically this file, but creating a link for people to download it…

What I suggest is the following:

0) Create a model for your file, if you want to generate it dynamically don’t use the FileField, but just the info you need for generating this file:

class ZipStored(models.Model):
    zip = FileField(upload_to="/choose/a/path/")

1) Create and store your Zip. This step is important, you create your zip in memory, and then cast it to assign it to the FileField:

function create_my_zip(request, [...]):
    [...]
    # This is a in-memory file
    file_like = StringIO.StringIO()
    # Create your zip, do all your stuff
    zf = zipfile.ZipFile(file_like, mode='w')
    [...]
    # Your zip is saved in this "file"
    zf.close()
    file_like.seek(0)
    # To store it we can use a InMemoryUploadedFile
    inMemory = InMemoryUploadedFile(file_like, None, "my_zip_%s" % filename, 'application/zip', file_like.len, None)
    zip = ZipStored(zip=inMemory)
    # Your zip will be stored!
    zip.save()
    # Notify the user the zip was created or whatever
    [...]

2) Create a url, for example get a number matching the id, you can also use a slugfield (this)

url(r'^get_my_zip/(d+)$', "zippyApp.views.get_zip")

3) Now the view, this view will return the file matching the id passed in the url, you can also use a slug sending the text instead of the id, and make the get filtering by your slugfield.

function get_zip(request, id):
    myzip = ZipStored.object.get(pk = id)
    filename = myzip.zip.name.split('/')[-1]
    # You got the zip! Now, return it!
    response = HttpResponse(myzip.file, content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
Answered By: Nate Gentile
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.