Python zlib.decompressobj() Examples

The following are 30 code examples of zlib.decompressobj(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module zlib , or try the search function .
Example #1
Source File: web.py    From wechat-alfred-workflow with MIT License 7 votes vote down vote up
def content(self):
        """Raw content of response (i.e. bytes).

        :returns: Body of HTTP response
        :rtype: str

        """
        if not self._content:

            # Decompress gzipped content
            if self._gzipped:
                decoder = zlib.decompressobj(16 + zlib.MAX_WBITS)
                self._content = decoder.decompress(self.raw.read())

            else:
                self._content = self.raw.read()

            self._content_loaded = True

        return self._content 
Example #2
Source File: response.py    From jawfish with MIT License 7 votes vote down vote up
def decompress(self, data):
        if not data:
            return data

        if not self._first_try:
            return self._obj.decompress(data)

        self._data += data
        try:
            return self._obj.decompress(data)
        except zlib.error:
            self._first_try = False
            self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
            try:
                return self.decompress(self._data)
            finally:
                self._data = None 
Example #3
Source File: response.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def decompress(self, data):
        ret = bytearray()
        if self._state == GzipDecoderState.SWALLOW_DATA or not data:
            return bytes(ret)
        while True:
            try:
                ret += self._obj.decompress(data)
            except zlib.error:
                previous_state = self._state
                # Ignore data after the first error
                self._state = GzipDecoderState.SWALLOW_DATA
                if previous_state == GzipDecoderState.OTHER_MEMBERS:
                    # Allow trailing garbage acceptable in other gzip clients
                    return bytes(ret)
                raise
            data = self._obj.unused_data
            if not data:
                return bytes(ret)
            self._state = GzipDecoderState.OTHER_MEMBERS
            self._obj = zlib.decompressobj(16 + zlib.MAX_WBITS) 
Example #4
Source File: response.py    From gist-alfred with MIT License 6 votes vote down vote up
def decompress(self, data):
        ret = bytearray()
        if self._state == GzipDecoderState.SWALLOW_DATA or not data:
            return bytes(ret)
        while True:
            try:
                ret += self._obj.decompress(data)
            except zlib.error:
                previous_state = self._state
                # Ignore data after the first error
                self._state = GzipDecoderState.SWALLOW_DATA
                if previous_state == GzipDecoderState.OTHER_MEMBERS:
                    # Allow trailing garbage acceptable in other gzip clients
                    return bytes(ret)
                raise
            data = self._obj.unused_data
            if not data:
                return bytes(ret)
            self._state = GzipDecoderState.OTHER_MEMBERS
            self._obj = zlib.decompressobj(16 + zlib.MAX_WBITS) 
Example #5
Source File: bgzf.py    From gcp-variant-transforms with Apache License 2.0 6 votes vote down vote up
def _complete_last_line(self):
    # Fetches the first line in the next `self._read_size` bytes.
    buf = self._file.raw._downloader.get_range(
        self._block.end, self._block.end + self._read_size)
    self._decompressor = zlib.decompressobj(self._gzip_mask)
    decompressed = self._decompressor.decompress(buf)
    del buf
    if not decompressed:
      return
    # Writes all data to the buffer until the first `\n` is reached.
    while '\n' not in decompressed:
      if self._decompressor.unused_data != b'':
        self._read_buffer.write(decompressed)
        buf = self._decompressor.unused_data
        self._decompressor = zlib.decompressobj(self._gzip_mask)
        decompressed = self._decompressor.decompress(buf)
        del buf
      else:
        raise ValueError('Read failed. The record is longer than {} '
                         'bytes.'.format(self._read_size))
    self._read_buffer.write(decompressed.split('\n')[0] + '\n') 
Example #6
Source File: bgzf.py    From gcp-variant-transforms with Apache License 2.0 6 votes vote down vote up
def _read_first_gzip_block_into_buffer(self):
    buf = self._read_data_from_source()
    decompressed = self._decompressor.decompress(buf)
    del buf
    # Discards all data before first `\n`.
    while '\n' not in decompressed:
      if self._decompressor.unused_data != b'':
        buf = self._decompressor.unused_data
        self._decompressor = zlib.decompressobj(self._gzip_mask)
        decompressed = self._decompressor.decompress(buf)
        del buf
      else:
        raise ValueError('Read failed. The block {} does not contain any '
                         'valid record.'.format(self._block))

    lines = decompressed.split('\n')
    self._read_buffer.write('\n'.join(lines[1:])) 
Example #7
Source File: web.py    From gist-alfred with MIT License 6 votes vote down vote up
def content(self):
        """Raw content of response (i.e. bytes).

        :returns: Body of HTTP response
        :rtype: str

        """
        if not self._content:

            # Decompress gzipped content
            if self._gzipped:
                decoder = zlib.decompressobj(16 + zlib.MAX_WBITS)
                self._content = decoder.decompress(self.raw.read())

            else:
                self._content = self.raw.read()

            self._content_loaded = True

        return self._content 
Example #8
Source File: response.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def decompress(self, data):
        if not data:
            return data

        if not self._first_try:
            return self._obj.decompress(data)

        self._data += data
        try:
            decompressed = self._obj.decompress(data)
            if decompressed:
                self._first_try = False
                self._data = None
            return decompressed
        except zlib.error:
            self._first_try = False
            self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
            try:
                return self.decompress(self._data)
            finally:
                self._data = None 
Example #9
Source File: response.py    From gist-alfred with MIT License 6 votes vote down vote up
def decompress(self, data):
        if not data:
            return data

        if not self._first_try:
            return self._obj.decompress(data)

        self._data += data
        try:
            decompressed = self._obj.decompress(data)
            if decompressed:
                self._first_try = False
                self._data = None
            return decompressed
        except zlib.error:
            self._first_try = False
            self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
            try:
                return self.decompress(self._data)
            finally:
                self._data = None 
Example #10
Source File: response.py    From NEIE-Assistant with GNU General Public License v3.0 6 votes vote down vote up
def decompress(self, data):
        if not data:
            return data

        if not self._first_try:
            return self._obj.decompress(data)

        self._data += data
        try:
            return self._obj.decompress(data)
        except zlib.error:
            self._first_try = False
            self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
            try:
                return self.decompress(self._data)
            finally:
                self._data = None 
Example #11
Source File: response.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def decompress(self, data):
        ret = bytearray()
        if self._state == GzipDecoderState.SWALLOW_DATA or not data:
            return bytes(ret)
        while True:
            try:
                ret += self._obj.decompress(data)
            except zlib.error:
                previous_state = self._state
                # Ignore data after the first error
                self._state = GzipDecoderState.SWALLOW_DATA
                if previous_state == GzipDecoderState.OTHER_MEMBERS:
                    # Allow trailing garbage acceptable in other gzip clients
                    return bytes(ret)
                raise
            data = self._obj.unused_data
            if not data:
                return bytes(ret)
            self._state = GzipDecoderState.OTHER_MEMBERS
            self._obj = zlib.decompressobj(16 + zlib.MAX_WBITS) 
Example #12
Source File: response.py    From jbox with MIT License 6 votes vote down vote up
def decompress(self, data):
        if not data:
            return data

        if not self._first_try:
            return self._obj.decompress(data)

        self._data += data
        try:
            return self._obj.decompress(data)
        except zlib.error:
            self._first_try = False
            self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
            try:
                return self.decompress(self._data)
            finally:
                self._data = None 
Example #13
Source File: response.py    From jbox with MIT License 6 votes vote down vote up
def decompress(self, data):
        if not data:
            return data

        if not self._first_try:
            return self._obj.decompress(data)

        self._data += data
        try:
            return self._obj.decompress(data)
        except zlib.error:
            self._first_try = False
            self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
            try:
                return self.decompress(self._data)
            finally:
                self._data = None 
Example #14
Source File: gzip_decoder.py    From snowflake-connector-python with Apache License 2.0 6 votes vote down vote up
def decompress_raw_data_to_unicode_stream(raw_data_fd: IO):
    """Decompresses a raw data in file like object and yields a Unicode string.

    Args:
        raw_data_fd: File descriptor object.

    Yields:
        A string of the decompressed file in chunks.
    """
    obj = zlib.decompressobj(MAGIC_NUMBER + zlib.MAX_WBITS)
    yield '['
    d = raw_data_fd.read(CHUNK_SIZE)
    while d:
        yield obj.decompress(d).decode('utf-8')
        while obj.unused_data != b'':
            unused_data = obj.unused_data
            obj = zlib.decompressobj(MAGIC_NUMBER + zlib.MAX_WBITS)
            yield obj.decompress(unused_data).decode('utf-8')
        d = raw_data_fd.read(CHUNK_SIZE)
    yield obj.flush().decode('utf-8') + ']' 
Example #15
Source File: response.py    From core with MIT License 6 votes vote down vote up
def decompress(self, data):
        if not data:
            return data

        if not self._first_try:
            return self._obj.decompress(data)

        self._data += data
        try:
            decompressed = self._obj.decompress(data)
            if decompressed:
                self._first_try = False
                self._data = None
            return decompressed
        except zlib.error:
            self._first_try = False
            self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
            try:
                return self.decompress(self._data)
            finally:
                self._data = None 
Example #16
Source File: web.py    From Quiver-alfred with MIT License 6 votes vote down vote up
def content(self):
        """Raw content of response (i.e. bytes).

        :returns: Body of HTTP response
        :rtype: :class:`str`

        """
        if not self._content:

            # Decompress gzipped content
            if self._gzipped:
                decoder = zlib.decompressobj(16 + zlib.MAX_WBITS)
                self._content = decoder.decompress(self.raw.read())

            else:
                self._content = self.raw.read()

            self._content_loaded = True

        return self._content 
Example #17
Source File: response.py    From ServerlessCrawler-VancouverRealState with MIT License 6 votes vote down vote up
def decompress(self, data):
        if not data:
            return data

        if not self._first_try:
            return self._obj.decompress(data)

        self._data += data
        try:
            decompressed = self._obj.decompress(data)
            if decompressed:
                self._first_try = False
                self._data = None
            return decompressed
        except zlib.error:
            self._first_try = False
            self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
            try:
                return self.decompress(self._data)
            finally:
                self._data = None 
Example #18
Source File: response.py    From vulscan with MIT License 6 votes vote down vote up
def decompress(self, data):
        if not data:
            return data

        if not self._first_try:
            return self._obj.decompress(data)

        self._data += data
        try:
            return self._obj.decompress(data)
        except zlib.error:
            self._first_try = False
            self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
            try:
                return self.decompress(self._data)
            finally:
                self._data = None 
Example #19
Source File: response.py    From ServerlessCrawler-VancouverRealState with MIT License 6 votes vote down vote up
def decompress(self, data):
        if not data:
            return data

        if not self._first_try:
            return self._obj.decompress(data)

        self._data += data
        try:
            decompressed = self._obj.decompress(data)
            if decompressed:
                self._first_try = False
                self._data = None
            return decompressed
        except zlib.error:
            self._first_try = False
            self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
            try:
                return self.decompress(self._data)
            finally:
                self._data = None 
Example #20
Source File: response.py    From plugin.video.emby with GNU General Public License v3.0 6 votes vote down vote up
def decompress(self, data):
        if not data:
            return data

        if not self._first_try:
            return self._obj.decompress(data)

        self._data += data
        try:
            return self._obj.decompress(data)
        except zlib.error:
            self._first_try = False
            self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
            try:
                return self.decompress(self._data)
            finally:
                self._data = None 
Example #21
Source File: response.py    From jbox with MIT License 5 votes vote down vote up
def __init__(self):
        self._first_try = True
        self._data = binary_type()
        self._obj = zlib.decompressobj() 
Example #22
Source File: html.py    From bilibiliupload with MIT License 5 votes vote down vote up
def undeflate(data):
    """Decompresses data for Content-Encoding: deflate.
    (the zlib compression is used.)
    """
    import zlib
    decompressobj = zlib.decompressobj(-zlib.MAX_WBITS)
    return decompressobj.decompress(data)+decompressobj.flush() 
Example #23
Source File: response.py    From plugin.video.emby with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self._obj = zlib.decompressobj(16 + zlib.MAX_WBITS) 
Example #24
Source File: decrypter.py    From SafeInCloud with GNU General Public License v3.0 5 votes vote down vote up
def decrypt( self ):
        #print "@ Decrypting %s ..." % self.db_filename

        self.input = open( self.db_filename, 'rb' )

        magic  = self.__read_short()
        sver   = self.__read_byte()
        salt   = self.__read_bytearray()

        # print "  - [PBKDF2] Deriving first key ..."

        skey   = self.__derive( self.password, salt )
        iv     = self.__read_bytearray()
        cipher = AES.new( skey, AES.MODE_CBC, iv )
        salt2  = self.__read_bytearray()
        block  = self.__read_bytearray()
        decr   = cipher.decrypt(block)
        sub_fd = StringIO.StringIO(decr)
        iv2    = self.__read_bytearray( sub_fd )
        pass2  = self.__read_bytearray( sub_fd )
        check  = self.__read_bytearray( sub_fd )

        # print "  - [PBKDF2] Deriving second key ..."

        skey2  = self.__derive( pass2, salt2, 1000 )
        cipher = AES.new( pass2, AES.MODE_CBC, iv2 )
        data   = cipher.decrypt( self.input.read() )

        # print "@ Decompressing ..."
        decompressor = zlib.decompressobj()
        return decompressor.decompress(data) + decompressor.flush() 
Example #25
Source File: test_zlib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_gzip_stream(self):
        """gzip header, uncomplete header"""
        for delta in xrange(1, 25):
            do = zlib.decompressobj(zlib.MAX_WBITS | 16)
            bufs = []
            for i in xrange(0, len(self.gzip_data), delta):
                bufs.append(do.decompress(self.gzip_data[i:i+delta]))
                self.assertEqual(len(do.unconsumed_tail), 0)
            bufs.append(do.flush())
            self.assertEqual(b"".join(bufs), self.text) 
Example #26
Source File: response.py    From recruit with Apache License 2.0 5 votes vote down vote up
def decompress(self, data):
        if not data:
            return data

        if not self._first_try:
            return self._obj.decompress(data)

        self._data += data
        try:
            return self._obj.decompress(data)
        except zlib.error:
            self._first_try = False
            self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
            try:
                return self.decompress(self._data)
            finally:
                self._data = None 
Example #27
Source File: test_zlib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_header_auto_detect(self):
        """autodetect zlib and gzip header"""
        do = zlib.decompressobj(zlib.MAX_WBITS | 32)
        self.assertEqual(do.decompress(self.gzip_data), self.text)
        do = zlib.decompressobj(zlib.MAX_WBITS | 32)
        self.assertEqual(do.decompress(self.zlib_data), self.text)
        self.assertEqual(zlib.decompress(self.gzip_data, zlib.MAX_WBITS | 32), self.text)
        self.assertEqual(zlib.decompress(self.zlib_data, zlib.MAX_WBITS | 32), self.text) 
Example #28
Source File: response.py    From recruit with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self._obj = zlib.decompressobj(16 + zlib.MAX_WBITS) 
Example #29
Source File: test_zlib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_gzip(self):
        """decompression with gzip header"""
        do = zlib.decompressobj(zlib.MAX_WBITS | 16)
        self.assertEqual(do.decompress(self.gzip_data), self.text)
        self.assertEqual(zlib.decompress(self.gzip_data, zlib.MAX_WBITS | 16), self.text) 
Example #30
Source File: response.py    From vulscan with MIT License 5 votes vote down vote up
def __init__(self):
        self._obj = zlib.decompressobj(16 + zlib.MAX_WBITS)