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算法学习之计数排序实例
Dec 18 Python
Python编写百度贴吧的简单爬虫
Apr 02 Python
如何安装多版本python python2和python3共存以及pip共存
Sep 18 Python
Python使用scipy模块实现一维卷积运算示例
Sep 05 Python
python实现统计代码行数的小工具
Sep 19 Python
opencv-python 提取sift特征并匹配的实例
Dec 09 Python
wxpython多线程防假死与线程间传递消息实例详解
Dec 13 Python
python如何实现单链表的反转
Feb 10 Python
Python filter()及reduce()函数使用方法解析
Sep 05 Python
详解如何修改jupyter notebook的默认目录和默认浏览器
Jan 24 Python
Python使用openpyxl批量处理数据
Jun 23 Python
Python+Appium自动化测试的实战
Jun 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+SqlServer实现分页显示
2006/10/09 PHP
Prototype Object对象 学习
2009/07/12 Javascript
js 对联广告、漂浮广告封装类(IE,FF,Opera,Safari,Chrome
2009/11/26 Javascript
使用jquery修改表单的提交地址基本思路
2014/06/04 Javascript
Javascript设计模式之观察者模式(推荐)
2016/03/29 Javascript
JavaScript实现in-place思想的快速排序方法
2016/08/07 Javascript
Angular 中 select指令用法详解
2016/09/29 Javascript
利用node.js搭建简单web服务器的方法教程
2017/02/20 Javascript
原生js实现放大镜
2017/02/20 Javascript
非常实用的vue导航钩子
2017/03/20 Javascript
JS防抖和节流实例解析
2019/09/24 Javascript
layer 关闭指定弹出层的例子
2019/09/25 Javascript
jQuery实现购物车全功能
2021/01/11 jQuery
Python远程桌面协议RDPY安装使用介绍
2015/04/15 Python
详谈pandas中agg函数和apply函数的区别
2018/04/20 Python
Python实现将Excel转换成为image的方法
2018/10/23 Python
python爬取网易云音乐评论
2018/11/16 Python
Python实现的ftp服务器功能详解【附源码下载】
2019/06/26 Python
浅谈Python小波分析库Pywavelets的一点使用心得
2019/07/09 Python
python使用socket实现的传输demo示例【基于TCP协议】
2019/09/24 Python
python argparser的具体使用
2019/11/10 Python
python解析命令行参数的三种方法详解
2019/11/29 Python
Pyspark获取并处理RDD数据代码实例
2020/03/27 Python
Python要求O(n)复杂度求无序列表中第K的大元素实例
2020/04/02 Python
keras的load_model实现加载含有参数的自定义模型
2020/06/22 Python
Python基于xlrd模块处理合并单元格
2020/07/28 Python
国际书籍零售商:Wordery
2017/11/01 全球购物
英国优质家居用品网上品牌:URBANARA
2018/06/01 全球购物
Joie官方网上商店:购买服装和女装配饰
2018/06/05 全球购物
团购业务员岗位职责
2014/03/15 职场文书
四风问题民主生活会对照检查材料思想汇报
2014/09/27 职场文书
申报优秀教师材料
2014/12/16 职场文书
市场营销计划书范文
2015/01/16 职场文书
会议通知
2015/04/15 职场文书
律政俏佳人观后感
2015/06/09 职场文书
车辆安全隐患排查制度
2015/08/05 职场文书