except ImportError:
raise CompressionError("zlib module is not available")
self.zlib = zlib
- self.crc = zlib.crc32("") & 0xffffffffL
if mode == "r":
self._init_read_gz()
else:
self._init_write_gz()
+ self.crc = zlib.crc32("") & 0xffffffffL
if comptype == "bz2":
try:
'''
Add a new gzip block, closing last one
'''
- import zlib
self.close(close_fileobj=False)
self.closed = False
self.concat_pos = 0L
- self.crc = zlib.crc32("") & 0xffffffffL
+ self.crc = self.zlib.crc32("") & 0xffffffffL
self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED,
-self.zlib.MAX_WBITS,
self.zlib.DEF_MEM_LEVEL,
if close_fileobj and not self._extfileobj:
self.fileobj.close()
- # read the zlib crc
- if not close_fileobj and self.mode == "r":
- self.__read(8)
+ # read the zlib crc and length and check them
+ if not close_fileobj and self.mode == "r" and self.comptype == "gz":
+ read_crc = self.__read(4)
+ read_length = self.__read(4)
+ calculated_crc = self.crc & 0xffffffffL
+ if struct.unpack("<L", read_crc)[0] != calculated_crc:
+ raise CompressionError("bad gzip crc")
self.closed = True
except IOError:
raise ReadError("invalid compressed data")
+ if self.comptype == "gz" and hasattr(self, "crc"):
+ self.crc = self.zlib.crc32(buf, self.crc) & 0xffffffffL
if self.concat_stream and len(self.cmp.unused_data) != 0:
self.buf = self.cmp.unused_data + self.buf
self.close(close_fileobj=False)
try:
self._init_read_gz()
- self.closed = False
except:
# happens at the end of the file
pass
+ self.crc = self.zlib.crc32("") & 0xffffffffL
+ self.closed = False
t.append(buf)
c += len(buf)
t = "".join(t)