bazarr/libs/subzero/lib/geezip.py

39 lines
1.0 KiB
Python
Raw Normal View History

2018-10-31 16:08:29 +00:00
# coding=utf-8
2019-09-17 02:04:27 +00:00
from __future__ import absolute_import
2018-10-31 16:08:29 +00:00
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
2019-09-13 19:12:26 +00:00
gzip.write32u(fileobj, self.size & 0xffffffff)
2018-10-31 16:08:29 +00:00
fileobj.flush()
finally:
myfileobj = self.myfileobj
if myfileobj:
self.myfileobj = None
myfileobj.close()