ZstdSeekable 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ZstdSeekable --version 0.2.0
                    
NuGet\Install-Package ZstdSeekable -Version 0.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ZstdSeekable" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZstdSeekable" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="ZstdSeekable" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ZstdSeekable --version 0.2.0
                    
#r "nuget: ZstdSeekable, 0.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ZstdSeekable@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ZstdSeekable&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=ZstdSeekable&version=0.2.0
                    
Install as a Cake Tool

ZstdSeekable

Random-access (seekable) reading of zstandard streams in .NET — plus writing of the official zstd seekable format.

Zstd streams are normally forward-only: reading byte N means decompressing everything before it. ZstdSeekable gives you a standard seekable Stream over zstd data using whichever of two mechanisms fits your input:

Official seekable format Custom index
Input Files written in the zstd seekable format (e.g. by t2sz, zstd's seekable API, or this library's writer): many small frames + a seek table at EOF Any ordinary zstd stream, including single giant frames (e.g. Clonezilla images)
Setup cost None — the seek table is parsed instantly One full sequential decode to build an index (persist it, and later opens are instant)
Seek cost Decode ≤ 1 frame Decode from the nearest resume point (bounded by the index's span size)

Everything is managed code (ZstdSharp) — no native binaries. Targets netstandard2.0 and net8.0.

Quick start

using ZstdSeekable;

// Auto-detect: uses the seek table if present, otherwise loads/builds a custom index
using var stream = ZstdSeekableStream.OpenRead(@"C:\images\sda1.zst", indexPath: null); // index defaults to sda1.zst.zsi

stream.Position = 123_456_789;          // seek anywhere
var buffer = new byte[4096];
var read = stream.Read(buffer, 0, buffer.Length);

Both the compressed input and the index can be files or streams, in any combination:

using var compressed = File.OpenRead("data.zst");
using var indexStream = new MemoryStream();          // or a FileStream, a database blob, ...
using var stream = ZstdSeekableStream.OpenRead(compressed, indexStream);

Writing seekable files

ZstdSeekableWriter produces official seekable-format output — readable by any zstd decompressor, and instantly seekable by any seekable-format implementation (including this library):

using (var writer = ZstdSeekableWriter.Create("data.zst",
        new ZstdSeekableWriterOptions { MaxFrameSize = 1024 * 1024, CompressionLevel = 3 }))
{
    source.CopyTo(writer);
}   // Dispose (or Finish()) appends the seek table

If you control how your files are compressed, prefer this — no index build is ever needed.

Reading seekable-format files directly

using var reader = ZstdSeekableReader.Open("data.zst",
        new ZstdSeekableReaderOptions { VerifyChecksums = true });
Console.WriteLine($"{reader.SeekTable.Entries.Count} frames, {reader.Length:N0} bytes uncompressed");

Indexing ordinary zstd streams

For zstd data that was not written in the seekable format, ZstdIndex builds a random-access index by decoding the stream once:

using var compressed = File.OpenRead("clonezilla-image.zst");

using var index = ZstdIndex.LoadOrBuild(compressed, "clonezilla-image.zst.zsi",
    new ZstdIndexOptions { TargetSpanBytes = 64L * 1024 * 1024 },
    progress: new Progress<ZstdIndexProgress>(p => Console.Write($"\r{p.Phase} {p.Fraction:P0}")));

using var stream = new ZstdIndexedStream(compressed, index);
stream.Position = 40L * 1024 * 1024 * 1024;     // seek into the 40 GB mark
How the index works (and why it's trustworthy)

A zstd block can depend on decoder state beyond the content window (repeat offsets, entropy tables), so a block boundary is only usable as a resume point if a resumed decode is bit-identical to the true decode. The build is a single sequential pass that verifies as it goes: two shadow decoders run alongside the true decode — one for the current candidate point, and an insurance shadow covering the last confirmed point's still-open span. A candidate that stays byte-identical for a whole span is confirmed, which seals its predecessor with its entire serving span proven. A diverging candidate (normal — roughly 12% of block boundaries) is simply re-armed at a later boundary.

Frame starts are stateless and always sound, so the build never fails on valid zstd input — in the theoretical worst case (an insurance divergence deeper than a whole span, never observed on real data) the index degrades to frame-start points only: coarser, never wrong. Readers additionally never serve bytes beyond a point's verified span.

Interrupted builds resume

When building to a file or seekable stream (ZstdIndex.LoadOrBuild), each sealed point is flushed the moment it's verified — build memory stays flat (~10 MB regardless of stream size), and if the build is interrupted (crash, cancellation, lost connection), the next LoadOrBuild resumes from the last sealed point instead of starting over.

The index format (.zsi, magic ZSTZRAN2) stores point records with their zstd-compressed window snapshots inline, loaded lazily on cold seeks. Indexes written by ZstdSeekable 0.1.x (ZSTZRAN1) still load.

Concurrency

Stream instances follow the usual BCL rule: one instance, one thread. For concurrent random access, call CreateView() on ZstdSeekableReader or ZstdIndexedStream — views are cheap independent cursors sharing the underlying source through a lock:

Parallel.For(0, 8, _ =>
{
    using var view = stream.CreateView();
    view.Position = ...;    // each view has its own position and cache
});

Notes

  • The compressed input must be seekable (CanSeek == true) — both mechanisms need random access to the compressed bytes.
  • An index loaded lazily from a seekable stream needs that stream to remain open while in use; indexes loaded from non-seekable streams are read eagerly into memory.
  • ZstdIndex files are compatible with the .zsi index files produced by clonezilla-util, where this mechanism originated.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.0 287 7/18/2026
0.3.0 154 7/11/2026
0.2.0 130 7/10/2026
0.1.0 141 7/10/2026

Index builds are now a single verified pass (~35% faster), use flat memory when building to a file/seekable stream, and resume after interruption. New ZSTZRAN2 index format (ZSTZRAN1 files still load).