One-Shot APIs

compress()

zstandard.compress(data: Buffer, level: int = 3) bytes

Compress source data using the zstd compression format.

This performs one-shot compression using basic/default compression settings.

This method is provided for convenience and is equivalent to calling ZstdCompressor(level=level).compress(data).

If you find yourself calling this function in a tight loop, performance will be greater if you construct a single ZstdCompressor and repeatedly call compress() on it.

decompress()

zstandard.decompress(data: Buffer, max_output_size: int = 0) bytes

Decompress a zstd frame into its original data.

This performs one-shot decompression using basic/default compression settings.

This method is provided for convenience and is equivalent to calling ZstdDecompressor().decompress(data, max_output_size=max_output_size).

If you find yourself calling this function in a tight loop, performance will be greater if you construct a single ZstdDecompressor and repeatedly call decompress() on it.

open()

zstandard.open(filename, mode='rb', cctx=None, dctx=None, encoding=None, errors=None, newline=None, closefd=None)

Create a file object with zstd (de)compression.

The object returned from this function will be a ZstdDecompressionReader if opened for reading in binary mode, a ZstdCompressionWriter if opened for writing in binary mode, or an io.TextIOWrapper if opened for reading or writing in text mode.

Parameters:
  • filenamebytes, str, or os.PathLike defining a file to open or a file object (with a read() or write() method).

  • modestr File open mode. Accepts any of the open modes recognized by open().

  • cctxZstdCompressor to use for compression. If not specified and file is opened for writing, the default ZstdCompressor will be used.

  • dctxZstdDecompressor to use for decompression. If not specified and file is opened for reading, the default ZstdDecompressor will be used.

  • encodingstr that defines text encoding to use when file is opened in text mode.

  • errorsstr defining text encoding error handling mode.

  • newlinestr defining newline to use in text mode.

  • closefd

    bool whether to close the file when the returned object is closed.

    Only used if a file object is passed. If a filename is specified, the opened file is always closed when the returned object is closed.