class MemoryZipFile(object):
def __init__(self):
#创建内存文件
self._memory_zip = StringIO.StringIO()
def append_content(self, filename_in_zip, file_content):
"""
description: 写文本内容到zip
"""
zf = zipfile.ZipFile(self._memory_zip, "a", zipfile.ZIP_DEFLATED, False)
zf.writestr(filename_in_zip, file_content)
for zfile in zf.filelist: zfile.create_system = 0
return self
def append_file(self, filename_in_zip, local_file_full_path):
""" 使用方法如下:
mem_zip_file = MemoryZipFile()
mem_zip_file.append_content('mimetype', "application/epub+zip")
mem_zip_file.append_content('META-INF/container.xml', '''
''');
#追加磁盘上的文件内容到内存,注意这里的第二个参数是本地磁盘文件的全路径(windows:c/demo/1.jpg | linux: /usr/local/test/1.jpg)
mem_zip_file.append_file("1.jpg", "c:\1.jpg")
#将内存中的zip文件写入磁盘
mem_zip_file.write_file("c:test.zip")
#获取内存zip文件内容
data = mem_zip_file.read()
#上传到fdfs
my_fdfs_client.upload_by_buffer(data, 'zip')
查看更多关于PYTHON如何在内存中生成ZIP文件的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did86790