python使用内存zipfile对象在内存中打包文件示例


Posted in Python onApril 30, 2014
import zipfile
import StringIO
class InMemoryZip(object):
    def __init__(self):
        # Create the in-memory file-like object
        self.in_memory_zip = StringIO.StringIO()
    def append(self, filename_in_zip, file_contents):
        '''Appends a file with name filename_in_zip and contents of 
        file_contents to the in-memory zip.'''
        # Get a handle to the in-memory zip in append mode
        zf = zipfile.ZipFile(self.in_memory_zip, "a", zipfile.ZIP_DEFLATED, False)
        # Write the file to the in-memory zip
        zf.writestr(filename_in_zip, file_contents)
        # Mark the files as having been created on Windows so that
        # Unix permissions are not inferred as 0000
        for zfile in zf.filelist:
            zfile.create_system = 0        
        return self
    def read(self):
        '''Returns a string with the contents of the in-memory zip.'''
        self.in_memory_zip.seek(0)
        return self.in_memory_zip.read()
    def writetofile(self, filename):
        '''Writes the in-memory zip to a file.'''
        f = file(filename, "w")
        f.write(self.read())
        f.close()
if __name__ == "__main__":
    # Run a test
    imz = InMemoryZip()
    imz.append("test.txt", "Another test").append("test2.txt", "Still another")
    imz.writetofile("test.zip")
Python 相关文章推荐
基于Python实现一个简单的银行转账操作
Mar 06 Python
利用Python将时间或时间间隔转为ISO 8601格式方法示例
Sep 05 Python
Django admin美化插件suit使用示例
Dec 12 Python
Python实现简单的语音识别系统
Dec 13 Python
Python实现多级目录压缩与解压文件的方法
Sep 01 Python
Python爬虫实现获取动态gif格式搞笑图片的方法示例
Dec 24 Python
Python3.6中Twisted模块安装的问题与解决
Apr 15 Python
Python迭代器iterator生成器generator使用解析
Oct 24 Python
Python开发之pip安装及使用方法详解
Feb 21 Python
python实现滑雪游戏
Feb 22 Python
利用Python将多张图片合成视频的实现
Nov 23 Python
python入门学习关于for else的特殊特性讲解
Nov 20 Python
python数据结构之二叉树的统计与转换实例
Apr 29 #Python
python数据结构之二叉树的遍历实例
Apr 29 #Python
python数据结构之二叉树的建立实例
Apr 29 #Python
python数据结构树和二叉树简介
Apr 29 #Python
Python的ORM框架SQLAlchemy入门教程
Apr 28 #Python
Python中实现远程调用(RPC、RMI)简单例子
Apr 28 #Python
Python的ORM框架SQLObject入门实例
Apr 28 #Python
You might like
20个PHP常用类库小结
2011/09/11 PHP
php除数取整示例
2014/04/24 PHP
PHP实现下载断点续传的方法
2014/11/12 PHP
PHP会员找回密码功能的简单实现
2016/09/05 PHP
Nigma vs Liquid BO3 第二场2.13
2021/03/10 DOTA
js作用域及作用域链概念理解及使用
2013/04/15 Javascript
jQuery jcrop插件截图使用方法
2013/11/20 Javascript
jQuery实现锚点scoll效果实例分析
2015/03/10 Javascript
jQuery插件jcrop+Fileapi完美实现图片上传+裁剪+预览的代码分享
2015/04/22 Javascript
Express实现前端后端通信上传图片之存储数据库(mysql)傻瓜式教程(一)
2015/12/10 Javascript
JavaScript利用正则表达式替换字符串中的内容
2016/12/12 Javascript
vue组件学习教程
2017/09/09 Javascript
微信端调取相册和摄像头功能,实现图片上传,并上传到服务器
2019/05/16 Javascript
微信小程序代码上传、审核发布小程序
2019/05/18 Javascript
如何实现js拖拽效果及原理解析
2020/05/08 Javascript
python多重继承新算法C3介绍
2014/09/28 Python
Python中join和split用法实例
2015/04/14 Python
Python实现Linux中的du命令
2017/06/12 Python
python 编码规范整理
2018/05/05 Python
Python中的pathlib.Path为什么不继承str详解
2019/06/23 Python
python实现桌面托盘气泡提示
2019/07/29 Python
详解Python可视化神器Yellowbrick使用
2019/11/11 Python
python实现PCA降维的示例详解
2020/02/24 Python
利用django model save方法对未更改的字段依然进行了保存
2020/03/28 Python
简单了解python列表和元组的区别
2020/05/14 Python
python中round函数如何使用
2020/06/19 Python
在网络中有两台主机A和B,并通过路由器和其他交换设备连接起来,已经确认物理连接正确无误,怎么来测试这两台机器是否连通?如果不通,怎么来判断故障点?怎么排
2014/01/13 面试题
行政前台岗位职责
2013/12/04 职场文书
马云的职业生涯规划之路
2014/01/01 职场文书
机关驾驶员违规检讨书
2014/09/13 职场文书
小学教师学习党的群众路线教育实践活动心得体会
2014/10/31 职场文书
2014年镇党建工作汇报材料
2014/11/02 职场文书
营销计划书范文
2015/01/17 职场文书
辞职信怎么写
2015/02/27 职场文书
六一晚会主持词开场白
2015/05/28 职场文书
汉字听写大会观后感
2015/06/12 职场文书