Python's method of compressing and decompressing gzip large files _python

Source: Internet
Author: User

This article describes the Python implementation of compression and decompression of the gzip large file method. Share to everyone for your reference, specific as follows:

#encoding =utf-8
#author: Walker
#date: 2015-10-26
#summary: Test gzip compression/Extract file
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! ')

can also be simply encapsulated into a 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 ()

More information about Python-related content can be viewed in this site: "Python file and directory operation tips Summary", "Python text file Operation tips Summary", "Python URL operation tips Summary", "Python Picture Operation tips Summary", " Python data structure and algorithm tutorial, Python socket Programming Tips Summary, Python function Usage tips Summary, python string manipulation tips and Python introductory and advanced classic tutorials

I hope this article will help you with Python programming.

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.