• 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模块压缩文件的简单教程

相关文章

  • 使用C#配合ArcGIS Engine进行地理信息系统开发
  • Python进阶篇之字典操作总结
  • 谈谈如何手动释放Python的内存
  • python33 urllib2使用方法细节讲解
  • Python+Django在windows下的开发环境配置图解
  • Python错误提示:[Errno 24] Too many open files的分析与解决
  • 在Python中使用lambda高效操作列表的教程
  • python通过pil模块将raw图片转换成png图片的方法
  • python中遍历文件的3个方法
  • python访问纯真IP数据库的代码

文章分类

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

最近更新的内容

    • python fabric实现远程部署
    • Python map和reduce函数用法示例
    • python端口扫描系统实现方法
    • python实现带验证码网站的自动登陆实现代码
    • python实现将html表格转换成CSV文件的方法
    • python解析html开发库pyquery使用方法
    • python使用PyGame绘制图像并保存为图片文件的方法
    • python学习笔记:字典的使用示例详解
    • 详解Python2.x中对Unicode编码的使用
    • Python处理json字符串转化为字典的简单实现

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

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