如何在Django中为用户提供动态生成的ZIP存档文件?
我正在制作一个网站,用户可以选择可用书籍的任意组合,并将它们下载为ZIP存档文件。我担心为每个请求生成这样的存档文件会使我的服务器变得非常缓慢。我也听说Django目前没有很好的解决方案来提供动态生成的文件。
HttpResponse
返回StringIO对象的内容,并将mimetype设置为application/x-zip-compressed
(或至少application/octet-stream
)。如果需要,可以设置content-disposition
标头,但这不是真正必需的。以下是一个用Django编写的视图:
import os
import zipfile
import StringIO
from django.http import HttpResponse
def getfiles(request):
# Files (local path) to put in the .zip
# FIXME: Change this (get paths from DB etc)
filenames = ["/tmp/file1.txt", "/tmp/file2.txt"]
# Folder name in ZIP archive which contains the above files
# E.g [thearchive.zip]/somefiles/file2.txt
# FIXME: Set this to something better
zip_subdir = "somefiles"
zip_filename = "%s.zip" % zip_subdir
# Open StringIO to grab in-memory ZIP contents
s = StringIO.StringIO()
# The zip compressor
zf = zipfile.ZipFile(s, "w")
for fpath in filenames:
# Calculate path for file in zip
fdir, fname = os.path.split(fpath)
zip_path = os.path.join(zip_subdir, fname)
# Add file, at correct path
zf.write(fpath, zip_path)
# Must close zip for all contents to be written
zf.close()
# Grab ZIP file from in-memory, make response with correct MIME-type
resp = HttpResponse(s.getvalue(), mimetype = "application/x-zip-compressed")
# ..and correct content-disposition
resp['Content-Disposition'] = 'attachment; filename=%s' % zip_filename
return resp
b = BytesIO.BytesIO()
替换这个来处理二进制文件吗? - qarthandso许多答案建议使用StringIO
或BytesIO
缓冲区。但是,这并不需要,因为HttpResponse
已经是一个类似文件的对象:
response = HttpResponse(content_type='application/zip')
zip_file = zipfile.ZipFile(response, 'w')
for filename in filenames:
zip_file.write(filename)
response['Content-Disposition'] = 'attachment; filename={}'.format(zipfile_name)
return response
请注意,您不应该调用zip_file.close()
,因为打开的“文件”是response
,我们绝对不希望关闭它。
zip_file.close()
函数。 - chaggy我使用了Django 2.0和Python 3.6。
import zipfile
import os
from io import BytesIO
def download_zip_file(request):
filelist = ["path/to/file-11.txt", "path/to/file-22.txt"]
byte_data = BytesIO()
zip_file = zipfile.ZipFile(byte_data, "w")
for file in filelist:
filename = os.path.basename(os.path.normpath(file))
zip_file.write(file, filename)
zip_file.close()
response = HttpResponse(byte_data.getvalue(), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=files.zip'
# Print list files in zip_file
zip_file.printdir()
return response
对于Python3,我使用io.ByteIO 代替 StringIO 来实现此功能,因为StringIO已经被弃用。希望有所帮助。
import io
def my_downloadable_zip(request):
zip_io = io.BytesIO()
with zipfile.ZipFile(zip_io, mode='w', compression=zipfile.ZIP_DEFLATED) as backup_zip:
backup_zip.write('file_name_loc_to_zip') # u can also make use of list of filename location
# and do some iteration over it
response = HttpResponse(zip_io.getvalue(), content_type='application/x-zip-compressed')
response['Content-Disposition'] = 'attachment; filename=%s' % 'your_zipfilename' + ".zip"
response['Content-Length'] = zip_io.tell()
return response
不要介意我打广告:您可以使用django-zipview来实现相同的目的。
pip install django-zipview
后:
from zipview.views import BaseZipView
from reviews import Review
class CommentsArchiveView(BaseZipView):
"""Download at once all comments for a review."""
def get_files(self):
document_key = self.kwargs.get('document_key')
reviews = Review.objects \
.filter(document__document_key=document_key) \
.exclude(comments__isnull=True)
return [review.comments.file for review in reviews if review.comments.name]
我建议使用单独的模型来存储这些临时的压缩文件。您可以动态创建zip文件,使用filefield保存到模型中,最后将url发送给用户。
优点:
这个主题已经有很多贡献了,但是当我第一次研究这个问题时,我发现了这个帖子,所以我想加入我的两分钱。
集成自己的zip创建可能不如Web服务器级别的解决方案稳健和优化。同时,我们正在使用Nginx,它没有默认的模块。
然而,您可以使用mod_zip模块编译Nginx(请参见此处获取具有最新稳定版本Nginx的docker镜像,并使用alpine基础使其比默认Nginx镜像更小)。这将添加zip流功能。
然后Django只需要提供要压缩的文件列表,就完成了!使用库来响应此文件列表更具可重用性,django-zip-stream正好提供了这一点。
不幸的是,它从未真正为我工作过,所以我开始了一个分支,进行修复和改进。
您可以在几行代码中使用它:
def download_view(request, name=""):
from django_zip_stream.responses import FolderZipResponse
path = settings.STATIC_ROOT
path = os.path.join(path, name)
return FolderZipResponse(path)
你需要一种方法让Nginx服务于所有你想要存档的文件,但仅限于此。
你不能只写一个指向“zip服务器”或其他什么的链接吗?为什么zip归档文件本身需要从Django提供服务?在我看来,这里真正需要的只是一个90年代CGI脚本来生成zip并将其输出到stdout。
zipfile
,并让它直接写入其中。我已经在其他事情上做过了。如果您正在处理大量流,这可能会更快且更节省内存。 - Olif.seek()
,而HttpResponse
不支持。 - dbr