zstd, short for Zstandard, is a fast lossless compression algorithm, targeting real-time compression scenarios at zlib-level and better compression ratios. The zstd compression library provides in-memory compression and decompression functions.
The library supports regular compression levels from 1 up to maxCLevel, which is currently 22. Levels ≥ 20, labeled --ultra, should be
used with caution, as they require more memory. The library also offers negative compression levels, which extend the range of speed vs. ratio
preferences. The lower the level, the faster the speed (at the cost of compression).
Compression can be done in:
The compression ratio achievable on small data can be highly improved using a dictionary. Dictionary compression can be performed in:
Advanced experimental functions can be accessed using #define ZSTD_STATIC_LINKING_ONLY before including zstd.h. Advanced experimental
APIs should never be used with a dynamically-linked library. They are not "stable", their definitions or signatures may change in the future. Only
static linking is allowed.
A ZSTD_CStream object is required to track streaming operation.
Use createCStream and freeCStream to create/release resources. ZSTD_CStream objects can be reused multiple times on consecutive
compression operations. It is recommended to re-use ZSTD_CStream since it will play nicer with system's memory, by re-using already allocated
memory.
For parallel execution, use one separate ZSTD_CStream.
Since v1.3.0, ZSTD_CStream and ZSTD_CCtx are the same thing.
Parameters are sticky: when starting a new compression on the same context, it will re-use the same sticky parameters as previous compression session.
When in doubt, it's recommended to fully initialize the context before usage. Use CCtx_reset to reset the context and CCtx_setParameter,
CCtx_setPledgedSrcSize, or CCtx_loadDictionary and friends to set more specific parameters, the pledged source size, or load a dictionary.
Use compressStream2 with e_continue as many times as necessary to consume input stream. The function will automatically update both pos
fields within input and output. Note that the function may not consume the entire input, for example, because the output buffer is
already full, in which case input.pos < input.size. The caller must check if input has been entirely consumed. If not, the caller must make
some room to receive more compressed data, and then present again remaining input data.
Note
ZSTD_e_continue is guaranteed to make some forward progress when called, but doesn't guarantee maximal forward progress. This is
especially relevant when compressing with multiple threads. The call won't block if it can consume some input, but if it can't it will wait for some,
but not all, output to be flushed.
At any moment, it's possible to flush whatever data might remain stuck within internal buffer, using ZSTD_compressStream2() with e_flush.
output->pos will be updated. Note that, if output->size is too small, a single invocation with ZSTD_e_flush might not be enough
(return code > 0). In which case, make some room to receive more compressed data, and call again ZSTD_compressStream2() with
ZSTD_e_flush. You must continue calling ZSTD_compressStream2() with ZSTD_e_flush until it returns 0, at which point you can
change the operation.
Note
ZSTD_e_flush will flush as much output as possible, meaning when compressing with multiple threads, it will block until the flush is
complete or the output buffer is full.
Calling ZSTD_compressStream2() with e_end instructs to finish a frame. It will perform a flush and write frame epilogue. The epilogue is
required for decoders to consider a frame completed. Flush operation is the same, and follows same rules as calling ZSTD_compressStream2() with
ZSTD_e_flush. You must continue calling ZSTD_compressStream2() with ZSTD_e_end until it returns 0, at which point you are free
to start a new frame.
Note
ZSTD_e_end will flush as much output as possible, meaning when compressing with multiple threads, it will block until the flush is
complete or the output buffer is full.
A ZSTD_DStream object is required to track streaming operations.
Use createDStream and freeDStream to create/release resources. ZSTD_DStream objects can be re-used multiple times.
Use DCtx_reset and DCtx_refDDict to start a new decompression operation. Alternatively, use advanced API to set specific properties.
Use decompressStream repetitively to consume your input. The function will update both pos fields. If input.pos < input.size, some
input has not been consumed. It's up to the caller to present again remaining data. The function tries to flush all data decoded immediately,
respecting output buffer size. If output.pos < output.size, decoder has flushed everything it could. But if output.pos == output.size,
there might be some data left within internal buffers. In which case, call ZSTD_decompressStream() again to flush whatever remains in the
buffer.
Note
with no additional input provided, amount of data flushed is necessarily ≤ BLOCKSIZE_MAX.
| Modifier and Type | Field and Description |
|---|---|
| public static final int | |
| public static final int | |
| public static final int | ZSTD_btlazy2
Compression strategies, listed from fastest to strongest. |
| public static final int | ZSTD_btopt
Compression strategies, listed from fastest to strongest. |
| public static final int | ZSTD_btultra
Compression strategies, listed from fastest to strongest. |
| public static final int | ZSTD_btultra2
Compression strategies, listed from fastest to strongest. |
| public static final int | ZSTD_c_chainLog
Compression parameters. |
| public static final int | ZSTD_c_checksumFlag
Compression parameters. |
| public static final int | ZSTD_c_compressionLevel
Compression parameters. |
| public static final int | ZSTD_c_contentSizeFlag
Compression parameters. |
| public static final int | ZSTD_c_dictIDFlag
Compression parameters. |
| public static final int | ZSTD_c_enableLongDistanceMatching
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam1
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam10
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam11
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam12
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam13
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam14
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam15
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam16
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam17
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam18
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam19
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam2
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam3
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam4
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam5
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam6
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam7
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam8
Compression parameters. |
| public static final int | ZSTD_c_experimentalParam9
Compression parameters. |
| public static final int | ZSTD_c_hashLog
Compression parameters. |
| public static final int | ZSTD_c_jobSize
Compression parameters. |
| public static final int | ZSTD_c_ldmBucketSizeLog
Compression parameters. |
| public static final int | ZSTD_c_ldmHashLog
Compression parameters. |
| public static final int | ZSTD_c_ldmHashRateLog
Compression parameters. |
| public static final int | ZSTD_c_ldmMinMatch
Compression parameters. |
| public static final int | ZSTD_c_minMatch
Compression parameters. |
| public static final int | ZSTD_c_nbWorkers
Compression parameters. |
| public static final int | ZSTD_c_overlapLog
Compression parameters. |
| public static final int | ZSTD_c_searchLog
Compression parameters. |
| public static final int | ZSTD_c_strategy
Compression parameters. |
| public static final int | ZSTD_c_targetLength
Compression parameters. |
| public static final int | ZSTD_c_windowLog
Compression parameters. |
| public static final int | ZSTD_CLEVEL_DEFAULT
Default compression level. |
| public static final long | ZSTD_CONTENTSIZE_ERROR
Content size. |
| public static final long | ZSTD_CONTENTSIZE_UNKNOWN
Content size. |
| public static final int | ZSTD_d_experimentalParam1
The advanced API pushes parameters one by one into an existing |
| public static final int | ZSTD_d_experimentalParam2
The advanced API pushes parameters one by one into an existing |
| public static final int | ZSTD_d_experimentalParam3
The advanced API pushes parameters one by one into an existing |
| public static final int | ZSTD_d_experimentalParam4
The advanced API pushes parameters one by one into an existing |
| public static final int | ZSTD_d_experimentalParam5
The advanced API pushes parameters one by one into an existing |
| public static final int | ZSTD_d_windowLogMax
The advanced API pushes parameters one by one into an existing |
| public static final int | ZSTD_dfast
Compression strategies, listed from fastest to strongest. |
| public static final int | ZSTD_e_continue
|
| public static final int | ZSTD_e_end
|
| public static final int | ZSTD_e_flush
|
| public static final int | ZSTD_fast
Compression strategies, listed from fastest to strongest. |
| public static final int | ZSTD_greedy
Compression strategies, listed from fastest to strongest. |
| public static final int | ZSTD_lazy
Compression strategies, listed from fastest to strongest. |
| public static final int | ZSTD_lazy2
Compression strategies, listed from fastest to strongest. |
| public static final int | |
| public static final int | |
| public static final int | |
| public static final int | |
| public static final int | ZSTD_reset_parameters
|
| public static final int | ZSTD_reset_session_and_parameters
|
| public static final int | ZSTD_reset_session_only
|
| public static final int | ZSTD_VERSION_MAJOR
Version number part. |
| public static final int | ZSTD_VERSION_MINOR
Version number part. |
| public static final int | ZSTD_VERSION_NUMBER
Version number. |
| public static final int | ZSTD_VERSION_RELEASE
Version number part. |
| public static final String | ZSTD_VERSION_STRING
Version string. |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description | |||
|---|---|---|---|---|
| public static native long | nZSTD_CCtx_loadDictionary(long cctx, long dict, long dictSize)
Unsafe version of: | |||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static native long | nZSTD_CCtx_setPledgedSrcSize(long cctx, long pledgedSrcSize)
Unsafe version of: | |||
| public static native long | nZSTD_compress(long dst, long dstCapacity, long src, long srcSize, int compressionLevel)
Unsafe version of: | |||
| public static native long | nZSTD_compress2(long cctx, long dst, long dstCapacity, long src, long srcSize)
Unsafe version of: | |||
| public static native long | nZSTD_compress_usingCDict(long cctx, long dst, long dstCapacity, long src, long srcSize, long cdict)
Unsafe version of: | |||
| public static native long | nZSTD_compress_usingDict(long ctx, long dst, long dstCapacity, long src, long srcSize, long dict, long dictSize, int compressionLevel)
Unsafe version of: | |||
| public static native long | nZSTD_compressCCtx(long ctx, long dst, long dstCapacity, long src, long srcSize, int compressionLevel)
Unsafe version of: | |||
| public static native long | nZSTD_compressStream2(long cctx, long output, long input, int endOp)
Unsafe version of: | |||
| public static native void | ||||
| public static native long | nZSTD_createCDict(long dictBuffer, long dictSize, int compressionLevel)
Unsafe version of: | |||
| public static native long | ||||
| public static native long | nZSTD_DCtx_loadDictionary(long dctx, long dict, long dictSize)
Unsafe version of: | |||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static native long | nZSTD_decompress(long dst, long
is an upper bound of dstCapacity, long src, long originalSize to regenerate. If user cannot imply a maximum upper bound, it's better to use streaming mode to
decompress data.must be the exact size of some number of compressed and/or skippable frames compressedSize)Unsafe version of: | |||
| public static native long | nZSTD_decompress_usingDDict(long dctx, long dst, long dstCapacity, long src, long srcSize, long ddict)
Unsafe version of: | |||
| public static native long | nZSTD_decompress_usingDict(long dctx, long dst, long dstCapacity, long src, long srcSize, long dict, long dictSize)
Unsafe version of: | |||
| public static native long | nZSTD_decompressDCtx(long ctx, long dst, long dstCapacity, long src, long srcSize)
Unsafe version of: | |||
| public static native long | ||||
| public static native void | ||||
| public static native long | nZSTD_findFrameCompressedSize(long src, long
must be ≥ first frame size srcSize)Unsafe version of: | |||
| public static native int | ||||
| public static native int | ||||
| public static native int | ||||
| public static native int | ||||
| public static native long | ||||
| public static native long | nZSTD_getFrameContentSize(long src, long
must be at least as large as the frame header. Hint: any size ≥ srcSize)FRAMEHEADERSIZE_MAX is large enough.Unsafe version of: | |||
| public static native int | ||||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static long | Returns: 0, or an error code (which can be tested withisError).Creates an internal | |||
| public static long | Returns: 0, or an error code (which can be tested withisError).References a prepared dictionary, to be used for all future compressed frames. | |||
| public static long | Returns: 0, or an error code (which can be tested withisError).References a prefix (single-usage dictionary) for next compressed frame. | |||
| public static long | ZSTD_CCtx_reset(long cctx, int reset)
There are 2 different things that can be reset, independently or jointly :
| |||
| public static long | Returns: an error code (which can be tested usingisError)one of: param, int value)Set one compression parameter, selected by enum | |||
| public static long | Returns: 0, or an error code (which can be tested withisError).Total input data size to be compressed as a single frame. | |||
| public static long | Returns: compressed size written intodst (≤ dstCapacity), or an error code if it fails (which can be tested using isError).Compresses | |||
| public static long | Returns: compressed size written intodst (≤ dstCapacity), or an error code if it fails (which can be tested using isError)Behaves the same as | |||
| public static long | ZSTD_compress_usingCDict(long cctx, ByteBuffer dst, ByteBuffer src, long cdict)
Compression using a digested Dictionary. | |||
| public static long | ZSTD_compress_usingDict(long ctx, ByteBuffer dst, ByteBuffer src, ByteBuffer dict, int compressionLevel)
Compression at an explicit compression level using a Dictionary. | |||
| public static native long | ZSTD_compressBound(long srcSize)
Returns the maximum compressed size in worst case single-pass scenario. | |||
| public static long | ||||
| public static long | ZSTD_compressCCtx(long ctx, ByteBuffer dst, ByteBuffer src, int compressionLevel)
Same as | |||
| public static long | Returns: provides a minimum amount of data remaining to be flushed from internal buffers or an error code, which can be tested usingisError.
If - after a one of: endOp)
Behaves about the same as | |||
| public static ZSTDBounds | ZSTD_cParam_getBounds(int
one of: cParam, ZSTDBounds a structure, __result)ZSTD_bounds, which contains
All parameters must belong to an interval with lower and upper bounds, otherwise they will either trigger an error or be automatically clamped. | |||
| public static native long | ||||
| public static long | ZSTD_createCDict(ByteBuffer dictBuffer, int compressionLevel)
When compressing multiple messages or blocks using the same dictionary, it's recommended to digest the dictionary only once, since it's a costly operation. | |||
| public static native long | ||||
| public static native long | ||||
| public static long | ZSTD_createDDict(ByteBuffer dictBuffer)
Creates a digested dictionary, ready to start decompression operation without startup delay. | |||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static long | Returns: 0, or an error code (which can be tested with ZSTD_isError())Create an internal | |||
| public static long | Returns: 0, or an error code (which can be tested withisError)References a prepared dictionary, to be used to decompress next frames. | |||
| public static long | Returns: 0, or an error code (which can be tested withisError)References a prefix (single-usage dictionary) to decompress next frame. | |||
| public static long | Returns: 0, or an error code, which can be tested withisErrorReturns a | |||
| public static long | Returns: 0, or an error code (which can be tested usingisError)Set one compression parameter, selected by | |||
| public static long | Returns: the number of bytes decompressed intodst (≤ dstCapacity), or an errorCode if it fails (which can be tested using
isError).
| |||
| public static long | ZSTD_decompress_usingDDict(long dctx, ByteBuffer dst, ByteBuffer src, long ddict)
Decompression using a digested Dictionary. | |||
| public static long | ZSTD_decompress_usingDict(long dctx, ByteBuffer dst, ByteBuffer src, ByteBuffer dict)
Decompression using a known Dictionary. | |||
| public static long | ZSTD_decompressDCtx(long ctx, ByteBuffer dst, ByteBuffer src)
Same as | |||
| public static long | Returns: 0 when a frame is completely decoded and fully flushed, an error code, which can be tested usingisError, any other value > 0, which means there
is still some decoding to do to complete current frame. The return value is a suggested next input size (just a hint to improve latency) that will
never request more than the remaining frame size.Use | |||
| public static native int | ||||
| public static ZSTDBounds | ZSTD_dParam_getBounds(int dParam, ZSTDBounds
returns a structure, __result)ZSTD_bounds, which contains - an error status field, which must be tested using isError - both lower and upper bounds,
inclusiveAll parameters must belong to an interval with lower and upper bounds, otherwise they will either trigger an error or be automatically clamped. | |||
| public static native long | ||||
| public static native long | ||||
| public static long | Returns: the compressed size of the first frame starting atsrc, suitable to pass as srcSize to decompress or similar, or an error code if
input is invalidshould point to the start of a ZSTD frame or skippable frame src)
| |||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static native long | ||||
| public static int | ||||
| public static int | Returns: if== 0, the dictionary is not conformant to Zstandard specification, or empty.
Non-conformant dictionaries can still be loaded, but as content-only dictionaries. Provides the | |||
| public static int | Returns: if== 0, the dictionary is not conformant with Zstandard specification. It can still be loaded, but as a content-only dictionary.Provides the | |||
| public static int | Returns: if== 0, the dictID could not be decoded. This could for one of the following reasons :
Provides the dictID required to decompressed the frame stored within | |||
| public static String | ||||
| public static long | Returns: decompressed size ofsrc frame content, if known
should point to the start of a ZSTD encoded frame src)Notes:
| |||
| public static boolean | ||||
| public static native int | ||||
| public static native int | ||||
| public static long | ||||
| public static long | ||||
| public static long | ||||
| public static long | ||||
| public static long | ||||
| public static long | ||||
| public static native int | ZSTD_versionNumber()
Returns runtime library version, the value is | |||
| public static String |