【问题标题】:Create zip file from in memory file in python从python中的内存文件创建zip文件
【发布时间】:2015-11-19 20:51:16
【问题描述】:

我正在使用以下代码从 S3 存储桶中检索文件,它工作正常。

file = io.BytesIO()
k.get_contents_to_file(file)

现在我想将这个内存文件添加到一个 zip 文件中。下面的代码将文件名作为参数,但我有一个内存文件。

zip_file.write(filename, zip_path)

我的项目使用 python 3.4。

【问题讨论】:

    标签: python django amazon-s3 zip


    【解决方案1】:

    尝试使用writestr

    签名:writestr(zinfo_or_arcname, data, compress_type=None)

    Docstring:将文件写入存档。内容是“数据”, 可以是“str”或“bytes”实例;如果它是一个'str', 它首先被编码为 UTF-8。 'zinfo_or_arcname' 是 ZipInfo 实例或存档中文件的名称。

    【讨论】:

      猜你喜欢
      • 2016-09-23
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 2014-06-30
      • 1970-01-01
      相关资源
      最近更新 更多