Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t打开用python和Django创建的zip文件_Python_Django_Zipfile - Fatal编程技术网

Can';t打开用python和Django创建的zip文件

Can';t打开用python和Django创建的zip文件,python,django,zipfile,Python,Django,Zipfile,我创建了一组pdf文件,并希望将它们添加到zip存档中。一切似乎都很好,但当我下载我的zip文件时,它无法打开 因此,我使用create\u pdf函数创建pdf def create_pdf(child): buffer = io.BytesIO() canvas = Canvas(buffer, pagesize=A4) p = staticfiles_storage.path('TNR.ttf') pdfmetrics.registerFont(TTFont

我创建了一组pdf文件,并希望将它们添加到zip存档中。一切似乎都很好,但当我下载我的zip文件时,它无法打开

因此,我使用
create\u pdf
函数创建pdf

def create_pdf(child):
    buffer = io.BytesIO()
    canvas = Canvas(buffer, pagesize=A4)
    p = staticfiles_storage.path('TNR.ttf')
    pdfmetrics.registerFont(TTFont('TNR', p))
    canvas.setFont('TNR', 14)
    t = canvas.beginText(-1 * cm, 29.7 * cm - 1 * cm)
    t.textLines(create_text(child), trim=0)

    canvas.drawText(t)
    canvas.save()
    pdf = buffer.getvalue()
    return pdf
然后我创建zip文件并将其打包以响应

def create_zip(pdfs):
    mem_zip = io.BytesIO()
    i = 0
    with zipfile.ZipFile(mem_zip, mode='w', compression=zipfile.ZIP_DEFLATED)\
         as zf:
        for f in pdfs:
            i += 1
            zf.writestr(f'{str(i)}.pdf', f)
    return mem_zip.getvalue()


def get_files(request, children):
    pdfs = []
    for child in children:
        pdfs.append(create_pdf(child))
    zip = create_zip(pdfs)
    response = FileResponse(zip,
                            content_type='application/zip',
                            filename='zayavleniya.zip')
    response['Content-Disposition'] = 'attachment; filename=files.zip'
    return response
请帮助找出我的错误所在。

在中,您可以看到
write\u str
方法期望
数据
作为第二个参数。在这里,您提供了一个文件名。 因此,pdf文件的内容只是“i.pdf”,这当然不是您期望的pdf文件的内容

试试这样的方法:

def create_zip(pdfs):
    mem_zip = io.BytesIO()
    i = 0
    with zipfile.ZipFile(mem_zip, mode='w', compression=zipfile.ZIP_DEFLATED)\
         as zf:
        for filename in pdfs:
            i += 1
            with open(filename, 'rb') as f:
                zf.writestr(f'{i}.png', f.read())
    return mem_zip.getvalue()
注意:尽量避免使用
zip
作为变量名,因为它已经是一个内置的python函数

更新 如果隔离归档创建以获得最小的工作示例,则会得到以下结果,即根据需要创建zipfile:

def create_zip(pdfs):
    i = 0
    with zipfile.ZipFile(HERE / "my_archive.zip", mode='w', compression=zipfile.ZIP_DEFLATED)\
         as zf:
        for filename in pdfs:
            i += 1
            with open(filename, 'rb') as f:
                zf.writestr(f'{str(i)}.png', f.read())

create_zip(["icon.png"])

在我发布这个问题后,我自己设法找到了答案。我更改了
create\u-zip

def create_zip(pdfs):
    mem_zip = io.BytesIO()
    i = 0
    with zipfile.ZipFile(mem_zip, mode='w', compression=zipfile.ZIP_DEFLATED)\
         as zf:
        for f in pdfs:
            i += 1
            zf.writestr(f'{str(i)}.pdf', f)
    mem_zip.seek(0)
    return mem_zip

谢谢你的建议。我想我成功了。你能确认这是有效的吗?因为我不明白如何使用文件名?(参见我的答案的编辑)