Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save viveksoundrapandi/9600712 to your computer and use it in GitHub Desktop.
Save viveksoundrapandi/9600712 to your computer and use it in GitHub Desktop.
A simple snippet to zip files in a directory and send it the browser in downloadable format in django
from shutil import make_archive
from django.core.servers.basehttp import FileWrapper
def download(request,file_name=""):
"""
A django view to zip files in directory and send it as downloadable response to the browser.
Args:
@request: Django request object
@file_name: Name of the directory to be zipped
Returns:
A downloadable Http response
"""
file_path = "/tmp/albums/"+file_name
path_to_zip = make_archive(file_path,"zip",file_path)
response = HttpResponse(FileWrapper(file(path_to_zip,'rb')), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename='+file_name.replace(" ","_")+'.zip'
return response
@kandreyrosales
Copy link

MUltiple zips or single zip too?

@rafaelcapucho
Copy link

file function isn't defined in any place

@skrzypek
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment