ZstdSeekable 0.1.0
See the version list below for details.
dotnet add package ZstdSeekable --version 0.1.0
NuGet\Install-Package ZstdSeekable -Version 0.1.0
<PackageReference Include="ZstdSeekable" Version="0.1.0" />
<PackageVersion Include="ZstdSeekable" Version="0.1.0" />
<PackageReference Include="ZstdSeekable" />
paket add ZstdSeekable --version 0.1.0
#r "nuget: ZstdSeekable, 0.1.0"
#:package ZstdSeekable@0.1.0
#addin nuget:?package=ZstdSeekable&version=0.1.0
#tool nuget:?package=ZstdSeekable&version=0.1.0
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:
- Scans the stream once, planting a candidate resume point roughly every
TargetSpanBytesof output. Each candidate carries a snapshot of the decoder window and is trial-decoded in parallel with the true decode for 4 MB. - Verifies every span: each candidate is re-decoded from its resume point exactly as readers will, and byte-checked (MD5) against the true decode across its entire span. Unsound points are dropped and their neighbours re-verified over the merged span.
Frame starts are stateless and always sound, so the build never fails on valid zstd input — the worst case is a coarser index, never wrong bytes. Readers additionally never serve bytes beyond a point's verified span.
The index file format (.zsi) is compact: point records plus zstd-compressed window snapshots, loaded lazily on cold seeks.
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.
ZstdIndexfiles are compatible with the.zsiindex files produced by clonezilla-util, where this mechanism originated.
License
MIT
| Product | Versions 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. |
-
.NETStandard 2.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- System.Memory (>= 4.6.0)
- ZstdSharp.Port (>= 0.8.8)
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- ZstdSharp.Port (>= 0.8.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.