deimos-zstd 0.0.1

A minimal D application.


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

deimos-zstd

Zstandard (zstd) 压缩库的 D 语言绑定。

支持版本

Zstd 库版本:1.5.7

本绑定完整覆盖 zstd C API,包括:

  • 核心压缩/解压缩函数
  • 流式 API
  • 字典压缩
  • 高级参数
  • 字典构建器 API (zdict.h)

要求

  • D 编译器(DMD、LDC2 或 GDC)
  • 系统安装 Zstd 库 (libzstd)

安装

将此包添加到 dub.jsondub.sdl

dub.json:

"dependencies": {
    "deimos-zstd": "~>1.0.0"
}

dub.sdl:

dependency "deimos-zstd" version="~>1.0.0"

链接

需要链接 zstd 库。在大多数系统上:

dub.json:

"libs": ["zstd"]

dub.sdl:

libs "zstd"

Windows

在 Windows 上,您可以:

  1. zstd releases 下载预编译二进制文件
  2. 从源代码构建

确保 DLL 在 PATH 中或与可执行文件在同一目录。

Linux

# Ubuntu/Debian
sudo apt-get install libzstd-dev

# Fedora
sudo dnf install libzstd-devel

# Arch Linux
sudo pacman -S zstd

macOS

brew install zstd

使用示例

简单压缩

import deimos.zstd;
import std.stdio;

void main()
{
    // 要压缩的数据
    string input = "Hello, Zstandard compression!";
    auto src = input.ptr;
    auto srcSize = input.length;
    
    // 分配输出缓冲区(使用 ZSTD_compressBound 计算大小)
    auto dstCapacity = ZSTD_compressBound(srcSize);
    auto dst = new ubyte[dstCapacity];
    
    // 压缩
    auto compressedSize = ZSTD_compress(dst.ptr, dstCapacity, src, srcSize, ZSTD_CLEVEL_DEFAULT);
    
    if (ZSTD_isError(compressedSize))
    {
        writeln("压缩错误: ", ZSTD_getErrorName(compressedSize));
        return;
    }
    
    writeln("压缩: ", srcSize, " 字节 -> ", compressedSize, " 字节");
    
    // 解压缩
    auto decompressed = new ubyte[srcSize];
    auto decompressedSize = ZSTD_decompress(decompressed.ptr, srcSize, dst.ptr, compressedSize);
    
    if (ZSTD_isError(decompressedSize))
    {
        writeln("解压缩错误: ", ZSTD_getErrorName(decompressedSize));
        return;
    }
    
    writeln("解压缩: ", cast(string)decompressed[0..decompressedSize]);
}

流式压缩

import deimos.zstd;

void compressStream(ZSTD_CStream* cstream, const(void)* data, size_t size)
{
    ZSTD_inBuffer input = ZSTD_inBuffer_s(data, size, 0);
    ubyte[64 * 1024] outputBuf;
    
    while (input.pos < input.size)
    {
        ZSTD_outBuffer output = ZSTD_outBuffer_s(outputBuf.ptr, outputBuf.length, 0);
        auto result = ZSTD_compressStream(cstream, &output, &input);
        
        if (ZSTD_isError(result))
        {
            // 处理错误
            break;
        }
        
        // 处理 output.buf[0..output.pos]
    }
}

使用字典

import deimos.zstd;

void compressWithDict(ZSTD_CCtx* cctx, const(void)* dict, size_t dictSize)
{
    // 将字典加载到上下文
    auto result = ZSTD_CCtx_loadDictionary(cctx, dict, dictSize);
    if (ZSTD_isError(result))
    {
        // 处理错误
    }
    
    // 现在使用 ZSTD_compress2() 进行压缩
}

API 覆盖

核心 API

  • ZSTD_compress() / ZSTD_decompress()
  • ZSTD_compressBound() / ZSTD_getFrameContentSize()
  • 错误处理函数

上下文 API

  • ZSTD_CCtx / ZSTD_DCtx 管理
  • 参数设置函数

流式 API

  • ZSTD_CStream / ZSTD_DStream
  • ZSTD_compressStream2() / ZSTD_decompressStream()
  • 缓冲区类型:ZSTD_inBufferZSTD_outBuffer

字典 API

  • ZSTD_CDict / ZSTD_DDict
  • 字典创建和使用函数

字典构建器 (zdict.h)

  • ZDICT_trainFromBuffer()
  • ZDICT_finalizeDictionary()
  • COVER 和 FastCOVER 算法

许可证

本绑定采用与 zstd 库相同的许可证:

  • BSD 3-Clause 许可证
  • GNU General Public License v2.0 或更高版本

您可以选择上述许可证之一。

链接

Authors:
  • sdv
Dependencies:
none
Versions:
0.0.1 2026-May-25
~main 2026-May-25
Show all 2 versions
Download Stats:
  • 0 downloads today

  • 5 downloads this week

  • 5 downloads this month

  • 5 downloads total

Score:
0.1
Short URL:
deimos-zstd.dub.pm