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字符和字符值(ASCII或Unicode码值)转换方法
May 21 Python
python 通过logging写入日志到文件和控制台的实例
Apr 28 Python
Python随机生成身份证号码及校验功能
Dec 04 Python
Django实现一对多表模型的跨表查询方法
Dec 18 Python
对python生成业务报表的实例详解
Feb 03 Python
Django中使用 Closure Table 储存无限分级数据
Jun 06 Python
Python将主机名转换为IP地址的方法
Aug 14 Python
Python基于Tensor FLow的图像处理操作详解
Jan 15 Python
Pytest框架之fixture的详细使用教程
Apr 07 Python
使用OpenCV获取图像某点的颜色值,并设置某点的颜色
Jun 02 Python
Python绘制动态水球图过程详解
Jun 03 Python
Python 第三方库 openpyxl 的安装过程
Dec 24 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
深入探讨:Nginx 502 Bad Gateway错误的解决方法
2013/06/03 PHP
ThinkPHP模板范围判断输出In标签与Range标签用法详解
2014/06/30 PHP
PHP设计模式之模板方法模式定义与用法详解
2018/04/02 PHP
php中pcntl_fork创建子进程的方法实例
2019/03/14 PHP
纯Javascript实现Windows 8 Metro风格实现
2013/10/15 Javascript
javascript垃圾收集机制与内存泄漏详细解析
2013/11/11 Javascript
JavaScript 获取任一float型小数点后两位的小数
2014/06/30 Javascript
jQuery.parseJSON(json)将JSON字符串转换成js对象
2014/07/27 Javascript
4种JavaScript实现简单tab选项卡切换的方法
2016/01/06 Javascript
js实现常见的工具条效果
2017/03/02 Javascript
详解nodejs中的process进程
2017/03/19 NodeJs
详解vuejs几种不同组件(页面)间传值的方式
2017/06/01 Javascript
vue 请求后台数据的实例代码
2017/06/22 Javascript
详解webpack4升级指南以及从webpack3.x迁移
2018/06/12 Javascript
node中的密码安全(加密)
2018/09/17 Javascript
vue自定义指令用法经典实例小结
2019/03/16 Javascript
[04:09]2018年度DOTA2社区贡献奖-完美盛典
2018/12/16 DOTA
Python配置mysql的教程(推荐)
2017/10/13 Python
python3模块smtplib实现发送邮件功能
2018/05/22 Python
python处理csv中的空值方法
2018/06/22 Python
python中单下划线_的常见用法总结
2018/07/10 Python
python traceback捕获并打印异常的方法
2018/08/31 Python
离线状态下在jupyter notebook中使用plotly实例
2020/04/24 Python
python多线程实现同时执行两个while循环的操作
2020/05/02 Python
python爬虫中抓取指数的实例讲解
2020/12/01 Python
使用 HTML5 Canvas 制作水波纹效果点击图片就会触发
2014/09/15 HTML / CSS
New Balance天猫官方旗舰店:始于1906年,百年慢跑品牌
2017/11/15 全球购物
荷兰牛仔裤网上商店:Jeans Centre
2018/04/03 全球购物
美国婴儿服装购物网站:Gerber Childrenswear
2020/05/06 全球购物
大学生旷课检讨书
2014/01/22 职场文书
爱心捐款倡议书
2014/04/14 职场文书
校庆标语集锦
2014/06/25 职场文书
小学家长学校培训材料
2014/08/24 职场文书
《水浒传》读后感3篇(范文)
2019/09/19 职场文书
Nginx利用Logrotate实现日志分割
2022/05/20 Servers
Win11远程连接不上怎么办?Win11远程桌面用不了的解决方法
2022/08/05 数码科技