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基于pygame模块播放MP3的方法示例
Sep 30 Python
Python基础语言学习笔记总结(精华)
Nov 14 Python
python实现超简单的视频对象提取功能
Jun 04 Python
python3实现随机数
Jun 25 Python
详解flask入门模板引擎
Jul 18 Python
OpenCV+python手势识别框架和实例讲解
Aug 03 Python
python样条插值的实现代码
Dec 17 Python
对django views中 request, response的常用操作详解
Jul 17 Python
Django认证系统user对象实现过程解析
Mar 02 Python
python mock测试的示例
Oct 19 Python
Python list和str互转的实现示例
Nov 16 Python
Python: glob匹配文件的操作
Dec 11 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反射应用示例
2014/02/25 PHP
php ajax实现文件上传进度条
2016/03/29 PHP
PHP共享内存使用与信号控制实例分析
2018/05/09 PHP
PHP切割整数工具类似微信红包金额分配的思路详解
2019/09/18 PHP
使用Entrust扩展包在laravel 中实现RBAC的功能
2020/03/16 PHP
js parseInt("08")未指定进位制问题
2010/06/19 Javascript
parentElement,srcElement的使用小结
2014/01/13 Javascript
jquery移除、绑定、触发元素事件使用示例详解
2014/04/10 Javascript
JS使用getComputedStyle()方法获取CSS属性值
2014/04/23 Javascript
Node.js的特点和应用场景介绍
2014/11/04 Javascript
jQuery对象的selector属性用法实例
2014/12/27 Javascript
Javascript 正则表达式实现为数字添加千位分隔符
2015/03/10 Javascript
PHP+jQuery实现随意拖动层并即时保存拖动位置
2015/04/30 Javascript
js实现温度计时间样式代码分享
2015/08/21 Javascript
jquery实现像栅栏一样左右滑出式二级菜单效果代码
2015/08/24 Javascript
使用JS动态显示文本
2017/09/09 Javascript
Webpack 服务器端代码打包的示例代码
2017/09/19 Javascript
Vue异步组件处理路由组件加载状态的解决方案
2018/09/07 Javascript
利用JS如何获取form表单数据
2019/12/19 Javascript
JavaScript中的函数式编程详解
2020/08/22 Javascript
python实现登陆知乎获得个人收藏并保存为word文件
2015/03/16 Python
浅谈flask中的before_request与after_request
2018/01/20 Python
Python之reload流程实例代码解析
2018/01/29 Python
Python+PIL实现支付宝AR红包
2018/02/09 Python
Python使用numpy模块实现矩阵和列表的连接操作方法
2019/06/26 Python
Tensorflow实现酸奶销量预测分析
2019/07/19 Python
python中format函数如何使用
2020/06/22 Python
python collections模块的使用
2020/10/16 Python
SheIn俄罗斯:时尚女装网上商店
2017/02/28 全球购物
连锁酒店店长职责范本
2014/02/13 职场文书
《梅兰芳学艺》教学反思
2014/02/24 职场文书
现场施工员岗位职责
2014/03/10 职场文书
国庆节促销广告语2014
2014/09/19 职场文书
长江七号观后感
2015/06/11 职场文书
2016读书月活动心得体会
2016/01/14 职场文书
Python的flask接收前台的ajax的post数据和get数据的方法
2021/04/12 Python