CMake Zstd compression

Zstd is an open file compression standard. Zstd has become widely used and is incorporated in the Linux kernel and GCC. We use Zstd for data archiving particularly for large files where size and speed are a concern. CMake supports Zstd compression throughout, including file(ARCHIVE_CREATE) and file(ARCHIVE_EXTRACT). Zstd is vendored into CMake, so there is no need to worry about system libraries for Zstd.

CMake can be a useful command line tool for Zstd compression and extraction, especially on platforms where Zstd is not available as a system library. For example, CMake can be used to extract .zst file archives like:

cmake -E tar x arc.zst

CMakeLists.txt Zstd example

  • option file(ARCHIVE_CREATE ... WORKING_DIRECTORY ...) is necessary to avoid system-specific relative path issues.
  • option file(ARCHIVE_CREATE ... PATTERNS_EXCLUDE ...) saves time and disk space by skipping archiving of files that are not needed, such as documentation files.
cmake_minimum_required(VERSION 3.31)

set(archive "example.zst")
set(in_dir "data/")

if(CMAKE_VERSION VERSION_GREATER_EQUAL 4.5)
  set(_pat_excl_docs PATTERNS_EXCLUDE "docs/*")
endif()

file(ARCHIVE_CREATE
  OUTPUT ${archive}
  PATHS ${in_dir}
  COMPRESSION Zstd
  COMPRESSION_LEVEL 3
  WORKING_DIRECTORY ${in_dir}
  ${_pat_excl_docs}
  )
COMPRESSION_LEVEL
arbitrary, bigger value is more compressed.
FORMAT
not used for Zstd.