ZSTD_compress()
Compresses source content as a single zstd compressed frame into an already allocated destination buffer.Parameters
Pointer to the destination buffer where compressed data will be written.
Size of the destination buffer. Providing
dstCapacity >= ZSTD_compressBound(srcSize) guarantees that zstd will have enough space to successfully compress the data.Pointer to the source data to compress.
Size of the source data in bytes.
Compression level (1-22). Higher values provide better compression but are slower. Level 1 is fastest, level 22 is best compression. Levels >= 20 should be used with caution as they require more memory.
Return Value
Returns the compressed size written into
dst (<= dstCapacity), or an error code if it fails (which can be tested using ZSTD_isError()).Example
Related Functions
ZSTD_compressBound()- Calculate maximum compressed sizeZSTD_compressCCtx()- Compress with explicit contextZSTD_isError()- Check if return value is an error
ZSTD_compressCCtx()
Same asZSTD_compress(), but uses an explicit ZSTD_CCtx compression context. This allows reusing the context for multiple compression operations, which is more efficient for system memory.
Parameters
Pointer to a compression context created by
ZSTD_createCCtx().Pointer to the destination buffer where compressed data will be written.
Size of the destination buffer.
Pointer to the source data to compress.
Size of the source data in bytes.
Compression level (1-22). This function compresses at the requested compression level, ignoring any other advanced parameters.
Return Value
Returns the compressed size written into
dst (<= dstCapacity), or an error code if it fails.Important Notes
- This function compresses at the requested compression level, ignoring any other advanced parameters.
- If any advanced parameter was set using the advanced API, they will all be reset. Only
compressionLevelremains. - For parallel execution in multi-threaded environments, use one different context per thread.
Example
Related Functions
ZSTD_createCCtx()- Create compression contextZSTD_freeCCtx()- Free compression contextZSTD_compress()- Simple one-shot compression
ZSTD_compress_usingDict()
Compression at an explicit compression level using a dictionary. A dictionary can be any arbitrary data segment (also called a prefix), or a buffer with specified information.Parameters
Pointer to a compression context.
Pointer to the destination buffer where compressed data will be written.
Size of the destination buffer.
Pointer to the source data to compress.
Size of the source data in bytes.
Pointer to the dictionary data. When
dict == NULL || dictSize < 8, no dictionary is used.Size of the dictionary in bytes.
Compression level (1-22).
Return Value
Returns the compressed size written into
dst, or an error code if it fails.Important Notes
- This function loads the dictionary, resulting in significant startup delay.
- It’s intended for a dictionary used only once.
- When
dict == NULL || dictSize < 8, no dictionary is used. - For repeated use of the same dictionary, use
ZSTD_compress_usingCDict()instead.
Example
Related Functions
ZSTD_compress_usingCDict()- Compress using a pre-digested dictionary (more efficient for repeated use)ZSTD_createCDict()- Create a digested dictionary for reuse