我对Django有点陌生,所以请耐心等待。
我在这里使用zipstream,并有一个Django视图,它返回所有文件附件的zip文件,这些附件都托管在AmazonS3上。但当我下载zip文件时,它们都会被损坏,也就是说,我无法打开它们。我尝试过用unzip -t
验证这些文件,但错误并没有多大帮助。
file_paths = [fa.file.url for fa in file_attachments.all()]
zf = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED)
zip_subdir = "Attachments-%s" % (request_id)
for file_path in file_paths:
file_dir, file_name = os.path.split(file_path)
zf.writestr(file_name, urllib.urlopen(file_path).read())
zip_filename = "%s.zip" % (zip_subdir)
response = StreamingHttpResponse(zf, mimetype='application/zip')
response['Content-Disposition'] =
'attachment; filename={}'.format(zip_filename)
return response
有什么想法吗?
解决了它。
s = StringIO.StringIO()
with zipstream.ZipFile(s, mode='w', compression=zipstream.ZIP_DEFLATED) as zf:
#could fail on url open.
for file_path in file_paths:
file_dir, file_name = os.path.split(file_path)
try:
file_contents = urllib.urlopen(file_path).read()
zf.writestr(file_name, file_contents)
except IOError: #connection cannot be made
logging.error()
response = StreamingHttpResponse(s.getvalue(), mimetype='application/octet-stream')
response['Content-Disposition'] =
'attachment; filename={}'.format("%s" % (request_id))
return response
您应该在完成对ZipFile
的写入后关闭它。否则,引用文档,在完成之前,"不会写入重要记录"。
最干净的方法是使用with
语句:
with zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED) as zf:
# ...write to zf...