Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Create A Zip of Multiple Files and Make It Downloadable [duplicate]

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!

like image 990
yretuta Avatar asked Oct 14 '12 10:10

yretuta


People also ask

How to create zip file of Django project?

Django makes this easy, just add this the following code at the top of the download function, returning a 401 (and toast message) if the user is not authenticated: from django. contrib import messages ... def download(request): """Download archive zip file of code snippets""" if not request.

How do I zip multiple files in Python?

Create a zip archive from multiple files in PythonCreate a ZipFile object by passing the new file name and mode as 'w' (write mode). It will create a new zip file and open it within ZipFile object. Call write() function on ZipFile object to add the files in it. call close() on ZipFile object to Close the zip file.

How do I zip multiple PDF files?

To place multiple files into a zip folder, select all of the files while hitting the Ctrl button. Then, right-click on one of the files, move your cursor over the “Send to” option and select “Compressed (zipped) folder”.

Does Python Django support multiple file upload?

The Django documentation for File Uploads, gives a brief understanding of the workflow of how to upload a single file using forms and importing in views. In a similar way, we can also tweak it to upload multiple files using forms and views.


1 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 
like image 124
dbr Avatar answered Sep 30 '22 00:09

dbr