| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| from __future__ import absolute_import |
| from __future__ import print_function |
| from __future__ import unicode_literals |
| from io import BytesIO |
| import codecs |
| import zlib |
|
|
| from .utils import GeneratorReader |
|
|
|
|
| class ZLibIncrementalDecoder(codecs.IncrementalDecoder): |
| def __init__(self, errors='strict', wbits=15): |
| assert errors == 'strict' |
| self.errors = errors |
| self.wbits = wbits |
| self.reset() |
|
|
| def decode(self, input, final=False): |
| c = self.decompressobj.decompress(input) |
| if final: |
| c += self.decompressobj.flush() |
| return c |
|
|
| def reset(self): |
| self.decompressobj = zlib.decompressobj(self.wbits) |
|
|
|
|
| def decompress_gen(source, bufsize=4096): |
| dec = ZLibIncrementalDecoder(wbits=-15) |
| exausted = False |
| while not exausted: |
| input = source.read(bufsize) |
| if len(input) < bufsize: |
| exausted = True |
| yield dec.decode(input, exausted) |
|
|
|
|
| def decompress_experimental(source, bufsize=4096): |
| ''' decompress inputstream |
| |
| stream: a file-like readable |
| returns a file-like readable |
| ''' |
| return GeneratorReader(decompress_gen(source, bufsize)) |
|
|
|
|
| def decompress(stream): |
| ''' decompress inputstream |
| |
| stream: a file-like readable |
| returns a file-like readable |
| ''' |
| |
| |
| compressed_maybe = stream.read() |
| try: |
| decompressed = zlib.decompress(compressed_maybe, -15) |
| except zlib.error: |
| return BytesIO(compressed_maybe) |
| else: |
| return BytesIO(decompressed) |
|
|