Read a gzip header from `fp` and progress to the end of the header. Returns last mtime if header was present or None otherwise.
(fp)
| 416 | |
| 417 | |
| 418 | def _read_gzip_header(fp): |
| 419 | '''Read a gzip header from `fp` and progress to the end of the header. |
| 420 | |
| 421 | Returns last mtime if header was present or None otherwise. |
| 422 | ''' |
| 423 | magic = fp.read(2) |
| 424 | if magic == b'': |
| 425 | return None |
| 426 | |
| 427 | if magic != b'\037\213': |
| 428 | raise BadGzipFile('Not a gzipped file (%r)' % magic) |
| 429 | |
| 430 | (method, flag, last_mtime) = struct.unpack("<BBIxx", _read_exact(fp, 8)) |
| 431 | if method != 8: |
| 432 | raise BadGzipFile('Unknown compression method') |
| 433 | |
| 434 | if flag & FEXTRA: |
| 435 | # Read & discard the extra field, if present |
| 436 | extra_len, = struct.unpack("<H", _read_exact(fp, 2)) |
| 437 | _read_exact(fp, extra_len) |
| 438 | if flag & FNAME: |
| 439 | # Read and discard a null-terminated string containing the filename |
| 440 | while True: |
| 441 | s = fp.read(1) |
| 442 | if not s or s==b'\000': |
| 443 | break |
| 444 | if flag & FCOMMENT: |
| 445 | # Read and discard a null-terminated string containing a comment |
| 446 | while True: |
| 447 | s = fp.read(1) |
| 448 | if not s or s==b'\000': |
| 449 | break |
| 450 | if flag & FHCRC: |
| 451 | _read_exact(fp, 2) # Read & discard the 16-bit header CRC |
| 452 | return last_mtime |
| 453 | |
| 454 | |
| 455 | class _GzipReader(_compression.DecompressReader): |
no test coverage detected