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网络爬虫采集联想词示例
Feb 11 Python
Python作用域用法实例详解
Mar 15 Python
flask中过滤器的使用详解
Aug 01 Python
Random 在 Python 中的使用方法
Aug 09 Python
python开发准备工作之配置虚拟环境(非常重要)
Feb 11 Python
简单了解Python生成器是什么
Jul 02 Python
如何解决django-celery启动后迅速关闭
Oct 16 Python
使用 Python 合并多个格式一致的 Excel 文件(推荐)
Dec 09 Python
python装饰器的特性原理详解
Dec 25 Python
python中pop()函数的语法与实例
Dec 01 Python
python 录制系统声音的示例
Dec 21 Python
使用gunicorn部署django项目的问题
Dec 30 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
PHP 和 MySQL 基础教程(一)
2006/10/09 PHP
php实现网站插件机制的方法
2009/11/10 PHP
php 获取一个月第一天与最后一天的代码
2010/05/16 PHP
PHP开发规范手册之PHP代码规范详解
2011/01/13 PHP
php防止伪造数据从地址栏URL提交的方法
2014/08/24 PHP
PHP编写学校网站上新生注册登陆程序的实例分享
2016/03/21 PHP
php操作redis命令及代码实例大全
2020/11/19 PHP
jQuery 全选效果实现代码
2009/03/23 Javascript
js左右弹性滚动对联广告代码分享
2014/02/19 Javascript
escape函数解决js中ajax传递中文出现乱码问题
2014/10/30 Javascript
jquery实现鼠标滑过显示提示框的方法
2015/02/05 Javascript
jQuery实现加入收藏夹功能(主流浏览器兼职)
2016/12/24 Javascript
jQuery实现动态生成表格并为行绑定单击变色动作的方法
2017/04/17 jQuery
JavaScript之事件委托实例(附原生js和jQuery代码)
2017/07/22 jQuery
element-ui中select组件绑定值改变,触发change事件方法
2018/08/24 Javascript
利用d3.js力导布局绘制资源拓扑图实例教程
2019/01/08 Javascript
Python 列表(List)操作方法详解
2014/03/11 Python
Python使用Supervisor来管理进程的方法
2015/05/28 Python
利用python实现数据分析
2017/01/11 Python
python2与python3的print及字符串格式化小结
2018/11/30 Python
Python操作mongodb数据库的方法详解
2018/12/08 Python
Python-Flask:动态创建表的示例详解
2019/11/22 Python
pycharm内无法import已安装的模块问题解决
2020/02/12 Python
linux系统下pip升级报错的解决方法
2021/01/31 Python
中国茶叶、茶具一站式网上购物商城:醉品茶城
2018/07/03 全球购物
StubHub美国:购买或出售您的门票
2019/07/09 全球购物
美国在线和移动免费会员制批发零售商:Boxed(移动端的Costco)
2020/01/02 全球购物
我的applet原先好好的, 一放到web server就会有问题,为什么?
2016/05/10 面试题
小学生防溺水广播稿
2014/01/12 职场文书
高中美术教学反思
2014/01/19 职场文书
学生个人自我鉴定
2014/03/26 职场文书
爱护公共设施倡议书
2014/08/29 职场文书
党的群众路线教育实践活动个人对照检查材料范文
2014/09/25 职场文书
大学生自我鉴定怎么写
2019/05/07 职场文书
php 获取音视频时长,PHP 利用getid3 获取音频文件时长等数据
2021/04/01 PHP
win10如何开启ahci模式?win10开启ahci模式详细操作教程
2022/07/23 数码科技