29
29
# we want a \n preserved, break on \n only splitlines.
32
__all__ = ["GzipFile", "bytes_to_gzip"]
35
def bytes_to_gzip(bytes, factory=zlib.compressobj,
36
level=zlib.Z_DEFAULT_COMPRESSION, method=zlib.DEFLATED,
37
width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL,
39
"""Create a gzip file containing bytes and return its content."""
41
'\037\213' # self.fileobj.write('\037\213') # magic header
42
'\010' # self.fileobj.write('\010') # compression method
43
# fname = self.filename[:-3]
47
'\x00' # self.fileobj.write(chr(flags))
48
'\0\0\0\0' # write32u(self.fileobj, long(time.time()))
49
'\002' # self.fileobj.write('\002')
50
'\377' # self.fileobj.write('\377')
52
'' # self.fileobj.write(fname + '\000')
54
# using a compressobj avoids a small header and trailer that the compress()
55
# utility function adds.
56
compress = factory(level, method, width, mem, 0)
57
result.append(compress.compress(bytes))
58
result.append(compress.flush())
59
result.append(struct.pack("<L", LOWU32(crc32(bytes))))
60
# size may exceed 2GB, or even 4GB
61
result.append(struct.pack("<L", LOWU32(len(bytes))))
62
return ''.join(result)
32
__all__ = ["GzipFile"]
65
35
class GzipFile(gzip.GzipFile):
151
121
self._add_read_data(self.decompress.flush())
152
if len(self.decompress.unused_data) < 8:
153
raise AssertionError("what does flush do?")
122
assert len(self.decompress.unused_data) >= 8, "what does flush do?"
154
123
self._gzip_tail = self.decompress.unused_data[0:8]
156
125
# tell the driving read() call we have stuffed all the data
176
145
self._gzip_tail = self.decompress.unused_data[0:8]
177
146
elif seek_length < 0:
178
147
# we haven't read enough to check the checksum.
179
if not (-8 < seek_length):
180
raise AssertionError("too great a seek")
148
assert -8 < seek_length, "too great a seek."
181
149
buf = self.fileobj.read(-seek_length)
182
150
self._gzip_tail = self.decompress.unused_data + buf
202
170
# We then check the that the computed CRC and size of the
203
171
# uncompressed data matches the stored values. Note that the size
204
172
# stored is the true file size mod 2**32.
205
if not (len(self._gzip_tail) == 8):
206
raise AssertionError("gzip trailer is incorrect length.")
173
assert len(self._gzip_tail) == 8, "gzip trailer is incorrect length."
207
174
crc32, isize = struct.unpack("<LL", self._gzip_tail)
208
175
# note that isize is unsigned - it can exceed 2GB
209
176
if crc32 != U32(self.crc):