Python Zipstream模块在将zip文件流式传输到浏览器时使用了太多内存

2024-05-23 19:14:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在这里,我尝试使用zipstream新的python模块创建一个zip生成器。 https://pypi.org/project/zipstream-new/

有一件事我不知道,如何防止过多的内存使用。以下是我至今所拥有的:

''

    def zip_generator():

        import zipstream
        z = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_STORED, allowZip64=True)

        for path in path_list:
           z.write(asset_path, arcname=asset_details.get("file_name"))

        for chunk in z:
            yield chunk

    response.set_header('Content-Disposition','attachment; filename={}'.format(zip_filename))
    response.set_header('Content-Type', 'application/zip')
    return zip_generator()

''

如有其他选择,请提出建议


Tags: 模块pathinhttpsforresponsecontentasset