4

我对 Django 有点陌生,所以请多多包涵。
我从这里使用 zipstream并有一个 Django 视图,它返回所有文件附件的 zip 文件,这些文件附件都托管在 Amazon S3 上。但是当我下载 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
4

1 回答 1

3

ZipFile完成写入后,您应该关闭它。否则,要引用文档,“基本记录将不会被写入”,直到您这样做。

最干净的方法是使用以下with语句:

with zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED) as zf:
    # ...write to zf...
于 2014-10-15T17:23:37.923 回答