Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python3....
File: gzip.py
"""Functions that read and write gzipped files.
[0] Fix | Delete
[1] Fix | Delete
The user of the file doesn't have to worry about the compression,
[2] Fix | Delete
but random access is not allowed."""
[3] Fix | Delete
[4] Fix | Delete
# based on Andrew Kuchling's minigzip.py distributed with the zlib module
[5] Fix | Delete
[6] Fix | Delete
import struct, sys, time, os
[7] Fix | Delete
import zlib
[8] Fix | Delete
import builtins
[9] Fix | Delete
import io
[10] Fix | Delete
import _compression
[11] Fix | Delete
[12] Fix | Delete
__all__ = ["GzipFile", "open", "compress", "decompress"]
[13] Fix | Delete
[14] Fix | Delete
FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
[15] Fix | Delete
[16] Fix | Delete
READ, WRITE = 1, 2
[17] Fix | Delete
[18] Fix | Delete
def open(filename, mode="rb", compresslevel=9,
[19] Fix | Delete
encoding=None, errors=None, newline=None):
[20] Fix | Delete
"""Open a gzip-compressed file in binary or text mode.
[21] Fix | Delete
[22] Fix | Delete
The filename argument can be an actual filename (a str or bytes object), or
[23] Fix | Delete
an existing file object to read from or write to.
[24] Fix | Delete
[25] Fix | Delete
The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or "ab" for
[26] Fix | Delete
binary mode, or "rt", "wt", "xt" or "at" for text mode. The default mode is
[27] Fix | Delete
"rb", and the default compresslevel is 9.
[28] Fix | Delete
[29] Fix | Delete
For binary mode, this function is equivalent to the GzipFile constructor:
[30] Fix | Delete
GzipFile(filename, mode, compresslevel). In this case, the encoding, errors
[31] Fix | Delete
and newline arguments must not be provided.
[32] Fix | Delete
[33] Fix | Delete
For text mode, a GzipFile object is created, and wrapped in an
[34] Fix | Delete
io.TextIOWrapper instance with the specified encoding, error handling
[35] Fix | Delete
behavior, and line ending(s).
[36] Fix | Delete
[37] Fix | Delete
"""
[38] Fix | Delete
if "t" in mode:
[39] Fix | Delete
if "b" in mode:
[40] Fix | Delete
raise ValueError("Invalid mode: %r" % (mode,))
[41] Fix | Delete
else:
[42] Fix | Delete
if encoding is not None:
[43] Fix | Delete
raise ValueError("Argument 'encoding' not supported in binary mode")
[44] Fix | Delete
if errors is not None:
[45] Fix | Delete
raise ValueError("Argument 'errors' not supported in binary mode")
[46] Fix | Delete
if newline is not None:
[47] Fix | Delete
raise ValueError("Argument 'newline' not supported in binary mode")
[48] Fix | Delete
[49] Fix | Delete
gz_mode = mode.replace("t", "")
[50] Fix | Delete
if isinstance(filename, (str, bytes, os.PathLike)):
[51] Fix | Delete
binary_file = GzipFile(filename, gz_mode, compresslevel)
[52] Fix | Delete
elif hasattr(filename, "read") or hasattr(filename, "write"):
[53] Fix | Delete
binary_file = GzipFile(None, gz_mode, compresslevel, filename)
[54] Fix | Delete
else:
[55] Fix | Delete
raise TypeError("filename must be a str or bytes object, or a file")
[56] Fix | Delete
[57] Fix | Delete
if "t" in mode:
[58] Fix | Delete
return io.TextIOWrapper(binary_file, encoding, errors, newline)
[59] Fix | Delete
else:
[60] Fix | Delete
return binary_file
[61] Fix | Delete
[62] Fix | Delete
def write32u(output, value):
[63] Fix | Delete
# The L format writes the bit pattern correctly whether signed
[64] Fix | Delete
# or unsigned.
[65] Fix | Delete
output.write(struct.pack("<L", value))
[66] Fix | Delete
[67] Fix | Delete
class _PaddedFile:
[68] Fix | Delete
"""Minimal read-only file object that prepends a string to the contents
[69] Fix | Delete
of an actual file. Shouldn't be used outside of gzip.py, as it lacks
[70] Fix | Delete
essential functionality."""
[71] Fix | Delete
[72] Fix | Delete
def __init__(self, f, prepend=b''):
[73] Fix | Delete
self._buffer = prepend
[74] Fix | Delete
self._length = len(prepend)
[75] Fix | Delete
self.file = f
[76] Fix | Delete
self._read = 0
[77] Fix | Delete
[78] Fix | Delete
def read(self, size):
[79] Fix | Delete
if self._read is None:
[80] Fix | Delete
return self.file.read(size)
[81] Fix | Delete
if self._read + size <= self._length:
[82] Fix | Delete
read = self._read
[83] Fix | Delete
self._read += size
[84] Fix | Delete
return self._buffer[read:self._read]
[85] Fix | Delete
else:
[86] Fix | Delete
read = self._read
[87] Fix | Delete
self._read = None
[88] Fix | Delete
return self._buffer[read:] + \
[89] Fix | Delete
self.file.read(size-self._length+read)
[90] Fix | Delete
[91] Fix | Delete
def prepend(self, prepend=b''):
[92] Fix | Delete
if self._read is None:
[93] Fix | Delete
self._buffer = prepend
[94] Fix | Delete
else: # Assume data was read since the last prepend() call
[95] Fix | Delete
self._read -= len(prepend)
[96] Fix | Delete
return
[97] Fix | Delete
self._length = len(self._buffer)
[98] Fix | Delete
self._read = 0
[99] Fix | Delete
[100] Fix | Delete
def seek(self, off):
[101] Fix | Delete
self._read = None
[102] Fix | Delete
self._buffer = None
[103] Fix | Delete
return self.file.seek(off)
[104] Fix | Delete
[105] Fix | Delete
def seekable(self):
[106] Fix | Delete
return True # Allows fast-forwarding even in unseekable streams
[107] Fix | Delete
[108] Fix | Delete
class GzipFile(_compression.BaseStream):
[109] Fix | Delete
"""The GzipFile class simulates most of the methods of a file object with
[110] Fix | Delete
the exception of the truncate() method.
[111] Fix | Delete
[112] Fix | Delete
This class only supports opening files in binary mode. If you need to open a
[113] Fix | Delete
compressed file in text mode, use the gzip.open() function.
[114] Fix | Delete
[115] Fix | Delete
"""
[116] Fix | Delete
[117] Fix | Delete
# Overridden with internal file object to be closed, if only a filename
[118] Fix | Delete
# is passed in
[119] Fix | Delete
myfileobj = None
[120] Fix | Delete
[121] Fix | Delete
def __init__(self, filename=None, mode=None,
[122] Fix | Delete
compresslevel=9, fileobj=None, mtime=None):
[123] Fix | Delete
"""Constructor for the GzipFile class.
[124] Fix | Delete
[125] Fix | Delete
At least one of fileobj and filename must be given a
[126] Fix | Delete
non-trivial value.
[127] Fix | Delete
[128] Fix | Delete
The new class instance is based on fileobj, which can be a regular
[129] Fix | Delete
file, an io.BytesIO object, or any other object which simulates a file.
[130] Fix | Delete
It defaults to None, in which case filename is opened to provide
[131] Fix | Delete
a file object.
[132] Fix | Delete
[133] Fix | Delete
When fileobj is not None, the filename argument is only used to be
[134] Fix | Delete
included in the gzip file header, which may include the original
[135] Fix | Delete
filename of the uncompressed file. It defaults to the filename of
[136] Fix | Delete
fileobj, if discernible; otherwise, it defaults to the empty string,
[137] Fix | Delete
and in this case the original filename is not included in the header.
[138] Fix | Delete
[139] Fix | Delete
The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x', or
[140] Fix | Delete
'xb' depending on whether the file will be read or written. The default
[141] Fix | Delete
is the mode of fileobj if discernible; otherwise, the default is 'rb'.
[142] Fix | Delete
A mode of 'r' is equivalent to one of 'rb', and similarly for 'w' and
[143] Fix | Delete
'wb', 'a' and 'ab', and 'x' and 'xb'.
[144] Fix | Delete
[145] Fix | Delete
The compresslevel argument is an integer from 0 to 9 controlling the
[146] Fix | Delete
level of compression; 1 is fastest and produces the least compression,
[147] Fix | Delete
and 9 is slowest and produces the most compression. 0 is no compression
[148] Fix | Delete
at all. The default is 9.
[149] Fix | Delete
[150] Fix | Delete
The mtime argument is an optional numeric timestamp to be written
[151] Fix | Delete
to the last modification time field in the stream when compressing.
[152] Fix | Delete
If omitted or None, the current time is used.
[153] Fix | Delete
[154] Fix | Delete
"""
[155] Fix | Delete
[156] Fix | Delete
if mode and ('t' in mode or 'U' in mode):
[157] Fix | Delete
raise ValueError("Invalid mode: {!r}".format(mode))
[158] Fix | Delete
if mode and 'b' not in mode:
[159] Fix | Delete
mode += 'b'
[160] Fix | Delete
if fileobj is None:
[161] Fix | Delete
fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
[162] Fix | Delete
if filename is None:
[163] Fix | Delete
filename = getattr(fileobj, 'name', '')
[164] Fix | Delete
if not isinstance(filename, (str, bytes)):
[165] Fix | Delete
filename = ''
[166] Fix | Delete
else:
[167] Fix | Delete
filename = os.fspath(filename)
[168] Fix | Delete
if mode is None:
[169] Fix | Delete
mode = getattr(fileobj, 'mode', 'rb')
[170] Fix | Delete
[171] Fix | Delete
if mode.startswith('r'):
[172] Fix | Delete
self.mode = READ
[173] Fix | Delete
raw = _GzipReader(fileobj)
[174] Fix | Delete
self._buffer = io.BufferedReader(raw)
[175] Fix | Delete
self.name = filename
[176] Fix | Delete
[177] Fix | Delete
elif mode.startswith(('w', 'a', 'x')):
[178] Fix | Delete
self.mode = WRITE
[179] Fix | Delete
self._init_write(filename)
[180] Fix | Delete
self.compress = zlib.compressobj(compresslevel,
[181] Fix | Delete
zlib.DEFLATED,
[182] Fix | Delete
-zlib.MAX_WBITS,
[183] Fix | Delete
zlib.DEF_MEM_LEVEL,
[184] Fix | Delete
0)
[185] Fix | Delete
self._write_mtime = mtime
[186] Fix | Delete
else:
[187] Fix | Delete
raise ValueError("Invalid mode: {!r}".format(mode))
[188] Fix | Delete
[189] Fix | Delete
self.fileobj = fileobj
[190] Fix | Delete
[191] Fix | Delete
if self.mode == WRITE:
[192] Fix | Delete
self._write_gzip_header()
[193] Fix | Delete
[194] Fix | Delete
@property
[195] Fix | Delete
def filename(self):
[196] Fix | Delete
import warnings
[197] Fix | Delete
warnings.warn("use the name attribute", DeprecationWarning, 2)
[198] Fix | Delete
if self.mode == WRITE and self.name[-3:] != ".gz":
[199] Fix | Delete
return self.name + ".gz"
[200] Fix | Delete
return self.name
[201] Fix | Delete
[202] Fix | Delete
@property
[203] Fix | Delete
def mtime(self):
[204] Fix | Delete
"""Last modification time read from stream, or None"""
[205] Fix | Delete
return self._buffer.raw._last_mtime
[206] Fix | Delete
[207] Fix | Delete
def __repr__(self):
[208] Fix | Delete
s = repr(self.fileobj)
[209] Fix | Delete
return '<gzip ' + s[1:-1] + ' ' + hex(id(self)) + '>'
[210] Fix | Delete
[211] Fix | Delete
def _init_write(self, filename):
[212] Fix | Delete
self.name = filename
[213] Fix | Delete
self.crc = zlib.crc32(b"")
[214] Fix | Delete
self.size = 0
[215] Fix | Delete
self.writebuf = []
[216] Fix | Delete
self.bufsize = 0
[217] Fix | Delete
self.offset = 0 # Current file offset for seek(), tell(), etc
[218] Fix | Delete
[219] Fix | Delete
def _write_gzip_header(self):
[220] Fix | Delete
self.fileobj.write(b'\037\213') # magic header
[221] Fix | Delete
self.fileobj.write(b'\010') # compression method
[222] Fix | Delete
try:
[223] Fix | Delete
# RFC 1952 requires the FNAME field to be Latin-1. Do not
[224] Fix | Delete
# include filenames that cannot be represented that way.
[225] Fix | Delete
fname = os.path.basename(self.name)
[226] Fix | Delete
if not isinstance(fname, bytes):
[227] Fix | Delete
fname = fname.encode('latin-1')
[228] Fix | Delete
if fname.endswith(b'.gz'):
[229] Fix | Delete
fname = fname[:-3]
[230] Fix | Delete
except UnicodeEncodeError:
[231] Fix | Delete
fname = b''
[232] Fix | Delete
flags = 0
[233] Fix | Delete
if fname:
[234] Fix | Delete
flags = FNAME
[235] Fix | Delete
self.fileobj.write(chr(flags).encode('latin-1'))
[236] Fix | Delete
mtime = self._write_mtime
[237] Fix | Delete
if mtime is None:
[238] Fix | Delete
mtime = time.time()
[239] Fix | Delete
write32u(self.fileobj, int(mtime))
[240] Fix | Delete
self.fileobj.write(b'\002')
[241] Fix | Delete
self.fileobj.write(b'\377')
[242] Fix | Delete
if fname:
[243] Fix | Delete
self.fileobj.write(fname + b'\000')
[244] Fix | Delete
[245] Fix | Delete
def write(self,data):
[246] Fix | Delete
self._check_not_closed()
[247] Fix | Delete
if self.mode != WRITE:
[248] Fix | Delete
import errno
[249] Fix | Delete
raise OSError(errno.EBADF, "write() on read-only GzipFile object")
[250] Fix | Delete
[251] Fix | Delete
if self.fileobj is None:
[252] Fix | Delete
raise ValueError("write() on closed GzipFile object")
[253] Fix | Delete
[254] Fix | Delete
if isinstance(data, bytes):
[255] Fix | Delete
length = len(data)
[256] Fix | Delete
else:
[257] Fix | Delete
# accept any data that supports the buffer protocol
[258] Fix | Delete
data = memoryview(data)
[259] Fix | Delete
length = data.nbytes
[260] Fix | Delete
[261] Fix | Delete
if length > 0:
[262] Fix | Delete
self.fileobj.write(self.compress.compress(data))
[263] Fix | Delete
self.size += length
[264] Fix | Delete
self.crc = zlib.crc32(data, self.crc)
[265] Fix | Delete
self.offset += length
[266] Fix | Delete
[267] Fix | Delete
return length
[268] Fix | Delete
[269] Fix | Delete
def read(self, size=-1):
[270] Fix | Delete
self._check_not_closed()
[271] Fix | Delete
if self.mode != READ:
[272] Fix | Delete
import errno
[273] Fix | Delete
raise OSError(errno.EBADF, "read() on write-only GzipFile object")
[274] Fix | Delete
return self._buffer.read(size)
[275] Fix | Delete
[276] Fix | Delete
def read1(self, size=-1):
[277] Fix | Delete
"""Implements BufferedIOBase.read1()
[278] Fix | Delete
[279] Fix | Delete
Reads up to a buffer's worth of data is size is negative."""
[280] Fix | Delete
self._check_not_closed()
[281] Fix | Delete
if self.mode != READ:
[282] Fix | Delete
import errno
[283] Fix | Delete
raise OSError(errno.EBADF, "read1() on write-only GzipFile object")
[284] Fix | Delete
[285] Fix | Delete
if size < 0:
[286] Fix | Delete
size = io.DEFAULT_BUFFER_SIZE
[287] Fix | Delete
return self._buffer.read1(size)
[288] Fix | Delete
[289] Fix | Delete
def peek(self, n):
[290] Fix | Delete
self._check_not_closed()
[291] Fix | Delete
if self.mode != READ:
[292] Fix | Delete
import errno
[293] Fix | Delete
raise OSError(errno.EBADF, "peek() on write-only GzipFile object")
[294] Fix | Delete
return self._buffer.peek(n)
[295] Fix | Delete
[296] Fix | Delete
@property
[297] Fix | Delete
def closed(self):
[298] Fix | Delete
return self.fileobj is None
[299] Fix | Delete
[300] Fix | Delete
def close(self):
[301] Fix | Delete
fileobj = self.fileobj
[302] Fix | Delete
if fileobj is None:
[303] Fix | Delete
return
[304] Fix | Delete
self.fileobj = None
[305] Fix | Delete
try:
[306] Fix | Delete
if self.mode == WRITE:
[307] Fix | Delete
fileobj.write(self.compress.flush())
[308] Fix | Delete
write32u(fileobj, self.crc)
[309] Fix | Delete
# self.size may exceed 2GB, or even 4GB
[310] Fix | Delete
write32u(fileobj, self.size & 0xffffffff)
[311] Fix | Delete
elif self.mode == READ:
[312] Fix | Delete
self._buffer.close()
[313] Fix | Delete
finally:
[314] Fix | Delete
myfileobj = self.myfileobj
[315] Fix | Delete
if myfileobj:
[316] Fix | Delete
self.myfileobj = None
[317] Fix | Delete
myfileobj.close()
[318] Fix | Delete
[319] Fix | Delete
def flush(self,zlib_mode=zlib.Z_SYNC_FLUSH):
[320] Fix | Delete
self._check_not_closed()
[321] Fix | Delete
if self.mode == WRITE:
[322] Fix | Delete
# Ensure the compressor's buffer is flushed
[323] Fix | Delete
self.fileobj.write(self.compress.flush(zlib_mode))
[324] Fix | Delete
self.fileobj.flush()
[325] Fix | Delete
[326] Fix | Delete
def fileno(self):
[327] Fix | Delete
"""Invoke the underlying file object's fileno() method.
[328] Fix | Delete
[329] Fix | Delete
This will raise AttributeError if the underlying file object
[330] Fix | Delete
doesn't support fileno().
[331] Fix | Delete
"""
[332] Fix | Delete
return self.fileobj.fileno()
[333] Fix | Delete
[334] Fix | Delete
def rewind(self):
[335] Fix | Delete
'''Return the uncompressed stream file position indicator to the
[336] Fix | Delete
beginning of the file'''
[337] Fix | Delete
if self.mode != READ:
[338] Fix | Delete
raise OSError("Can't rewind in write mode")
[339] Fix | Delete
self._buffer.seek(0)
[340] Fix | Delete
[341] Fix | Delete
def readable(self):
[342] Fix | Delete
return self.mode == READ
[343] Fix | Delete
[344] Fix | Delete
def writable(self):
[345] Fix | Delete
return self.mode == WRITE
[346] Fix | Delete
[347] Fix | Delete
def seekable(self):
[348] Fix | Delete
return True
[349] Fix | Delete
[350] Fix | Delete
def seek(self, offset, whence=io.SEEK_SET):
[351] Fix | Delete
if self.mode == WRITE:
[352] Fix | Delete
if whence != io.SEEK_SET:
[353] Fix | Delete
if whence == io.SEEK_CUR:
[354] Fix | Delete
offset = self.offset + offset
[355] Fix | Delete
else:
[356] Fix | Delete
raise ValueError('Seek from end not supported')
[357] Fix | Delete
if offset < self.offset:
[358] Fix | Delete
raise OSError('Negative seek in write mode')
[359] Fix | Delete
count = offset - self.offset
[360] Fix | Delete
chunk = b'\0' * 1024
[361] Fix | Delete
for i in range(count // 1024):
[362] Fix | Delete
self.write(chunk)
[363] Fix | Delete
self.write(b'\0' * (count % 1024))
[364] Fix | Delete
elif self.mode == READ:
[365] Fix | Delete
self._check_not_closed()
[366] Fix | Delete
return self._buffer.seek(offset, whence)
[367] Fix | Delete
[368] Fix | Delete
return self.offset
[369] Fix | Delete
[370] Fix | Delete
def readline(self, size=-1):
[371] Fix | Delete
self._check_not_closed()
[372] Fix | Delete
return self._buffer.readline(size)
[373] Fix | Delete
[374] Fix | Delete
[375] Fix | Delete
class _GzipReader(_compression.DecompressReader):
[376] Fix | Delete
def __init__(self, fp):
[377] Fix | Delete
super().__init__(_PaddedFile(fp), zlib.decompressobj,
[378] Fix | Delete
wbits=-zlib.MAX_WBITS)
[379] Fix | Delete
# Set flag indicating start of a new member
[380] Fix | Delete
self._new_member = True
[381] Fix | Delete
self._last_mtime = None
[382] Fix | Delete
[383] Fix | Delete
def _init_read(self):
[384] Fix | Delete
self._crc = zlib.crc32(b"")
[385] Fix | Delete
self._stream_size = 0 # Decompressed size of unconcatenated stream
[386] Fix | Delete
[387] Fix | Delete
def _read_exact(self, n):
[388] Fix | Delete
'''Read exactly *n* bytes from `self._fp`
[389] Fix | Delete
[390] Fix | Delete
This method is required because self._fp may be unbuffered,
[391] Fix | Delete
i.e. return short reads.
[392] Fix | Delete
'''
[393] Fix | Delete
[394] Fix | Delete
data = self._fp.read(n)
[395] Fix | Delete
while len(data) < n:
[396] Fix | Delete
b = self._fp.read(n - len(data))
[397] Fix | Delete
if not b:
[398] Fix | Delete
raise EOFError("Compressed file ended before the "
[399] Fix | Delete
"end-of-stream marker was reached")
[400] Fix | Delete
data += b
[401] Fix | Delete
return data
[402] Fix | Delete
[403] Fix | Delete
def _read_gzip_header(self):
[404] Fix | Delete
magic = self._fp.read(2)
[405] Fix | Delete
if magic == b'':
[406] Fix | Delete
return False
[407] Fix | Delete
[408] Fix | Delete
if magic != b'\037\213':
[409] Fix | Delete
raise OSError('Not a gzipped file (%r)' % magic)
[410] Fix | Delete
[411] Fix | Delete
(method, flag,
[412] Fix | Delete
self._last_mtime) = struct.unpack("<BBIxx", self._read_exact(8))
[413] Fix | Delete
if method != 8:
[414] Fix | Delete
raise OSError('Unknown compression method')
[415] Fix | Delete
[416] Fix | Delete
if flag & FEXTRA:
[417] Fix | Delete
# Read & discard the extra field, if present
[418] Fix | Delete
extra_len, = struct.unpack("<H", self._read_exact(2))
[419] Fix | Delete
self._read_exact(extra_len)
[420] Fix | Delete
if flag & FNAME:
[421] Fix | Delete
# Read and discard a null-terminated string containing the filename
[422] Fix | Delete
while True:
[423] Fix | Delete
s = self._fp.read(1)
[424] Fix | Delete
if not s or s==b'\000':
[425] Fix | Delete
break
[426] Fix | Delete
if flag & FCOMMENT:
[427] Fix | Delete
# Read and discard a null-terminated string containing a comment
[428] Fix | Delete
while True:
[429] Fix | Delete
s = self._fp.read(1)
[430] Fix | Delete
if not s or s==b'\000':
[431] Fix | Delete
break
[432] Fix | Delete
if flag & FHCRC:
[433] Fix | Delete
self._read_exact(2) # Read & discard the 16-bit header CRC
[434] Fix | Delete
return True
[435] Fix | Delete
[436] Fix | Delete
def read(self, size=-1):
[437] Fix | Delete
if size < 0:
[438] Fix | Delete
return self.readall()
[439] Fix | Delete
# size=0 is special because decompress(max_length=0) is not supported
[440] Fix | Delete
if not size:
[441] Fix | Delete
return b""
[442] Fix | Delete
[443] Fix | Delete
# For certain input data, a single
[444] Fix | Delete
# call to decompress() may not return
[445] Fix | Delete
# any data. In this case, retry until we get some data or reach EOF.
[446] Fix | Delete
while True:
[447] Fix | Delete
if self._decompressor.eof:
[448] Fix | Delete
# Ending case: we've come to the end of a member in the file,
[449] Fix | Delete
# so finish up this member, and read a new gzip header.
[450] Fix | Delete
# Check the CRC and file size, and set the flag so we read
[451] Fix | Delete
# a new member
[452] Fix | Delete
self._read_eof()
[453] Fix | Delete
self._new_member = True
[454] Fix | Delete
self._decompressor = self._decomp_factory(
[455] Fix | Delete
**self._decomp_args)
[456] Fix | Delete
[457] Fix | Delete
if self._new_member:
[458] Fix | Delete
# If the _new_member flag is set, we have to
[459] Fix | Delete
# jump to the next member, if there is one.
[460] Fix | Delete
self._init_read()
[461] Fix | Delete
if not self._read_gzip_header():
[462] Fix | Delete
self._size = self._pos
[463] Fix | Delete
return b""
[464] Fix | Delete
self._new_member = False
[465] Fix | Delete
[466] Fix | Delete
# Read a chunk of data from the file
[467] Fix | Delete
buf = self._fp.read(io.DEFAULT_BUFFER_SIZE)
[468] Fix | Delete
[469] Fix | Delete
uncompress = self._decompressor.decompress(buf, size)
[470] Fix | Delete
if self._decompressor.unconsumed_tail != b"":
[471] Fix | Delete
self._fp.prepend(self._decompressor.unconsumed_tail)
[472] Fix | Delete
elif self._decompressor.unused_data != b"":
[473] Fix | Delete
# Prepend the already read bytes to the fileobj so they can
[474] Fix | Delete
# be seen by _read_eof() and _read_gzip_header()
[475] Fix | Delete
self._fp.prepend(self._decompressor.unused_data)
[476] Fix | Delete
[477] Fix | Delete
if uncompress != b"":
[478] Fix | Delete
break
[479] Fix | Delete
if buf == b"":
[480] Fix | Delete
raise EOFError("Compressed file ended before the "
[481] Fix | Delete
"end-of-stream marker was reached")
[482] Fix | Delete
[483] Fix | Delete
self._add_read_data( uncompress )
[484] Fix | Delete
self._pos += len(uncompress)
[485] Fix | Delete
return uncompress
[486] Fix | Delete
[487] Fix | Delete
def _add_read_data(self, data):
[488] Fix | Delete
self._crc = zlib.crc32(data, self._crc)
[489] Fix | Delete
self._stream_size = self._stream_size + len(data)
[490] Fix | Delete
[491] Fix | Delete
def _read_eof(self):
[492] Fix | Delete
# We've read to the end of the file
[493] Fix | Delete
# We check the that the computed CRC and size of the
[494] Fix | Delete
# uncompressed data matches the stored values. Note that the size
[495] Fix | Delete
# stored is the true file size mod 2**32.
[496] Fix | Delete
crc32, isize = struct.unpack("<II", self._read_exact(8))
[497] Fix | Delete
if crc32 != self._crc:
[498] Fix | Delete
raise OSError("CRC check failed %s != %s" % (hex(crc32),
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function