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)
| 487 | |
| 488 | |
| 489 | def _read_gzip_header(fp): |
| 490 | '''Read a gzip header from `fp` and progress to the end of the header. |
| 491 | |
| 492 | Returns last mtime if header was present or None otherwise. |
| 493 | ''' |
| 494 | magic = fp.read(2) |
| 495 | if magic == b'': |
| 496 | return None |
| 497 | |
| 498 | if magic != b'\037\213': |
| 499 | raise BadGzipFile('Not a gzipped file (%r)' % magic) |
| 500 | |
| 501 | (method, flag, last_mtime) = struct.unpack("<BBIxx", _read_exact(fp, 8)) |
| 502 | if method != 8: |
| 503 | raise BadGzipFile('Unknown compression method') |
| 504 | |
| 505 | if flag & FEXTRA: |
| 506 | # Read & discard the extra field, if present |
| 507 | extra_len, = struct.unpack("<H", _read_exact(fp, 2)) |
| 508 | _read_exact(fp, extra_len) |
| 509 | if flag & FNAME: |
| 510 | # Read and discard a null-terminated string containing the filename |
| 511 | while True: |
| 512 | s = fp.read(1) |
| 513 | if not s or s==b'\000': |
| 514 | break |
| 515 | if flag & FCOMMENT: |
| 516 | # Read and discard a null-terminated string containing a comment |
| 517 | while True: |
| 518 | s = fp.read(1) |
| 519 | if not s or s==b'\000': |
| 520 | break |
| 521 | if flag & FHCRC: |
| 522 | _read_exact(fp, 2) # Read & discard the 16-bit header CRC |
| 523 | return last_mtime |
| 524 | |
| 525 | |
| 526 | class _GzipReader(_streams.DecompressReader): |
no test coverage detected