适用于 .NET 的 Zstd 封装
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.
SpanTake a look on a library reference or unit tests to explore its behavior in different situations.
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
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.
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.
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:
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).
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:
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).
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);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
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).
Copyright (c) 2016-2026 SKB Kontur
ZstdNet is distributed under BSD 3-Clause License.
暂无开放 Issues,或尚未同步最近议题。