Skip to content

Instantly share code, notes, and snippets.

@hsleonis
Created January 2, 2018 11:36
Show Gist options
  • Save hsleonis/1ea5bd50e03db8c4c20c2a462028ad42 to your computer and use it in GitHub Desktop.
Save hsleonis/1ea5bd50e03db8c4c20c2a462028ad42 to your computer and use it in GitHub Desktop.
Create zip file
def home(request):
inMemoryOutputFile = BytesIO()
# zipFile = ZipFile(inMemoryOutputFile, 'w')
# zipFile.writestr('OEBPS/content.xhtml', 'hello world')
# zipFile.close()
zipfile = ZipFile(inMemoryOutputFile, "w")
zipfile.writestr("AB/stuff.txt","1sdfghj")
zipfile.writestr("AB/two.txt","2sdfghj")
zipfile.writestr("AB/hello.txt","3sdfgh")
zipfile.close()
inMemoryOutputFile.seek(0)
response = HttpResponse ( zipfile )
response['Content-Disposition'] = "attachment; filename=%s" % "test.zip"
return response
zz = zipfile.ZipFile('Backup.zip', 'w' , zipfile.ZIP_DEFLATED)
zinfo = zipfile.ZipInfo('file_name.txt', time.localtime()[:6])
zinfo.compress_type = zipfile.ZIP_DEFLATED
zz.writestr(zinfo, file_content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment