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定时执行之Timer用法示例
May 27 Python
python虚拟环境virualenv的安装与使用
Dec 18 Python
Python的时间模块datetime详解
Apr 17 Python
详解Python判定IP地址合法性的三种方法
Mar 06 Python
Python实现的FTP通信客户端与服务器端功能示例
Mar 28 Python
python smtplib模块实现发送邮件带附件sendmail
May 22 Python
Python实现的生产者、消费者问题完整实例
May 30 Python
CentOS7下python3.7.0安装教程
Jul 30 Python
python爬虫租房信息在地图上显示的方法
May 13 Python
python使用opencv实现马赛克效果示例
Sep 28 Python
python实现批处理文件
Jul 28 Python
Python实现打乒乓小游戏
Sep 25 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生成静态页
2006/11/25 PHP
PHP number_format() 函数定义和用法
2012/06/01 PHP
php获取服务器信息的实现代码
2013/02/04 PHP
ThinkPHP中关联查询实例
2014/12/02 PHP
Thinkphp 空操作、空控制器、命名空间(详解)
2017/05/05 PHP
移动节点的jquery代码
2014/01/13 Javascript
当某个文本框成为焦点时即清除文本框内容
2014/04/28 Javascript
JS中捕获console.log()输出的方法
2015/04/16 Javascript
在JavaScript中操作时间之getYear()方法的使用教程
2015/06/11 Javascript
JS实现课堂随机点名和顺序点名
2017/03/09 Javascript
vue实现验证码输入框组件
2017/12/14 Javascript
vue 2.0 购物车小球抛物线的示例代码
2018/02/01 Javascript
jQuery实现的手动拖动控制进度条效果示例【测试可用】
2018/04/18 jQuery
npm全局模块卸载及默认安装目录修改方法
2018/05/15 Javascript
js中事件对象和事件委托的介绍
2019/01/21 Javascript
使用Vue开发自己的Chrome扩展程序过程详解
2019/06/21 Javascript
利用JS如何获取form表单数据
2019/12/19 Javascript
JS实现商品橱窗特效
2020/01/09 Javascript
vue微信分享插件使用方法详解
2020/02/18 Javascript
python&MongoDB爬取图书馆借阅记录
2016/02/05 Python
浅析Python中else语句块的使用技巧
2016/06/16 Python
python入门教程之识别验证码
2017/03/04 Python
Python实现多并发访问网站功能示例
2017/06/19 Python
浅谈python中的数字类型与处理工具
2017/08/02 Python
Pycharm+Python+PyQt5使用详解
2019/09/25 Python
如何解决pycharm调试报错的问题
2020/08/06 Python
python归并排序算法过程实例讲解
2020/11/04 Python
Python学习之time模块的基本使用
2021/01/17 Python
Kenneth Cole官网:纽约时尚优雅品牌
2016/11/14 全球购物
Notino希腊:购买香水和美容产品
2019/07/25 全球购物
Foot Locker澳洲官网:美国运动服和鞋类零售商
2019/10/11 全球购物
夜大自我鉴定
2013/10/31 职场文书
公开承诺书格式
2014/05/21 职场文书
高等教育学专业自荐书
2014/06/17 职场文书
住房抵押登记委托书
2014/09/27 职场文书
2015年幼儿园班务工作总结
2015/05/12 职场文书