You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.0 KiB
39 lines
1.0 KiB
6 years ago
|
# coding=utf-8
|
||
|
|
||
5 years ago
|
from __future__ import absolute_import
|
||
6 years ago
|
import gzip
|
||
|
from zlib import Z_FINISH
|
||
|
|
||
|
|
||
|
__all__ = ["GeezipFile", "open", "Z_FINISH"]
|
||
|
|
||
|
|
||
|
def open(filename, mode="rb", compresslevel=9):
|
||
|
"""Shorthand for GzipFile(filename, mode, compresslevel).
|
||
|
|
||
|
The filename argument is required; mode defaults to 'rb'
|
||
|
and compresslevel defaults to 9.
|
||
|
|
||
|
"""
|
||
|
return GeezipFile(filename, mode, compresslevel)
|
||
|
|
||
|
|
||
|
class GeezipFile(gzip.GzipFile):
|
||
|
def close(self):
|
||
|
fileobj = self.fileobj
|
||
|
if fileobj is None:
|
||
|
return
|
||
|
self.fileobj = None
|
||
|
try:
|
||
|
if self.mode == gzip.WRITE:
|
||
|
fileobj.write(self.compress.flush(Z_FINISH))
|
||
|
gzip.write32u(fileobj, self.crc)
|
||
|
# self.size may exceed 2GB, or even 4GB
|
||
5 years ago
|
gzip.write32u(fileobj, self.size & 0xffffffff)
|
||
6 years ago
|
fileobj.flush()
|
||
|
finally:
|
||
|
myfileobj = self.myfileobj
|
||
|
if myfileobj:
|
||
|
self.myfileobj = None
|
||
|
myfileobj.close()
|