compress function

Uint8List compress(
  1. Uint8List src, {
  2. int level = Level.levelDefault,
})

Simple compression and decompression

Compresses src content as a single zstd compressed frame into already allocated dst. ZStdError throwed if failed. level is compression ratio 1-19, default 3

Implementation

Uint8List compress(Uint8List src, {int level = Level.levelDefault}) {
  _lib ??= ZStd(_openLibrary());

  final csrc = Uint8ArrayUtils.toPointer(src);

  final dstlen = _lib!.ZSTD_compressBound(src.length);
  final dst = calloc<ffi.Uint8>(dstlen);

  try {
    int reslen =
        _lib!.ZSTD_compress(dst.cast(), dstlen, csrc.cast(), src.length, level);
    if (_lib!.ZSTD_isError(reslen) > 0) {
      throw ZStdError.fromCode(reslen);
    }

    return Uint8ArrayUtils.fromPointer(dst, reslen);
  } finally {
    calloc.free(dst);
    calloc.free(csrc);
  }
}