• 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开发利器之ulipad的使用实践
  • Python的GUI框架PySide的安装配置教程
  • python读写ini配置文件方法实例分析
  • python获得一个月有多少天的方法
  • Python的装饰器用法学习笔记
  • 使用Python的Django框架实现事务交易管理的教程
  • Python中使用asyncio 封装文件读写
  • Python实现全局变量的两个解决方法
  • 用Python实现命令行闹钟脚本实例
  • python生成日历实例解析

文章分类

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

最近更新的内容

    • Python struct模块解析
    • Python入门篇之函数
    • python中的一些类型转换函数小结
    • python list转dict示例分享
    • Python中列表(list)操作方法汇总
    • Python网页解析利器BeautifulSoup安装使用介绍
    • 解决Python requests 报错方法集锦
    • 通过数据库对Django进行删除字段和删除模型的操作
    • 举例讲解Python中的list列表数据结构用法
    • 在Python中定义和使用抽象类的方法

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

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