
Zstandard CLI 完全指南构建变体、编译开关、字典训练与长距离匹配实战【免费下载链接】moldmold: A Modern Linker 项目地址: https://gitcode.com/GitHub_Trending/mo/moldZstandardzstd不仅是一个压缩库还随附了一套功能完整的命令行工具。本指南以当前仓库 third-party/zstd/programs 目录下官方 CLI 文档为主线系统讲解 zstd 命令行程序的构建变体、编译期开关、参数聚合、符号链接快捷方式、字典训练、内置基准测试、环境变量与长距离匹配模式并给出可复制运行的完整命令。读完本文你将掌握如何按需裁剪 zstd 二进制小体积/仅压缩/仅解压、如何为小数据训练专属字典以成倍提升压缩率以及如何在tar --zstd这类无法传参的场景下用环境变量控制压缩行为最后还会结合本仓库mold 链接器实际集成 zstd 的源码了解 CLI 之外的库级压缩用法。一、CLI 的构建与五种变体Zstandard 的命令行界面可以通过make命令直接构建无需任何额外参数。zstd的构建脚本位于 third-party/zstd/programs/Makefile其中all目标默认会构建出四种二进制all: zstd zstd-compress zstd-decompress zstd-small除默认的zstd外Makefile 还提供了若干目标产出功能裁剪过的变体Makefile 目标说明zstd默认 CLI支持 gzip 风格参数包含字典构建器、基准测试模块并支持解压 legacy 格式的 zstd 帧zstd_nolegacy与zstd相同但不含legacy 格式解压支持zstd-small面向最小体积优化的 CLI不含字典构建器、基准测试与 legacy 格式支持zstd-compress只能压缩为 zstd 格式的版本zstd-decompress只能解压 zstd 格式的版本从 Makefile 可以看到各裁剪目标的实际编译方式例如zstd-small使用-Os -Wl,-s优化体积并定义-DZSTD_NOBENCH -DZSTD_NODICT -DZSTD_NOTRACE -UZSTD_LEGACY_SUPPORT -DZSTD_LEGACY_SUPPORT0zstd-decompress在此基础上追加-DZSTD_NOCOMPRESSzstd-compress则追加-DZSTD_NODECOMPRESS。这意味着这些变体本质上就是同一份源码在不同编译宏组合下的产物。二、编译变量按需裁剪 zstd 功能zstd的编译范围可以通过以下make变量精确调整。这些变量直接映射为 Makefile 中的CPPFLAGS宏定义最终影响 zstdcli.c 中对应代码分支的编译。HAVE_THREAD多线程支持当构建环境检测到pthread时多线程支持自动启用。使用make zstd HAVE_THREAD0可显式关闭多线程。使用HAVE_THREAD1可强制启用多线程若此时既找不到pthread也找不到windows.h链接阶段将直接失败——这种宁可报错也不静默降级的设计正是为了确保该特性没有被悄悄关闭。ZSTD_LEGACY_SUPPORT旧版本帧格式支持zstd 自 v0.8.0 起所有版本产生的帧都符合压缩格式规范彼此兼容但 v0.8.0 之前的旧版本帧格式不兼容。该变量用于控制对旧格式的解压支持默认值ZSTD_LEGACY_SUPPORT4支持解码 v0.4.0 及以上的 legacy 格式ZSTD_LEGACY_SUPPORT1表示支持 v0.1.0 及以上的所有格式2、3依此类推ZSTD_LEGACY_SUPPORT0表示不支持任何legacy 格式当ZSTD_LEGACY_SUPPORT 8时效果等同于0legacy 格式只到 v0.7.x。需要特别强调的是zstd只能解码旧格式永远不能生成legacy 格式帧。该宏在 zstdcli.c 中还会被用于在-V版本信息里显示 zstd legacy v0.x 提示。HAVE_ZLIB.gz 格式支持启用后zstd可通过--formatgzip压缩/解压.gz文件将程序符号链接命名为gzip或gunzip也会触发对应行为。构建时检测到zlib库则自动启用make zstd HAVE_ZLIB0可关闭HAVE_ZLIB1可强制启用找不到zlib库时链接失败。HAVE_LZMA.xz 与 .lzma 格式支持通过--formatxz与--formatlzma命令使用符号链接xz、unxz、lzma、unlzma同样可触发。检测到lzma库自动启用HAVE_LZMA0关闭HAVE_LZMA1强制启用找不到库则链接失败。HAVE_LZ4.lz4 格式支持通过--formatlz4使用符号链接lz4、unlz4可触发。检测到lz4库自动启用make zstd HAVE_LZ40关闭HAVE_LZ41强制启用。在 Makefile 中可以看到这些自动检测实际上是在编译时用一段包含lz4frame.h/lz4.h的最小 C 程序做链接探测探测成功即追加-DZSTD_LZ4COMPRESS -DZSTD_LZ4DECOMPRESS与-llz4。ZSTD_NOBENCH / ZSTD_NODICT / ZSTD_NOCOMPRESS / ZSTD_NODECOMPRESS这四组开关用于剔除对应功能模块以缩小二进制ZSTD_NOBENCH编译时不包含内置基准测试模块ZSTD_NODICT编译时不包含字典构建器ZSTD_NOCOMPRESS编译时不包含压缩支持产出的二进制只能解压对应zstd-decompress目标ZSTD_NODECOMPRESS编译时不包含解压支持只能压缩对应zstd-compress目标。BACKTRACE运行时崩溃回溯zstd支持在运行时异常时打印栈回溯stack backtrace。默认情况下该特性在部分平台上会被降级或禁用除非附加额外的编译器指令排查运行时问题时可通过make zstd BACKTRACE1开启以获得更精确的故障定位上下文。Makefile 中Linux/Darwin 下BACKTRACE1会追加-DBACKTRACE_ENABLE1与-rdynamic。三、参数聚合与符号链接快捷方式参数聚合CLI 支持参数聚合aggregation即多个带数值的参数可以拼接书写。例如-b1、-e18、-i1三个参数可以合并写成zstd -b1e18i1这在基准测试场景下尤其方便详见第五节。符号链接快捷方式通过特定名称的符号链接调用zstd可以触发对应行为链接名触发行为zstdmt使用本机全部 CPU 核心进行压缩zcat用任意受支持格式解压并输出到标准输出gzcat、zstdcat等价gzip需 zlib 支持模仿gzip以.gz格式压缩默认删除源文件用--keep保留无 zlib 支持时报错xz需 lzma 支持模仿xz以.xz格式压缩默认删除源文件无 lzma 支持时报错lzma需 lzma 支持模仿lzma以.lzma格式压缩默认删除源文件无 lzma 支持时报错lz4需 lz4 支持模仿lz4以.lz4格式压缩无 lz4 支持时报错unzstd、unlz4解压任意受支持格式ungz、unxz、unlzma同样解压且默认删除源文件用--keep保留这些链接名在 zstdcli.c 中均有对应宏定义ZSTD_ZSTDMT、ZSTD_ZCAT、ZSTD_GZ、ZSTD_GZCAT等程序启动时会根据argv[0]的实际文件名分派行为。四、字典构建器让小数据压缩率飙升原理zstd 提供一种训练模式training mode向算法提供若干代表性样本让它针对这类数据量身定制一张字典。训练结果通过-o选项保存到文件默认名为dictionary压缩与解压前分别加载即可。使用字典后小数据的压缩率会大幅提升且同时获得更快的压缩与解压速度。需要清醒认识的两点字典只有在一族相关的小数据中才有效——不存在普适字典因此最好的实践是每种数据类型部署一张字典字典收益主要集中在文件开头几个 KB之后压缩算法会越来越依赖已解码内容来完成后续压缩。三步用法# 1. 训练字典从训练集中学习 zstd --train PathToTrainingSet/* -o dictionaryName # 2. 用字典压缩 zstd FILE -D dictionaryName # 3. 用字典解压 zstd --decompress FILE.zst -D dictionaryName三种训练算法与参数-h/-H帮助中列出了字典构建器的全部选项Dictionary builder: --train 从一组训练文件中创建字典。 --train-cover[k#,d#,steps#,split#,shrink[#]] 使用 cover 算法可选参数。 --train-fastcover[k#,d#,f#,steps#,split#,accel#,shrink[#]] 使用 fast cover 算法可选参数。 --train-legacy[s#] 使用 legacy 算法selectivity 默认 9。 -o NAME 字典输出名默认 dictionary。 --maxdict# 限制字典大小默认 112640 字节。 --dictID# 强制指定字典 ID默认随机。在 zstdcli.c 中--train-coverk48,d8,steps32、--train-fastcoverk48,d8,f20,steps32,accel2、--train-legacyselectivity8这类带参写法会被逐项解析进对应的训练参数结构体最终传给库级字典训练 API。五、内置基准测试模块CLI 内置了 zstd 的内存内压缩基准测试模块基准使用给定的文件名进行文件被读入内存并拼接在一起消除了 I/O 开销使测试结果更精确可以传多个文件名、使用通配符或用-r选项指定目录若未提供任何文件基准将使用程序化生成的 lorem ipsum 文本作为测试数据基准测量压缩率、压缩后大小、压缩速度与解压速度四项指标-b#选择起始压缩级别-e#选择结束级别在该区间逐级测试-i#设置每个级别的最小测试时间秒默认 3。基准模块同样支持测试特定参数例如线程数-T#、高级参数--zstd#或字典压缩-D DICTIONARY以及其他常规压缩/解压命令可用的参数。六、CLI 完整用法参考完整的选项列表可用-h简短或-H完整获取。以下是文档快照v1.5.6中-H输出的完整内容同时附上关键参数的默认值便于直接对照使用*** Zstandard CLI (64-bit) v1.5.6, by Yann Collet *** Compress or decompress the INPUT file(s); reads from STDIN if INPUT is - or not provided. Usage: zstd [OPTIONS...] [INPUT... | -] [-o OUTPUT] Options: -o OUTPUT Write output to a single file, OUTPUT. -k, --keep Preserve INPUT file(s). [Default] --rm Remove INPUT file(s) after successful (de)compression. -# Desired compression level, where # is a number between 1 and 19; lower numbers provide faster compression, higher numbers yield better compression ratios. [Default: 3] -d, --decompress Perform decompression. -D DICT Use DICT as the dictionary for compression or decompression. -f, --force Disable input and output checks. Allows overwriting existing files, receiving input from the console, printing output to STDOUT, and operating on links, block devices, etc. Unrecognized formats will be passed-through through as-is. -h Display short usage and exit. -H, --help Display full help and exit. -V, --version Display the program version and exit. Advanced options: -c, --stdout Write to STDOUT (even if it is a console) and keep the INPUT file(s). -v, --verbose Enable verbose output; pass multiple times to increase verbosity. -q, --quiet Suppress warnings; pass twice to suppress errors. --trace LOG Log tracing information to LOG. --[no-]progress Forcibly show/hide the progress counter. NOTE: Any (de)compressed output to terminal will mix with progress counter text. -r Operate recursively on directories. --filelist LIST Read a list of files to operate on from LIST. --output-dir-flat DIR Store processed files in DIR. --output-dir-mirror DIR Store processed files in DIR, respecting original directory structure. --[no-]asyncio Use asynchronous IO. [Default: Enabled] --[no-]check Add XXH64 integrity checksums during compression. [Default: Add, Validate] If -d is present, ignore/validate checksums during decompression. -- Treat remaining arguments after -- as files. Advanced compression options: --ultra Enable levels beyond 19, up to 22; requires more memory. --fast[#] Use to very fast compression levels. [Default: 1] --adapt Dynamically adapt compression level to I/O conditions. --long[#] Enable long distance matching with window log #. [Default: 27] --patch-fromREF Use REF as the reference point for Zstandards diff engine. -T# Spawn # compression threads. [Default: 1; pass 0 for core count.] --single-thread Share a single thread for I/O and compression (slightly different than -T1). --auto-threads{physical|logical} Use physical/logical cores when using -T0. [Default: Physical] -B# Set job size to #. [Default: 0 (automatic)] --rsyncable Compress using a rsync-friendly method (-B sets block size). --exclude-compressed Only compress files that are not already compressed. --stream-size# Specify size of streaming input from STDIN. --size-hint# Optimize compression parameters for streaming input of approximately size #. --target-compressed-block-size# Generate compressed blocks of approximately # size. --no-dictID Dont write dictID into the header (dictionary compression only). --[no-]compress-literals Force (un)compressed literals. --[no-]row-match-finder Explicitly enable/disable the fast, row-based matchfinder for the greedy, lazy, and lazy2 strategies. --formatzstd Compress files to the .zst format. [Default] --[no-]mmap-dict Memory-map dictionary file rather than mallocing and loading all at once --formatgzip Compress files to the .gz format. --formatxz Compress files to the .xz format. --formatlzma Compress files to the .lzma format. --formatlz4 Compress files to the .lz4 format. Advanced decompression options: -l Print information about Zstandard-compressed files. --test Test compressed file integrity. -M# Set the memory usage limit to # megabytes. --[no-]sparse Enable sparse mode. [Default: Enabled for files, disabled for STDOUT.] --[no-]pass-through Pass through uncompressed files as-is. [Default: Disabled] Dictionary builder: --train Create a dictionary from a training set of files. --train-cover[k#,d#,steps#,split#,shrink[#]] Use the cover algorithm (with optional arguments). --train-fastcover[k#,d#,f#,steps#,split#,accel#,shrink[#]] Use the fast cover algorithm (with optional arguments). --train-legacy[s#] Use the legacy algorithm with selectivity #. [Default: 9] -o NAME Use NAME as dictionary name. [Default: dictionary] --maxdict# Limit dictionary to specified size #. [Default: 112640] --dictID# Force dictionary ID to #. [Default: Random] Benchmark options: -b# Perform benchmarking with compression level #. [Default: 3] -e# Test all compression levels up to #; starting level is -b#. [Default: 1] -i# Set the minimum evaluation to time # seconds. [Default: 3] -B# Cut file into independent chunks of size #. [Default: No chunking] -S Output one benchmark result per input file. [Default: Consolidated result] -D dictionary Benchmark using dictionary --priorityrt Set process priority to real-time.几个高频参数的实战要点压缩级别-#的取值范围是 1–19配合--ultra可到 22默认 3级别越高压缩率越好、速度越慢默认保留源文件与 gzip 不同zstd 默认--keep只有显式--rm或通过gzip/xz等符号链接调用时才删除源文件完整性校验压缩时默认添加 XXH64 校验和解压时默认校验线程控制-T0表示使用全部核心--auto-threads{physical|logical}决定-T0按物理核还是逻辑核计数默认物理核。七、通过环境变量传参ZSTD_CLEVEL 与 ZSTD_NBTHREADSzstd没有提供任意参数透传给环境变量的通用机制——这种设计有安全考量因此该通道被刻意限制只支持ZSTD_CLEVEL与ZSTD_NBTHREADS两个变量宏定义见 zstdcli.c。ZSTD_CLEVEL修改默认压缩级别通常为 3可设为 1–19 的常规区间值ZSTD_NBTHREADS指定压缩使用的线程数默认 1仅当 zstd 以多线程支持编译时生效0表示使用本机检测到的 CPU 核心数线程数上限由ZSTDMT_NBWORKERS_MAX封顶——按文档描述32 位模式下为 6464 位环境下为 256。该机制最有价值的场景是无法传参的调用方式典型例子是tar --zstdexport ZSTD_CLEVEL9 export ZSTD_NBTHREADS0 tar --zstd -cf archive.tar.zst mydir/由于这两个变量只替换默认值它们可以被对应的命令行参数覆盖-#覆盖压缩级别-T#覆盖线程数。在命令可传参时显式参数优先级更高。八、长距离匹配模式--long--long模式专为远距离存在长匹配的大文件设计可把匹配距离扩展到最大窗口尺寸128 MiB同时保持压缩速度。启用后窗口大小被设为 128 MiB压缩端和解压端的内存占用都会增加速度表现取决于能否找到长匹配若文件中长匹配很少压缩速度可能下降而长距离匹配较多时解压速度通常会提升。文档给出了一个理想用例的实测数据将 clang 的 3.4.1、3.4.2、3.5.0、3.5.1 四个版本打成一个 244,889,600 字节的 tar 包每个版本小于 128 MiB窗口内存在大量长距离匹配方法压缩率压缩速度解压速度zstd -15.065284.8 MB/s759.3 MB/szstd -55.826124.9 MB/s674.0 MB/szstd -106.50429.5 MB/s771.3 MB/szstd -1 --long17.426220.6 MB/s1638.4 MB/szstd -5 --long19.661165.5 MB/s1530.6 MB/szstd -10 --long21.94975.6 MB/s1632.6 MB/s在该文件上压缩率显著提升且对压缩速度影响很小解压速度近乎翻倍。但另一个极端是长匹配很少的数据如 Silesia 压缩测试集--long反而可能带来低级别下压缩速度的下降压缩率变化却很小方法压缩率压缩速度解压速度zstd -12.878231.7 MB/s594.4 MB/szstd -1 --long2.929106.5 MB/s517.9 MB/szstd -53.27477.1 MB/s464.2 MB/szstd -5 --long3.31951.7 MB/s371.9 MB/szstd -103.52316.4 MB/s489.2 MB/szstd -10 --long3.56616.2 MB/s415.7 MB/s因此--long的适用结论很明确长距离匹配多如多版本源码打包时收益巨大匹配少时慎用。九、zstdgrep直接检索压缩文件zstdgrep允许像grep一样直接检索.zst压缩文件用法与普通grep一致zstdgrep pattern file.zst需要注意zstdgrep不兼容字典压缩。要检索用字典压缩的文件需要先用zstd或zstdcat解压再管道给grepzstdcat -D dictionary -qc -- file.zst | grep pattern十、仓库联动mold 链接器如何集成 zstd本文所依托的仓库是 mold一个现代链接器它把 zstd 作为第三方组件内嵌使用这正好是zstd 能力在实际项目中的落地点构建期集成CMakeLists.txt 中先检测系统是否存在zstd.h存在则链接系统zstd库否则通过add_subdirectory(third-party/zstd/build/cmake)编译仓库内自带的 third-party/zstd 源码并链接静态库libzstd_static——这与 zstd 官方自动探测、找不到则降级的构建哲学一致库级压缩lib/compress.cc 中的ZstdCompressor调用ZSTD_compress并把输入切分成多个 shard 后用 TBB 并行压缩再拼接最终生成的正是标准 zstd 帧可用zstdcat解压验证CLI 参数对接src/cmdline.cc 中 mold 的--compress-debug-sectionszstd[:level]参数zstd 级别范围 1–22默认 3与 zstd CLI 中--ultra才能触达的高级别区间对应测试验证test/compress-debug-sections-zstd.sh 中mold 生成压缩调试信息后用zstdcat解码校验印证了CLI 工具与库产物格式互通这一事实。也就是说无论你是想单独构建一个裁剪版 zstd 命令行工具还是想在自有项目里内嵌 zstd 库并复用这套构建与参数体系本文介绍的编译开关、字典训练与基准测试方法论都直接适用。小结zstd 的 CLI 远不止压缩/解压这么简单通过 Makefile 目标与编译变量你可以精确裁剪出最小体积、仅压缩或仅解压的二进制通过符号链接快捷方式可以无缝模拟 gzip/xz/lz4 行为字典训练器让 KB 级小数据的压缩率实现数量级提升--long模式针对多版本打包场景收益显著而ZSTD_CLEVEL/ZSTD_NBTHREADS则在tar --zstd等无参数场景下提供了唯一的调优入口。结合本仓库 mold 对 zstd 的集成方式CMakeLists.txt、lib/compress.cc、src/cmdline.cc你可以把同样的构建与使用经验迁移到任何需要内嵌 zstd 的项目中。【免费下载链接】moldmold: A Modern Linker 项目地址: https://gitcode.com/GitHub_Trending/mo/mold创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考