Python method for compressing and decompressing large files with gzip

Source: Internet
Author: User
This article describes the Python implementation of the compression and decompression of large files of the method. Share to everyone for your reference, as follows:

#encoding =utf-8#author:walker#date:2015-10-26#summary: Test gzip compression/unzip file import gzipbufsize = 1024*8def 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! ')

It can also be packaged in a single class:

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 ()


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.