Small proxy class that enables transparent compression detection for the Stream interface (mode 'r|*').
| 573 | # class _Stream |
| 574 | |
| 575 | class _StreamProxy(object): |
| 576 | """Small proxy class that enables transparent compression |
| 577 | detection for the Stream interface (mode 'r|*'). |
| 578 | """ |
| 579 | |
| 580 | def __init__(self, fileobj): |
| 581 | self.fileobj = fileobj |
| 582 | self.buf = self.fileobj.read(BLOCKSIZE) |
| 583 | |
| 584 | def read(self, size): |
| 585 | self.read = self.fileobj.read |
| 586 | return self.buf |
| 587 | |
| 588 | def getcomptype(self): |
| 589 | if self.buf.startswith(b"\x1f\x8b\x08"): |
| 590 | return "gz" |
| 591 | elif self.buf[0:3] == b"BZh" and self.buf[4:10] == b"1AY&SY": |
| 592 | return "bz2" |
| 593 | elif self.buf.startswith((b"\x5d\x00\x00\x80", b"\xfd7zXZ")): |
| 594 | return "xz" |
| 595 | else: |
| 596 | return "tar" |
| 597 | |
| 598 | def close(self): |
| 599 | self.fileobj.close() |
| 600 | # class StreamProxy |
| 601 | |
| 602 | #------------------------ |