百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Z

ZstdNet

> 编程语言
开源

适用于 .NET 的 Zstd 封装

340 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

适用于 .NET 的 Zstd 封装

ZstdNet  

ZstdNet is a wrapper of Zstd native library for .NET languages targeting netstadard2.{0|1}. ZstdNet NuGet package includes pre-built native shared libraries for various platforms, including win, linux, osx. For win, the static VC runtime is used.

The package relies on the dotnet runtime identifier resolution mechanism. For .NET Framework, the package provides a targets fallback, which requires an explicit selection of the platform — x86, x64 or ARM64).

If you need to resolve native dependency at runtime, you can use the NativeLibrary.SetDllImportResolver (see an example). And for .NET Framework — SetDllDirectory from kernel32.dll.

Provenance attestation is enabled for all artifacts in this repository including native libs, see page Attestations.

Features

  • Compression and decompression of byte arrays and Span
  • Streaming compression and decompression
  • Advanced parameters support
  • Generation of dictionaries

Take a look on a library reference or unit tests to explore its behavior in different situations.

Zstd

Zstandard, or zstd as short version, is a fast lossless compression algorithm, targeting real-time compression scenarios at zlib-level and better compression ratios.

Zstd is initially developed by Yann Collet and the source is available at: https://github.com/facebook/zstd

The motivation to develop of the algorithm, ways of use and its properties are explained in the blog that introduces the library: http://fastcompression.blogspot.com/2015/01/zstd-stronger-compression-algorithm.html

The benefits of the dictionary mode are described here: http://fastcompression.blogspot.ru/2016/02/compressing-small-data.html

Reference

Exceptions

The wrapper throws ZstdException in case of malformed data or an error inside libzstd. If the given destination buffer is too small, ZstdException with ZSTD_error_dstSize_tooSmall error code is thrown away.

Check zstd_errors.h for more info.

Compressor class

Block compression implementation. Instances of this class are not thread-safe. Consider using ThreadStatic or pool of compressors for bulk processing.

  • Constructor allow specifying compression options. Otherwise, default values will be used for CompressionOptions.

    csharp
    Compressor();
    Compressor(CompressionOptions options);

    Options will be exposed in Options read-only field.

    Note that Compressor class implements IDisposable. If you use a lot of instances of this class, it's recommended to call Dispose to avoid loading on the finalizer thread. For example:

    csharp
    using var compressor = new Compressor();
    var compressedData = compressor.Wrap(sourceData);
  • Wrap compress data and save it in a new or an existing buffer (in such case a length of saved data will be returned).

    csharp
    byte[] Wrap(byte[] src);
    byte[] Wrap(ArraySegment src);
    byte[] Wrap(ReadOnlySpan src);
    int Wrap(byte[] src, byte[] dst, int offset);
    int Wrap(ArraySegment src, byte[] dst, int offset);
    int Wrap(ReadOnlySpan src, byte[] dst, int offset);
    int Wrap(ReadOnlySpan src, Span dst);

    Note that on buffers close to 2GB Wrap tries its best, but if src is uncompressible and its size is too large, ZSTD_error_dstSize_tooSmall will be thrown. Wrap method call will only be reliable for a buffer size such that `GetCompressBoundLong(size) advancedParams, int compressionLevel = DefaultCompressionLevel);

…

c# using var options = new CompressionOptions(dict, compressionLevel: 5); using var compressor = new Compressor(options); var compressedData = compressor.Wrap(sourceData);


### Decompressor class

Block decompression implementation. Instances of this class are **not thread-safe**.
Consider using `ThreadStatic` or pool of decompressors for bulk processing.

* Constructor allow specifying decompression options. Otherwise, default values for `DecompressionOptions` will be used.

```c#
new Decompressor();
new Decompressor(DecompressionOptions options);

Options will be exposed in Options read-only field.

Note that Decompressor class implements IDisposable. If you use a lot of instances of this class, it's recommended to call Dispose to avoid loading on the finalizer thread. For example:

csharp
using var decompressor = new Decompressor();
var decompressedData = decompressor.Unwrap(compressedData);
  • Unwrap decompress data and save it in a new or an existing buffer (in such case a length of saved data will be returned).

    csharp
    byte[] Unwrap(byte[] src, int maxDecompressedSize = int.MaxValue);
    byte[] Unwrap(ArraySegment src, int maxDecompressedSize = int.MaxValue);
    byte[] Unwrap(ReadOnlySpan src, int maxDecompressedSize = int.MaxValue);
    int Unwrap(byte[] src, byte[] dst, int offset, bool bufferSizePrecheck = true);
    int Unwrap(ArraySegment src, byte[] dst, int offset, bool bufferSizePrecheck = true);
    int Unwrap(ReadOnlySpan src, byte[] dst, int offset, bool bufferSizePrecheck = true);
    int Unwrap(ReadOnlySpan src, Span dst, bool bufferSizePrecheck = true);
…

c# static ulong GetDecompressedSize(byte[] src); static ulong GetDecompressedSize(ArraySegment src); static ulong GetDecompressedSize(ReadOnlySpan src);


### DecompressionStream class

Implementation of streaming decompression. The stream is read-only.

* Constructor

```c#
DecompressionStream(Stream stream);
DecompressionStream(Stream stream, int bufferSize);
DecompressionStream(Stream stream, DecompressionOptions options, int bufferSize = 0);

…

c#
  await using var decompressionStream = new DecompressionStream(inputStream, zstdBufferSize);
  await decompressionStream.CopyToAsync(outputStream, copyBufferSize);

DecompressionOptions class

Stores decompression options and "digested" (for decompression) information from a compression dictionary, if present. Instances of this class are thread-safe. They can be shared across threads to avoid performance and memory overhead.

  • Constructor

    csharp
    DecompressionOptions();
    DecompressionOptions(byte[] dict);
    DecompressionOptions(byte[] dict, IReadOnlyDictionary advancedParams);
…

c# using var options = new DecompressionOptions(dict); using var decompressor = new Decompressor(options); var decompressedData = decompressor.Unwrap(compressedData);


### DictBuilder static class

* `TrainFromBuffer` generates a compression dictionary from a collection of samples.

```c#
static byte[] TrainFromBuffer(IEnumerable samples, int dictCapacity = DefaultDictCapacity);

Options: - int dictCapacity — maximal dictionary size in bytes. Default is DictBuilder.DefaultDictCapacity, currently 110 KiB (the default in zstd utility).

Wrapper Authors

Copyright (c) 2016-2026 SKB Kontur

ZstdNet is distributed under BSD 3-Clause License.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#compressioncsharpdotnetzstandard

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言