Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ jobs:
using GeneralZip;
using static GeneralZip.GeneralZip;

Initialize();
// 无任何显式引导调用:验证零初始化的隐式引导端到端可用
Console.WriteLine($"native={GeneralZip.Native.NativeCoreLoader.IsAvailable}");

var dir = Path.Combine(Path.GetTempPath(), "gzp-smoke");
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Nullable>enable</Nullable>
<Deterministic>true</Deterministic>
<Authors>GeneralZip</Authors>
<Version>1.2.0</Version>
<Version>2.0.0</Version>
<Company>GeneralZip</Company>
<Product>GeneralZip</Product>
<!-- Deterministic CI build; inject the commit hash via MinVer-style CI_VERSION if present -->
Expand Down
12 changes: 6 additions & 6 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ dotnet add package GeneralZip
Targets `netstandard2.0` and `net8.0`.

> One package, everything included. The library bundles prebuilt Rust native cores for
> win-x64, linux-x64, linux-musl-x64, osx-x64 and osx-arm64; `GeneralZip.Initialize()`
> loads them automatically. If the core is missing for your platform, 7z/RAR and the
> native codecs degrade gracefully; everything else keeps working.
> win-x64, linux-x64, linux-musl-x64, osx-x64 and osx-arm64; they are detected and loaded
> automatically on first use — no initialization call needed. If the core is missing for
> your platform, 7z/RAR and the native codecs degrade gracefully; everything else keeps
> working.

## Quick Start

Expand All @@ -44,8 +45,6 @@ Targets `netstandard2.0` and `net8.0`.
```csharp
using GeneralZip;

GeneralZip.Initialize(); // idempotent bootstrap

// Directory → archive (format is chosen by the file extension)
await GeneralZip.CompressAsync(@"C:\data", @"D:\backup.zip");

Expand Down Expand Up @@ -187,7 +186,8 @@ Place `generalzip_native.dll/.so/.dylib` in one of:
2. `runtimes/<RID>/native/` under the app directory,
3. any path referenced by the `GENERALZIP_NATIVE` environment variable,

then call `GeneralZip.Initialize()` once (already done implicitly by the one-shot APIs).
The core is picked up automatically the first time you use the library — no
initialization call is needed.

## Sample Files

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ dotnet add package GeneralZip
Targets `netstandard2.0` and `net8.0`.

> One package, everything included. The library bundles prebuilt Rust native cores for
> win-x64, linux-x64, linux-musl-x64, osx-x64 and osx-arm64; `GeneralZip.Initialize()`
> loads them automatically. If the core is missing for your platform, 7z/RAR and the
> native codecs degrade gracefully; everything else keeps working.
> win-x64, linux-x64, linux-musl-x64, osx-x64 and osx-arm64; they are detected and loaded
> automatically on first use — no initialization call needed. If the core is missing for
> your platform, 7z/RAR and the native codecs degrade gracefully; everything else keeps
> working.

## Quick Start

Expand All @@ -44,8 +45,6 @@ Targets `netstandard2.0` and `net8.0`.
```csharp
using GeneralZip;

GeneralZip.Initialize(); // idempotent bootstrap

// Directory → archive (format is chosen by the file extension)
await GeneralZip.CompressAsync(@"C:\data", @"D:\backup.zip");

Expand Down Expand Up @@ -189,7 +188,8 @@ Place `generalzip_native.dll/.so/.dylib` in one of:
2. `runtimes/<RID>/native/` under the app directory,
3. any path referenced by the `GENERALZIP_NATIVE` environment variable,

then call `GeneralZip.Initialize()` once (already done implicitly by the one-shot APIs).
The core is picked up automatically the first time you use the library — no
initialization call is needed.

## Sample Files

Expand Down
10 changes: 4 additions & 6 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dotnet add package GeneralZip
目标框架 `netstandard2.0` 与 `net8.0`。

> 一个包,能力齐全。包内已捆绑 win-x64、linux-x64、linux-musl-x64、osx-x64、osx-arm64
> 五个平台的预编译 Rust 原生核心,`GeneralZip.Initialize()` 会自动加载;
> 五个平台的预编译 Rust 原生核心,首次使用时自动探测并加载,**无需任何初始化调用**;
> 若平台无对应核心,7z/RAR 与原生编解码优雅降级,其余能力不受影响。

## 快速开始
Expand All @@ -43,8 +43,6 @@ dotnet add package GeneralZip
```csharp
using GeneralZip;

GeneralZip.Initialize(); // 幂等引导

// 目录 → 归档(格式由扩展名决定)
await GeneralZip.CompressAsync(@"C:\data", @"D:\backup.zip");

Expand Down Expand Up @@ -133,8 +131,8 @@ GeneralZip.Registry.Register(new MyArchiveFormat());
GeneralZip.Encryption.Register(new MyEncryptionProvider());
```

> 引导类与组件同名(`namespace GeneralZip` 中的 `class GeneralZip`)。
> 若需要与其它类型限定写法混用,可用 `using static GeneralZip.GeneralZip;` 裸调用 `Initialize()` / `CompressAsync()` 等。
> 门面类与组件同名(`namespace GeneralZip` 中的 `class GeneralZip`)。
> 若需要与其它类型限定写法混用,可用 `using static GeneralZip.GeneralZip;` 裸调用 `CompressAsync()` 等。

## 格式与加密矩阵

Expand Down Expand Up @@ -186,7 +184,7 @@ GeneralZip.Encryption.Register(new MyEncryptionProvider());
2. 应用目录下的 `runtimes/<RID>/native/`;
3. `GENERALZIP_NATIVE` 环境变量指向的路径,

然后启动时调用一次 `GeneralZip.Initialize()`(一行式 API 内部已自动调用)。
首次使用库时核心会被自动探测并加载,无需任何初始化调用。

## 示例文件

Expand Down
13 changes: 7 additions & 6 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

```
┌────────────────────────────────────────────────────────────┐
│ 门面层 GeneralZip (静态引导/门面) / │ 一行式与流式 API
│ 门面层 GeneralZip (静态门面) / │ 一行式与流式 API
│ GeneralZipReader / GeneralZipWriter │
├────────────────────────────────────────────────────────────┤
│ 事件层 EntryStarted / EntryProgress / EntryCompleted / │ 进度
Expand Down Expand Up @@ -65,10 +65,10 @@ GeneralZip.sln
│ │ ├─ Tar/ TAR / TAR.GZ / TAR.BZ2
│ │ └─ Codecs/ GZ/BZ2/XZ/Zstd/LZ4 单文件编解码格式
│ ├─ Native/ Rust 核心 P/Invoke 绑定(能力协商 + 降级)
│ ├─ GeneralZip.cs 启动引导(含原生自动装配)+ 静态门面
│ ├─ GeneralZip.cs 静态门面(零初始化)
│ ├─ ArchiveReader.cs 读取封装(事件源)
│ ├─ ArchiveWriter.cs 写入封装(事件源)
│ └─ ArchiveFormatRegistry.cs 格式注册表 + 魔数探测
│ └─ ArchiveFormatRegistry.cs 格式注册表 + 魔数探测(Default 装配时隐式原生引导)
├─ tests/
│ ├─ GeneralZip.Tests/ 核心测试(含互操作 oracle)
│ └─ GeneralZip.Native.Tests/ Rust 核心集成测试
Expand Down Expand Up @@ -301,14 +301,15 @@ gzp_close(handle) -> status
```
NativeCoreLoader.TryLoad()
├─ 按 RID 探测 runtimes/{rid}/native/generalzip_native.{dll|so|dylib}
│ 以及应用目录 / GENERALZIP_NATIVE_PATH 环境变量
│ 以及应用目录 / GENERALZIP_NATIVE 环境变量
├─ 载入成功 → gzp_capabilities() → 按 bit 注册 SevenZipFormat / RarFormat
└─ 载入失败 → 不注册;用户访问 7z/RAR 时抛
NativeCoreMissingException("...如何安装/部署 native core...")
```

原生绑定与托管核心同包发布:`GeneralZip.Initialize()` 自动调用
`GeneralZipNative.Initialize()`(幂等)完成能力注册;核心二进制缺失时仅降级、不抛异常。
原生绑定与托管核心同包发布,引导完全隐式:首次访问任何 API 时,
`GeneralZipFormatRegistry.Default` 的装配自动调用 `GeneralZipNative.Bootstrap()`
(幂等)完成能力注册与编解码桥接注入;核心二进制缺失时仅降级、不抛异常。

## 8. 关键流程

Expand Down
3 changes: 1 addition & 2 deletions samples/GeneralZip.Bench/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
var warmups = args.Length > 1 && int.TryParse(args[1], out var w) ? w : 1;
var runs = args.Length > 2 && int.TryParse(args[2], out var r) ? r : 3;

var nativeOk = GeneralZipNative.Initialize();
Initialize();
var nativeOk = NativeCoreLoader.TryLoad();

Console.WriteLine($"GeneralZip benchmark native={(nativeOk ? "available" : "missing (skips 7z/BZ2/XZ/ZSTD/LZ4)")}");
Console.WriteLine($"Runtime: {Environment.Version} OS: {Environment.OSVersion.VersionString} Logical cores: {Environment.ProcessorCount}");
Expand Down
3 changes: 1 addition & 2 deletions samples/GeneralZip.Fixtures/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
// 用法: dotnet run -c Release
// =====================================================================

Initialize(); // 幂等引导(using static 裸调用)
var nativeOk = GeneralZipNative.Initialize();
var nativeOk = NativeCoreLoader.TryLoad(); // 引导已隐式完成,此处仅探测核心可用性

var repoRoot = FindRepoRoot();
Env.FixturesDir = Path.Combine(repoRoot, "samples", "fixtures");
Expand Down
11 changes: 5 additions & 6 deletions samples/GeneralZip.Samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
// 演示用口令:运行时拼接派生(避免源码硬编码;实际应用请从密钥管理服务/用户输入获取)
static string DemoPassword(string label) => "GZ-" + label + "-Demo";

// ===== 0) 启动引导:核心注册表 + 可选原生核心 =====
Initialize(); // 幂等:确保内置格式/加密算法注册
if (GeneralZipNative.Initialize()) // 可选:Rust 原生核心(7z/xz 能力;缺失时优雅降级)
// ===== 0) 原生核心状态展示(引导已隐式完成,无需手动调用) =====
if (NativeCoreLoader.TryLoad()) // 可选:Rust 原生核心(7z/xz 能力;缺失时优雅降级)
{
Console.WriteLine($"[native core] v{GeneralZipNative.CoreVersion} loaded (7z/xz available)");
}
Expand All @@ -31,9 +30,9 @@
await CompressAsync(src, zip, o => o.Level = CompressionLevelChoice.Optimal);
Console.WriteLine($"[one-liner] compressed: {zip} ({new FileInfo(zip).Length}B, format {DetectFormat(zip)?.Name})");

// ===== 1.5) 启动引导说明 =====
// GeneralZip.Initialize() 为幂等引导入口(确保内置格式/算法注册,并自动加载内置 Rust 原生核心),
// 放在程序启动处调用一次即可;一行式 API 内部也会自动调用。
// ===== 1.5) 引导说明 =====
// GeneralZip 零初始化:内置格式/算法注册与 Rust 原生核心加载都在首次使用 API 时隐式完成
//(Default 注册表装配时自动引导),核心缺失时优雅降级,纯托管能力不受影响。

// ===== 2) 国密 SM4 加密 ZIP(加密写 → 解密读) =====
var sm4Zip = Path.Combine(work, "secret-sm4.zip");
Expand Down
13 changes: 12 additions & 1 deletion src/GeneralZip/ArchiveFormatRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ internal GeneralZipFormatRegistry(bool populateBuiltins)
}
}

public static GeneralZipFormatRegistry Default { get; } = new(populateBuiltins: true);
public static GeneralZipFormatRegistry Default { get; } = CreateDefault();

/// <summary>
/// 默认注册表装配:内置格式注册后即触发原生引导(幂等)。
/// 原生核心在位时追加 7z/RAR 并注入编解码桥接;缺失时静默降级,纯托管能力不受影响。
/// </summary>
private static GeneralZipFormatRegistry CreateDefault()
{
var registry = new GeneralZipFormatRegistry(populateBuiltins: true);
global::GeneralZip.Native.GeneralZipNative.Bootstrap(registry);
return registry;
}

/// <summary>注册格式提供者(重复注册同名格式将覆盖旧的)。</summary>
public void Register(IArchiveFormat format)
Expand Down
25 changes: 17 additions & 8 deletions src/GeneralZip/Formats/Codecs/BuiltinCodecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@ internal static class NativeCodecRouter
public static void EnsureCore()
{
// 通过桥接委托间接访问原生核心,保持编解码实现与 P/Invoke 细节的边界:
// GeneralZipNative.Initialize()(GeneralZip.Initialize() 已自动调用)注入 NativeBridge 的实现。
// NativeBridge 的实现由隐式引导注入(Default 注册表装配时自动调用 Bootstrap)。
if (!NativeBridgeProbe.IsAvailableSafe())
{
throw new NativeCoreMissingException(
"This codec format requires the Rust native core (generalzip_native), which was not found next to the application. " +
"Place generalzip_native.dll/.so/.dylib in the app directory or under runtimes/<RID>/native/, " +
"or re-save the archive as ZIP/TAR.GZ or another pure-managed format.");
// 兜底:默认注册表未被触碰时(自定义注册表场景)桥接尚未注入,显式拉起一次。
global::GeneralZip.Native.GeneralZipNative.EnsureBridge();
if (!NativeBridgeProbe.IsAvailableSafe())
{
throw new NativeCoreMissingException(
"This codec format requires the Rust native core (generalzip_native), which was not found next to the application. " +
"Place generalzip_native.dll/.so/.dylib in the app directory or under runtimes/<RID>/native/, " +
"or re-save the archive as ZIP/TAR.GZ or another pure-managed format.");
}
}
}

internal static bool IsCoreAvailable() => NativeBridge.IsAvailable?.Invoke() ?? false;
internal static bool IsCoreAvailable()
{
global::GeneralZip.Native.GeneralZipNative.EnsureBridge();
return NativeBridgeProbe.IsAvailableSafe();
}

internal static void Compress(string codec, string source, string destination)
{
Expand All @@ -48,12 +57,12 @@ internal static void Decompress(string codec, string source, string destination)
}

/// <summary>
/// 原生核心桥接点:GeneralZip.Native 包在 Initialize 时注入实现。
/// 原生核心桥接点:隐式引导(GeneralZipNative.Bootstrap)注入实现。
/// 核心包经此间接使用原生能力,保持"核心零 P/Invoke"的架构边界。
/// </summary>
public static class NativeBridge
{
/// <summary>原生核心是否已加载。GeneralZipNative.Initialize 注入。</summary>
/// <summary>原生核心是否已加载。隐式引导注入。</summary>
public static Func<bool>? IsAvailable { get; set; }

/// <summary>整文件编解码:codec ∈ {bzip2, xz, zstd, lz4}。返回是否成功(false = 不支持该 codec)。</summary>
Expand Down
29 changes: 2 additions & 27 deletions src/GeneralZip/GeneralZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,18 @@
namespace GeneralZip;

/// <summary>
/// 组件启动引导与门面入口(与组件同名)。
/// 静态门面入口(与组件同名,零初始化:内置格式/加密算法与原生能力均在首次使用时隐式装配)。
/// <para>
/// <see cref="Initialize"/> 完成内置格式/加密算法注册(幂等,可重复调用);
/// 随后即可使用一行式 API(<see cref="CompressAsync"/>/<see cref="ExtractAsync"/> 等),
/// 直接使用一行式 API(<see cref="CompressAsync"/>/<see cref="ExtractAsync"/> 等),
/// 需要事件与精细控制时使用 <see cref="GeneralZipReader"/> / <see cref="GeneralZipWriter"/>。
/// </para>
/// <code>
/// GeneralZip.Initialize();
/// await GeneralZip.CompressAsync(@"C:\data", @"D:\out.zip",
/// o => { o.EncryptionAlgorithm = "SM4-GM"; o.Password = "p@ss"; });
/// </code>
/// </summary>
public static class GeneralZip
{
private static int _initialized;

/// <summary>
/// 启动引导:确保内置格式提供者与加密算法已注册(幂等、线程安全)。
/// 同时尝试加载内置 Rust 原生核心(7z/RAR/原生编解码);核心二进制缺失时静默降级,
/// 纯托管能力不受影响(可用 <see cref="GeneralZip.Native.NativeCoreLoader.IsAvailable"/> 探测)。
/// </summary>
public static void Initialize()
{
if (Interlocked.Exchange(ref _initialized, 1) == 1)
{
return;
}

// 触发两个默认注册表的静态装配
_ = GeneralZipFormatRegistry.Default;
_ = GeneralZipEncryptionRegistry.Default;

// 原生引导(幂等;加载失败仅返回 false,不抛异常)。
// global:: 限定:类名 GeneralZip 会遮蔽同名命名空间。
_ = global::GeneralZip.Native.GeneralZipNative.Initialize();
}

/// <summary>组件版本。</summary>
public static Version Version => typeof(GeneralZip).Assembly.GetName().Version ?? new Version(0, 0, 0);

Expand Down
Loading
Loading