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中zfill()方法的使用教程
May 20 Python
python连接mysql实例分享
Oct 09 Python
Python3编程实现获取阿里云ECS实例及监控的方法
Aug 18 Python
django上传图片并生成缩略图方法示例
Dec 11 Python
python深度优先搜索和广度优先搜索
Feb 07 Python
Tensorflow中使用tfrecord方式读取数据的方法
Jun 19 Python
PyQt 实现使窗口中的元素跟随窗口大小的变化而变化
Jun 18 Python
pandas的qcut()方法详解
Jul 06 Python
python3在同一行内输入n个数并用列表保存的例子
Jul 20 Python
python标准库OS模块函数列表与实例全解
Mar 10 Python
基于python实现数组格式参数加密计算
Apr 21 Python
python里glob模块知识点总结
Jan 05 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 date函数参数详解
2006/11/27 PHP
php开启openssl的方法
2014/05/15 PHP
twig里使用js变量的方法
2016/02/05 PHP
基于jQueryUI和Corethink实现百度的搜索提示功能
2016/11/09 PHP
Phpstorm+Xdebug断点调试PHP的方法
2018/05/14 PHP
javascript 限制输入和粘贴(IE,firefox测试通过)
2008/11/14 Javascript
JScript分割字符串示例代码
2013/09/04 Javascript
js监听鼠标点击和键盘点击事件并自动跳转页面
2014/09/24 Javascript
JavaScript 实现完美兼容多浏览器的复制功能代码
2015/04/28 Javascript
使用Bootstrap + Vue.js实现添加删除数据示例
2017/02/27 Javascript
详解AngularJS用Interceptors来统一处理HTTP请求和响应
2017/06/08 Javascript
react-native组件中NavigatorIOS和ListView结合使用的方法
2017/09/30 Javascript
vue-cli 3.x 修改dist路径的方法
2018/09/19 Javascript
Nodejs异步流程框架async的方法
2019/06/07 NodeJs
js实现超级玛丽小游戏
2020/03/18 Javascript
[06:36]吞吞映像top1
2014/06/20 DOTA
[01:03:56]Mineski vs TNC 2018国际邀请赛淘汰赛BO1 8.21
2018/08/22 DOTA
python基于mysql实现的简单队列以及跨进程锁实例详解
2014/07/07 Python
使用Python脚本来控制Windows Azure的简单教程
2015/04/16 Python
用生成器来改写直接返回列表的函数方法
2017/05/25 Python
在python中获取div的文本内容并和想定结果进行对比详解
2019/01/02 Python
在Pycharm中修改文件默认打开方式的方法
2019/01/17 Python
在服务器上安装python3.8.2环境的教程详解
2020/04/26 Python
通过python-pptx模块操作ppt文件的方法
2020/12/26 Python
HTML5通用接口详解
2016/06/12 HTML / CSS
JavaScript+Canvas实现自定义画板的示例代码
2019/05/13 HTML / CSS
澳大利亚墨水站Ink Station:墨水和碳粉打印机墨盒
2019/03/24 全球购物
linux面试题参考答案(6)
2016/06/23 面试题
高一家长会邀请函
2014/01/12 职场文书
就业协议书样本
2014/08/20 职场文书
2015年纪念“卢沟桥事变”78周年活动方案
2015/05/06 职场文书
预备党员半年考察意见
2015/06/01 职场文书
小学体育组工作总结2015
2015/07/21 职场文书
小学教师师德培训心得体会
2016/01/09 职场文书
python简单验证码识别的实现过程
2021/06/20 Python
mysql脏页是什么
2021/07/26 MySQL