Skip to main content
The Simple Compression API provides one-shot functions for compressing data in a single call. These functions are ideal for compressing data when the entire input is available in memory.

ZSTD_compress()

Compresses source content as a single zstd compressed frame into an already allocated destination buffer.
size_t ZSTD_compress(void* dst, size_t dstCapacity,
                     const void* src, size_t srcSize,
                     int compressionLevel);

Parameters

dst
void*
Pointer to the destination buffer where compressed data will be written.
dstCapacity
size_t
Size of the destination buffer. Providing dstCapacity >= ZSTD_compressBound(srcSize) guarantees that zstd will have enough space to successfully compress the data.
src
const void*
Pointer to the source data to compress.
srcSize
size_t
Size of the source data in bytes.
compressionLevel
int
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

return
size_t
Returns the compressed size written into dst (<= dstCapacity), or an error code if it fails (which can be tested using ZSTD_isError()).

Example

#include <stdio.h>
#include <stdlib.h>
#include <zstd.h>

void compress_data(const char* fname, const char* oname)
{
    size_t fSize;
    void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize);
    size_t const cBuffSize = ZSTD_compressBound(fSize);
    void* const cBuff = malloc(cBuffSize);

    // Compress with level 1
    size_t const cSize = ZSTD_compress(cBuff, cBuffSize, fBuff, fSize, 1);
    if (ZSTD_isError(cSize)) {
        fprintf(stderr, "Compression error: %s\n", ZSTD_getErrorName(cSize));
        exit(1);
    }

    saveFile_orDie(oname, cBuff, cSize);
    printf("%s : %u -> %u\n", fname, (unsigned)fSize, (unsigned)cSize);

    free(fBuff);
    free(cBuff);
}

ZSTD_compressCCtx()

Same as ZSTD_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.
size_t ZSTD_compressCCtx(ZSTD_CCtx* cctx,
                         void* dst, size_t dstCapacity,
                         const void* src, size_t srcSize,
                         int compressionLevel);

Parameters

cctx
ZSTD_CCtx*
Pointer to a compression context created by ZSTD_createCCtx().
dst
void*
Pointer to the destination buffer where compressed data will be written.
dstCapacity
size_t
Size of the destination buffer.
src
const void*
Pointer to the source data to compress.
srcSize
size_t
Size of the source data in bytes.
compressionLevel
int
Compression level (1-22). This function compresses at the requested compression level, ignoring any other advanced parameters.

Return Value

return
size_t
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 compressionLevel remains.
  • For parallel execution in multi-threaded environments, use one different context per thread.

Example

#include <zstd.h>

// Compress multiple files reusing the same context
void compress_multiple_files(int argc, const char** argv)
{
    ZSTD_CCtx* const cctx = ZSTD_createCCtx();
    if (cctx == NULL) {
        fprintf(stderr, "ZSTD_createCCtx() failed!\n");
        exit(1);
    }

    for (int i = 1; i < argc; i++) {
        size_t fSize = loadFile(argv[i], fBuffer, fBufferSize);
        
        // Reuse the same context for each file
        size_t const cSize = ZSTD_compressCCtx(cctx, cBuffer, cBufferSize, 
                                                fBuffer, fSize, 1);
        if (ZSTD_isError(cSize)) {
            fprintf(stderr, "Error: %s\n", ZSTD_getErrorName(cSize));
            continue;
        }
        
        saveFile(outFilename, cBuffer, cSize);
    }

    ZSTD_freeCCtx(cctx);
}

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.
size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
                               void* dst, size_t dstCapacity,
                               const void* src, size_t srcSize,
                               const void* dict, size_t dictSize,
                               int compressionLevel);

Parameters

ctx
ZSTD_CCtx*
Pointer to a compression context.
dst
void*
Pointer to the destination buffer where compressed data will be written.
dstCapacity
size_t
Size of the destination buffer.
src
const void*
Pointer to the source data to compress.
srcSize
size_t
Size of the source data in bytes.
dict
const void*
Pointer to the dictionary data. When dict == NULL || dictSize < 8, no dictionary is used.
dictSize
size_t
Size of the dictionary in bytes.
compressionLevel
int
Compression level (1-22).

Return Value

return
size_t
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

#include <zstd.h>

void compress_with_dict(const char* inputFile, const char* dictFile, 
                        const char* outputFile)
{
    // Load dictionary
    size_t dictSize;
    void* const dictBuffer = mallocAndLoadFile(dictFile, &dictSize);
    
    // Load input
    size_t fSize;
    void* const fBuff = mallocAndLoadFile(inputFile, &fSize);
    
    // Allocate output buffer
    size_t const cBuffSize = ZSTD_compressBound(fSize);
    void* const cBuff = malloc(cBuffSize);
    
    // Create context and compress
    ZSTD_CCtx* const ctx = ZSTD_createCCtx();
    size_t const cSize = ZSTD_compress_usingDict(
        ctx, cBuff, cBuffSize, fBuff, fSize, 
        dictBuffer, dictSize, 3
    );
    
    if (ZSTD_isError(cSize)) {
        fprintf(stderr, "Compression error: %s\n", ZSTD_getErrorName(cSize));
        exit(1);
    }
    
    saveFile(outputFile, cBuff, cSize);
    
    ZSTD_freeCCtx(ctx);
    free(dictBuffer);
    free(fBuff);
    free(cBuff);
}

Build docs developers (and LLMs) love