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.