• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python使用内存zipfile对象在内存中打包文件示例

python使用内存zipfile对象在内存中打包文件示例

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python zipfile,python zipfile解压,python3 zipfile,python zipfile 加密,zipfile等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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")
</div>

</div>

您可能想查找下面的文章:

  • Python用zip函数同时遍历多个迭代器示例详解
  • Python 读写文件和file对象的方法(推荐)
  • python查看zip包中文件及大小的方法
  • python自动zip压缩目录的方法
  • Python中的zipfile模块使用详解
  • Python遍历zip文件输出名称时出现乱码问题的解决方法
  • Python遍历zip文件输出名称时出现乱码问题的解决方法
  • Python中zip()函数用法实例教程
  • python使用内存zipfile对象在内存中打包文件示例
  • python使用内存zipfile对象在内存中打包文件示例

相关文章

  • python统计文本字符串里单词出现频率的方法
  • 详解Python实现按任意键继续/退出的功能
  • Python脚本实现网卡流量监控
  • Python编程之event对象的用法实例分析
  • Python中的列表生成式与生成器学习教程
  • Python中list列表的一些进阶使用方法介绍
  • 在Django的模型中执行原始SQL查询的方法
  • python中requests模块的使用方法
  • python实现dnspod自动更新dns解析的方法
  • python使用nntp读取新闻组内容的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python wav模块获取采样率 采样点声道量化位数(实例代码)
    • python每次处理固定个数的字符的方法总结
    • python解析文件示例
    • Python实现的二维码生成小软件
    • 使用IronPython把Python脚本集成到.NET程序中的教程
    • Django学习笔记之Class-Based-View
    • 跟老齐学Python之大话题小函数(2)
    • python实现爬取千万淘宝商品的方法
    • 深入理解Python中变量赋值的问题
    • Python open读写文件实现脚本

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有