def create_attachments_zipfile(attachments, temporary_file=None):
if not temporary_file:
temporary_file = NamedTemporaryFile()
storage = get_storage_class()()
with zipfile.ZipFile(temporary_file, 'w', zipfile.ZIP_STORED, allowZip64=True) as zip_file:
for attachment in attachments:
if storage.exists(attachment.media_file.name):
try:
with storage.open(attachment.media_file.name, 'rb') as source_file:
zip_file.writestr(attachment.media_file.name, source_file.read())
except Exception, e:
report_exception("Error adding file \"{}\" to archive.".format(attachment.media_file.name), e)
# Be kind; rewind.
temporary_file.seek(0)
return temporary_file
评论列表
文章目录