blosc2.decompress#

blosc2.decompress(src, dst=None, as_bytearray=False)#

Decompresses a bytes-like compressed object.

Parameters:
  • src (bytes-like object) – The data to be decompressed. Must be a bytes-like object that supports the Python Buffer Protocol, like bytes, bytearray, memoryview, or numpy.ndarray.

  • dst (NumPy object or bytearray) – The destination NumPy object or bytearray to fill, the length of which must be greater than 0. The user must make sure that it has enough capacity for hosting the decompressed data. Default is None, meaning that a new bytes or bytearray object is created, filled and returned.

  • as_bytearray (bool (optional)) – If this flag is True then the return type will be a bytearray object instead of a bytes object.

Returns:

out – If dst is None, the decompressed data in form of a Python str / bytes object. If as_bytearray is True then this will be a bytearray object.

If dst is not None, it will return None because the result will already be in dst.

Return type:

str/bytes or bytearray

Raises:
  • RuntimeError – The compressed data is corrupted or the output buffer is not large enough. Could not get a bytes object.

  • TypeError – If src does not support Buffer Protocol.

  • ValueError – If the length of src is smaller than the minimum. If dst is not None and its length is 0.

Examples

>>> import array, sys
>>> a = array.array('i', range(1000*1000))
>>> a_bytesobj = a.tobytes()
>>> c_bytesobj = blosc2.compress(a_bytesobj, typesize=4)
>>> a_bytesobj2 = blosc2.decompress(c_bytesobj)
>>> a_bytesobj == a_bytesobj2
True
>>> b"" == blosc2.decompress(blosc2.compress(b""))
True
>>> b"1"*7 == blosc2.decompress(blosc2.compress(b"1"*7))
True
>>> type(blosc2.decompress(blosc2.compress(b"1"*7),
...                                      as_bytearray=True)) is bytearray
True
>>> import numpy
>>> arr = numpy.arange(10)
>>> comp_arr = blosc2.compress(arr)
>>> dest = numpy.empty(arr.shape, arr.dtype)
>>> blosc2.decompress(comp_arr, dst=dest)
>>> numpy.array_equal(arr, dest)
True