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