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

Python实现压缩与解压gzip大文件的方法

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

通过本文主要向大家介绍了python gzip,python gzip模块,python gzip解压,python gzip.open,gzip文件怎么打开等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了Python实现压缩与解压gzip大文件的方法。分享给大家供大家参考,具体如下:

#encoding=utf-8
#author: walker
#date: 2015-10-26
#summary: 测试gzip压缩/解压文件
import gzip
BufSize = 1024*8
def gZipFile(src, dst):
  fin = open(src, 'rb')
  fout = gzip.open(dst, 'wb')
  in2out(fin, fout)
def gunZipFile(gzFile, dst):
  fin = gzip.open(gzFile, 'rb')
  fout = open(dst, 'wb')
  in2out(fin, fout)
def in2out(fin, fout):
  while True:
    buf = fin.read(BufSize)
    if len(buf) < 1:
      break
    fout.write(buf)
  fin.close()
  fout.close()
if __name__ == '__main__':
  src = r'D:\tmp\src.txt'
  dst = r'D:\tmp\src.txt.gz'
  ori = r'D:\tmp\ori.txt'
  gZipFile(src, dst)
  print('gZipFile over!')
  gunZipFile(dst, ori)
  print('gunZipFile over!')

</div>

也可以简单地封装成一个类:

class GZipTool:
  def __init__(self, bufSize):
    self.bufSize = bufSize
    self.fin = None
    self.fout = None
  def compress(self, src, dst):
    self.fin = open(src, 'rb')
    self.fout = gzip.open(dst, 'wb')
    self.__in2out()
  def decompress(self, gzFile, dst):
    self.fin = gzip.open(gzFile, 'rb')
    self.fout = open(dst, 'wb')
    self.__in2out()
  def __in2out(self,):
    while True:
      buf = self.fin.read(self.bufSize)
      if len(buf) < 1:
        break
      self.fout.write(buf)
    self.fin.close()
    self.fout.close()

</div>

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python URL操作技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

</div>

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

  • Python实现压缩与解压gzip大文件的方法
  • Python实现压缩与解压gzip大文件的方法
  • Python中使用gzip模块压缩文件的简单教程
  • Python中使用gzip模块压缩文件的简单教程

相关文章

  • Python抓取电影天堂电影信息的代码
  • Python Queue模块详细介绍及实例
  • Python中的localtime()方法使用详解
  • Python判断操作系统类型代码分享
  • python统计一个文本中重复行数的方法
  • python判断windows系统是32位还是64位的方法
  • 深入讲解Python编程中的字符串
  • Python中操作符重载用法分析
  • 在Python的Django框架中simple-todo工具的简单使用
  • 详解python调度框架APScheduler使用

文章分类

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

最近更新的内容

    • python字典基本操作实例分析
    • Python基础教程之正则表达式基本语法以及re模块
    • python实现折半查找和归并排序算法
    • python 中文乱码问题深入分析
    • 学习python处理python编码问题
    • Python设计模式之抽象工厂模式
    • Python中对列表排序实例
    • Python实现遍历目录的方法【测试可用】
    • jupyter安装小结
    • 讲解Python中for循环下的索引变量的作用域

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

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