ManagedLibDeflate 0.1.0-alpha

This is a prerelease version of ManagedLibDeflate.
dotnet add package ManagedLibDeflate --version 0.1.0-alpha
                    
NuGet\Install-Package ManagedLibDeflate -Version 0.1.0-alpha
                    
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="ManagedLibDeflate" Version="0.1.0-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ManagedLibDeflate" Version="0.1.0-alpha" />
                    
Directory.Packages.props
<PackageReference Include="ManagedLibDeflate" />
                    
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 ManagedLibDeflate --version 0.1.0-alpha
                    
#r "nuget: ManagedLibDeflate, 0.1.0-alpha"
                    
#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 ManagedLibDeflate@0.1.0-alpha
                    
#: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=ManagedLibDeflate&version=0.1.0-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ManagedLibDeflate&version=0.1.0-alpha&prerelease
                    
Install as a Cake Tool

ManagedLibDeflate

⚠️ 0.x 阶段公共 API 不承诺稳定。 0.2.0-beta 将引入大量破坏性变更(详见 发布计划 §2.2)。生产环境请等待 1.0.0

libdeflate 的纯托管 C# 移植——DEFLATE / zlib / gzip 压缩与解压,零原生依赖,SIMD 加速。

这是什么

ManagedLibDeflatelibdeflate(C 语言高性能 DEFLATE 库)的纯 C# 移植版。与 LibDeflate.Native 等原生绑定包不同,它:

  • 零原生依赖:不 P/Invoke 任何 native DLL,纯 IL,开箱即用,AOT 友好
  • SIMD 加速:使用 System.Runtime.Intrinsics(Vector256 / Vector512)加速匹配与拷贝
  • 多目标框架net8.0net10.0
  • API 简洁Compress / Decompress 核心方法,语义与 libdeflate 原版一致

支持矩阵

维度 支持
目标框架 net8.0net10.0
操作系统 Windows / Linux / macOS(随 .NET 运行时)
压缩格式 DEFLATE(raw)、zlib、gzip
API 稳定性 ⚠️ 0.x 不承诺稳定;1.0.0 起冻结并遵循 SemVer

安装

dotnet add package ManagedLibDeflate

快速上手

解压 raw DEFLATE

using ManagedLibDeflate;

var decompressor = new LibDeflateDecompressor();
// compressed = raw DEFLATE 字节(无 zlib 头)
// output    = 预分配的输出缓冲区
int actualBytes = decompressor.Decompress(compressed, output);

解压(用静态实例,免构造)

int actualBytes = LibDeflateDecompressor.DecompressStatic(compressed, output);

压缩为 raw DEFLATE

using var compressor = new LibDeflateCompressor(level: 6);
int bound = LibDeflateCompressor.CompressBound(src.Length);
var dst = new byte[bound];
int compressedLen = compressor.Compress(src, dst);
var result = dst.AsSpan(0, compressedLen).ToArray();

压缩为 zlib 格式(2 字节头 + Adler-32 校验)

int compressedLen = compressor.CompressZlib(src, dst);

流式压缩 / 解压(同步)

// 压缩
using var outStream = new MemoryStream();
using (var zs = new LibDeflateStream(outStream, CompressionLevel.Optimal))
{
    zs.Write(src, 0, src.Length);
}
byte[] compressed = outStream.ToArray();

// 解压
using var inStream = new MemoryStream(compressed);
using var zs = new LibDeflateStream(inStream, CompressionMode.Decompress);
zs.Read(output, 0, output.Length);

0.1 仅提供 LibDeflateStream同步 API。异步重写与 ViaPipeAsync 管道将在 0.2.0-beta 移入实验性的 LibDeflatePipeline(见 发布计划 §2.2)。

API 速览

LibDeflateDecompressor

成员 说明
Decompress(ReadOnlySpan<byte> input, Span<byte> output) 解压 raw DEFLATE,返回写入字节数
static DecompressStatic(ReadOnlySpan<byte> input, Span<byte> output) 用线程静态实例解压(免构造)

LibDeflateCompressor : IDisposable

成员 说明
LibDeflateCompressor(int level = 1, CompressionLevel strategy = CompressionLevel.Optimal) 构造压缩器
Compress(ReadOnlySpan<byte> src, Span<byte> dst) 压缩为 raw DEFLATE,返回压缩字节数
CompressZlib(ReadOnlySpan<byte> src, Span<byte> dst) 压缩为 zlib 格式(含头与校验)
static CompressBound(int srcLen) 计算压缩输出上限,用于预分配缓冲区
Dispose() 释放资源

压缩级别

level 说明
0 仅存储(不压缩)
1 最快(ht matchfinder)
2-4 贪心(greedy)
5-7 惰性(lazy)
8-12 惰性2(lazy2)

LibDeflateStream : Stream(同步,0.1)

成员 说明
LibDeflateStream(Stream, CompressionLevel, bool leaveOpen = false) 构造压缩流
LibDeflateStream(Stream, CompressionMode, bool leaveOpen = false) 构造压缩 / 解压流
Write / Read / Flush + 标准 Stream 重写 同步流式读写

CompressionFormat 枚举

Deflate / Zlib / Gzip。0.1 已暴露该枚举;统一的 Compress(src, dst, format) 入口将在 0.2.0-beta(A6)提供。

版本与路线图

版本 定位
0.1.0-alpha(当前) 发布链路验证,公共 API 最小面
0.2.0-beta 一次性 API 破坏性总成(A1–A6、A11)
1.0.0 API 冻结,SemVer 生效,稳定版

完整逐版本接口清单与时间线见发布计划(内部规划文档,随本地 internal/ 仓库维护,不发布到 GitHub)。

许可

MIT

Product Compatible and additional computed target framework versions.
.NET 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 is compatible.  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. 
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.1.0-alpha 68 7/31/2026

0.1.0-alpha:首个预览版,发布链路验证。API 未冻结(0.2.0-beta 将引入破坏性变更),请勿用于生产。