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