从 python 中的内存文件创建 zip 文件

Create zip file from in memory file in python

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

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

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

zip_file.write(filename, zip_path)

我正在为我的项目使用 python 3.4。

尝试使用writestr

Signature: writestr(zinfo_or_arcname, data, compress_type=None)

Docstring: Write a file into the archive. The contents is 'data', which may be either a 'str' or a 'bytes' instance; if it is a 'str', it is encoded as UTF-8 first. 'zinfo_or_arcname' is either a ZipInfo instance or the name of the file in the archive.