• Stars
    star
    276
  • Rank 149,319 (Top 3 %)
  • Language
    C#
  • License
    Other
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Zstd wrapper for .NET

ZstdNet

NuGet

ZstdNet is a wrapper of Zstd native library for .NET languages. It has the following features:

  • Compression and decompression of byte arrays
  • Streaming compression and decompression
  • Generation of Dictionaries from a collection of samples

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

Zstd

Zstd, short for Zstandard, is a fast lossless compression algorithm, which provides both good compression ratio and speed for your standard compression needs. "Standard" translates into everyday situations which neither look for highest possible ratio (which LZMA and ZPAQ cover) nor extreme speeds (which LZ4 covers). Zstandard is licensed under BSD 3-Clause License.

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

Requirements

ZstdNet requires libzstd >= v1.4.0. Both 32-bit and 64-bit versions are supported. The corresponding DLLs are included in this repository cross-compiled using (i686|x86_64)-w64-mingw32-gcc -DZSTD_MULTITHREAD -DZSTD_LEGACY_SUPPORT=0 -pthread -s. Note that ZSTD_LEGACY_SUPPORT=0 means "do not support legacy formats" to minimize the binary size.

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.

    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<byte> src);
    byte[] Wrap(ReadOnlySpan<byte> src);
    int Wrap(byte[] src, byte[] dst, int offset);
    int Wrap(ArraySegment<byte> src, byte[] dst, int offset);
    int Wrap(ReadOnlySpan<byte> src, byte[] dst, int offset);
    int Wrap(ReadOnlySpan<byte> src, Span<byte> 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) <= 0x7FFFFFC7. Consider using streaming compression API on large data inputs.

  • GetCompressBound returns required destination buffer size for source data of size size.

    static int GetCompressBound(int size);
    static ulong GetCompressBoundLong(ulong size);

CompressionStream class

Implementation of streaming compression. The stream is write-only.

  • Constructor

    CompressionStream(Stream stream);
    CompressionStream(Stream stream, int bufferSize);
    CompressionStream(Stream stream, CompressionOptions options, int bufferSize = 0);

    Options:

    • Stream stream — output stream for writing compressed data.
    • CompressionOptions options Default is CompressionOptions.Default with default compression level.
    • int bufferSize — buffer size used for compression buffer. Default is the result of calling ZSTD_CStreamOutSize which guarantees to successfully flush at least one complete compressed block (currently ~128KB).

    The buffer for compression is allocated using ArrayPool<byte>.Shared.Rent().

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

    await using var compressionStream = new CompressionStream(outputStream, zstdBufferSize);
    await inputStream.CopyToAsync(compressionStream, copyBufferSize);

CompressionOptions class

Stores compression options and "digested" (for compression) 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

    CompressionOptions(int compressionLevel);
    CompressionOptions(byte[] dict, int compressionLevel = DefaultCompressionLevel);
    CompressionOptions(byte[] dict, IReadOnlyDictionary<ZSTD_cParameter, int> advancedParams, int compressionLevel = DefaultCompressionLevel);

    Options:

    • byte[] dict — compression dictionary. It can be read from a file or generated with DictBuilder class. Default is null (no dictionary).
    • int compressionLevel — compression level. Should be in range from CompressionOptions.MinCompressionLevel to CompressionOptions.MaxCompressionLevel (currently 22). Default is CompressionOptions.DefaultCompressionLevel (currently 3).
    • IReadOnlyDictionary<ZSTD_cParameter, int> advancedParams — advanced API provides a way to set specific parameters during compression. For example, it allows you to compress with multiple threads, enable long distance matching mode and more. Check zstd.h for additional info.

    Specified options will be exposed in read-only fields.

    Note that CompressionOptions 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 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.

    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<byte> src, int maxDecompressedSize = int.MaxValue);
    byte[] Unwrap(ReadOnlySpan<byte> src, int maxDecompressedSize = int.MaxValue);
    int Unwrap(byte[] src, byte[] dst, int offset, bool bufferSizePrecheck = true);
    int Unwrap(ArraySegment<byte> src, byte[] dst, int offset, bool bufferSizePrecheck = true);
    int Unwrap(ReadOnlySpan<byte> src, byte[] dst, int offset, bool bufferSizePrecheck = true);
    int Unwrap(ReadOnlySpan<byte> src, Span<byte> dst, bool bufferSizePrecheck = true);

    Data can be saved to a new buffer only if a field with decompressed data size is present in compressed data. You can limit size of the new buffer with maxDecompressedSize parameter (it's necessary to do this on untrusted data).

    If bufferSizePrecheck flag is set and the decompressed field length is specified, the size of the destination buffer will be checked before actual decompression.

    Note that if this field is malformed (and is less than actual decompressed data size), libzstd still doesn't allow a buffer overflow to happen during decompression.

  • GetDecompressedSize reads a field with decompressed data size stored in compressed data.

    static ulong GetDecompressedSize(byte[] src);
    static ulong GetDecompressedSize(ArraySegment<byte> src);
    static ulong GetDecompressedSize(ReadOnlySpan<byte> src);

DecompressionStream class

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

  • Constructor

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

    Options:

    • Stream stream — input stream for reading raw data.
    • DecompressionOptions options Default is null (no dictionary).
    • int bufferSize — buffer size used for decompression buffer. Default is the result of calling ZSTD_DStreamInSize — recommended size for input buffer (currently ~128KB).

    The buffer for decompression is allocated using ArrayPool<byte>.Shared.Rent().

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

    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

    DecompressionOptions();
    DecompressionOptions(byte[] dict);
    DecompressionOptions(byte[] dict, IReadOnlyDictionary<ZSTD_dParameter, int> advancedParams);

    Options:

    • byte[] dict — compression dictionary. It can be read from a file or generated with DictBuilder class. Default is null (no dictionary).
    • IReadOnlyDictionary<ZSTD_dParameter, int> advancedParams — advanced decompression API that allows you to set parameters like maximum memory usage. Check zstd.h for additional info.

    Specified options will be exposed in read-only fields.

    Note that CompressionOptions 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 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.

    static byte[] TrainFromBuffer(IEnumerable<byte[]> 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-present SKB Kontur

ZstdNet is distributed under BSD 3-Clause License.

More Repositories

1

retail-ui

React controls to implement standards at https://guides.kontur.ru/
TypeScript
245
star
2

dotEducation

База знаний для .NET разработчиков
215
star
3

gremit

.NET Reflection.Emit extensions
C#
161
star
4

GroBuf

Fast binary serializer
C#
61
star
5

cement

C# dependency management tool
C#
55
star
6

TypeScript.ContractGenerator

A tool that can generate TypeScript types from C# classes
C#
51
star
7

extern-csharp-sdk

SDK для работы с API Контур.Экстерна
C#
43
star
8

extern-java-sdk

SDK для работы с API Контур.Экстерна для платформы JVM
Java
27
star
9

project-calendar

Календари на каждый месяц от дизайнеров Контура
26
star
10

contests

C#
25
star
11

frontreport

Simple frontend logging collector written in Go
Go
24
star
12

GrobExp.Compiler

Efficient compiler of .NET expression trees
C#
21
star
13

results

C#
17
star
14

simple-container

Yet another DI container
C#
14
star
15

AoC2021

Advent of Code 2021 solutions from our community
1C Enterprise
12
star
16

react-ui-tour

Tours system for retail-ui
TypeScript
12
star
17

AoC2022

Advent of Code 2022 solutions from our community
Python
9
star
18

Selone

Flexible Selenium WebDriver extension
C#
9
star
19

GroboTrace

Lightweight .NET performance profiler
C#
9
star
20

markdown

TypeScript
8
star
21

openvpn-auth-radius

C# authentication plugin for OpenVPN
C#
8
star
22

perl-jsonschema-validator

JSON Schema and OpenAPI data validator for Perl
Perl
8
star
23

react-stack-layout

Flexbox based stack layouts for react
TypeScript
8
star
24

WiredTigerNet

C++/CLI wrapper for WiredTiger key-value engine
C
7
star
25

elopen

Kibana plugin for smooth opening of Elasticsearch indexes
JavaScript
6
star
26

cspreport

Simple CSP and HPKP report collector written in Go
Go
6
star
27

AoC2020

Advent of Code 2020 solutions by SKB Kontur's developers
C#
5
star
28

react-ui-testing

Page objects and useful extensions for testing applications based on react-ui control
C#
5
star
29

Excel.TemplateEngine

Engine for printing Excel documents with templates
C#
5
star
30

db-viewer

Database Viewer with custom configuration
C#
5
star
31

edi-api-client

EDI API client
C#
4
star
32

searchpicker

Searchpicker or combo-box dropdown ui form component, written in typescript
TypeScript
4
star
33

OctoPnP

TeamCity plugin to support Octopus-based deployment process
Java
4
star
34

extern-api-docs

Документация по работе с API Контур.Экстерна
4
star
35

react-ui-validations

Репозиторий переехал в skbkontur/retail-ui
JavaScript
4
star
36

AoC2023

Advent of Code 2023 solutions from our community
3
star
37

django-ru-validators

Django RU financial validators
Python
3
star
38

ofd-api-docs

3
star
39

cassandra-thrift-client

.NET client for Apache Cassandra thrift interface
C#
3
star
40

serilog-enrichers-exceptionstacktracehash

C#
3
star
41

cassandra-distributed-task-queue

.NET library implementing distributed task queue machinery using Apache Cassandra
C#
3
star
42

angular-router-ex

TypeScript
3
star
43

GrobExp.Mutators

Code generation mechanics for documents conversion and validation
C#
3
star
44

billing-ux-starter

CSS
2
star
45

edi-api-docs

Python
2
star
46

GroboContainer

Simple DI-container
C#
2
star
47

kontur.lz4.net

C#
2
star
48

perl-dbd-avatica

DBI driver for Apache Avatica
Perl
2
star
49

react-ui

React UI components doc
HTML
2
star
50

cajrr

Cassandra Java Range Repair service
Java
2
star
51

portal-oidc-docs

Python
2
star
52

highload-2017-quiz

Задание для участников HighLoad++ 2017
Go
1
star
53

GroboContainer.NUnitExtensions

NUnit extensions simplifying DI-container management in tests
C#
1
star
54

serilog-sinks-elkstreams

C#
1
star
55

oidc-client

1
star
56

react-sorge

TypeScript
1
star
57

react-props2attrs

TypeScript
1
star
58

KFixture

Generate test data
Kotlin
1
star
59

spring-reactive-mongo-soft-delete

SoftDelete with mongo for spring
Kotlin
1
star
60

docker-kibana

Custom Kibana image with plugins
Shell
1
star