I use Django and Python2.6 to create a zip file of custom Django templates for each user to download a custom zip file. Currently, the code in views.py looks like this:
def download(request): response = HttpResponse(mimetype='application/x-zip-compressed') response['Content-Disposition'] = 'attachment; filename=download.zip' myzip = zipfile.ZipFile(response, 'w') now = datetime.datetime.now() zipInfo = zipfile.ZipInfo('thefile.txt', (now.year, now.month, now.day, now.hour, now.minute, now.second)) myzip.writestr(zipInfo, render_to_string('template.txt', locals(), context_instance=RequestContext(request))) myzip.close() return response
It basically works just fine: the zip file (containing one txt file in this example) loads correctly and I can extract the contents. The only problem, however, is that the permissions for the generated file are neither read nor write for my user by default, and this will not be for users of my site.
How to change the permissions of an automatically generated file before downloading?
Update:
I tried using os.chmod and os.fchmod , as suggested by Mike, but this requires either a path name (which I donβt have) or an error message (for fchmod ):
ZipFile instance has no attribute '__trunc__'
One option, I think, was to save the zip file first, set permissions, and then allow the download, but this seems like overkill - there should be a better way to overcome this simple problem. Anyone have any suggestions or ideas?
Update2:
This problem seems to be limited to Unix systems, as it works fine on Windows and (apparently) OS X. There is a similar thread that I found here . As far as I can tell, this should be related to the writestr method. How to set permissions for a file added to a zip file using writestr ?