compression.zstd --- 與 Zstandard 格式相容的壓縮

在 3.14 版被加入.

原始碼:Lib/compression/zstd/__init__.py


This module provides classes and functions for compressing and decompressing data using the Zstandard (or zstd) compression algorithm. The zstd manual describes Zstandard as "a fast lossless compression algorithm, targeting real-time compression scenarios at zlib-level and better compression ratios." Also included is a file interface that supports reading and writing the contents of .zst files created by the zstd utility, as well as raw zstd compressed streams.

compression.zstd 模組包含:

This is an optional module. If it is missing from your copy of CPython, look for documentation from your distributor (that is, whoever provided Python to you). If you are the distributor, see 可選模組的需求.

例外

exception compression.zstd.ZstdError

This exception is raised when an error occurs during compression or decompression, or while initializing the (de)compressor state.

Reading and writing compressed files

compression.zstd.open(file, /, mode='rb', *, level=None, options=None, zstd_dict=None, encoding=None, errors=None, newline=None)

Open a Zstandard-compressed file in binary or text mode, returning a file object.

The file argument can be either a file name (given as a str, bytes or path-like object), in which case the named file is opened, or it can be an existing file object to read from or write to.

The mode argument can be either 'rb' for reading (default), 'wb' for overwriting, 'ab' for appending, or 'xb' for exclusive creation. These can equivalently be given as 'r', 'w', 'a', and 'x' respectively. You may also open in text mode with 'rt', 'wt', 'at', and 'xt' respectively.

When reading, the options argument can be a dictionary providing advanced decompression parameters; see DecompressionParameter for detailed information about supported parameters. The zstd_dict argument is a ZstdDict instance to be used during decompression. When reading, if the level argument is not None, a TypeError will be raised.

When writing, the options argument can be a dictionary providing advanced compression parameters; see CompressionParameter for detailed information about supported parameters. The level argument is the compression level to use when writing compressed data. Only one of level or options may be non-None. The zstd_dict argument is a ZstdDict instance to be used during compression.

In binary mode, this function is equivalent to the ZstdFile constructor: ZstdFile(file, mode, ...). In this case, the encoding, errors, and newline parameters must not be provided.

In text mode, a ZstdFile object is created, and wrapped in an io.TextIOWrapper instance with the specified encoding, error handling behavior, and line endings.

class compression.zstd.ZstdFile(file, /, mode='rb', *, level=None, options=None, zstd_dict=None)

Open a Zstandard-compressed file in binary mode.

A ZstdFile can wrap an already-open file object, or operate directly on a named file. The file argument specifies either the file object to wrap, or the name of the file to open (as a str, bytes or path-like object). If wrapping an existing file object, the wrapped file will not be closed when the ZstdFile is closed.

The mode argument can be either 'rb' for reading (default), 'wb' for overwriting, 'xb' for exclusive creation, or 'ab' for appending. These can equivalently be given as 'r', 'w', 'x' and 'a' respectively.

If file is a file object (rather than an actual file name), a mode of 'w' does not truncate the file, and is instead equivalent to 'a'.

When reading, the options argument can be a dictionary providing advanced decompression parameters; see DecompressionParameter for detailed information about supported parameters. The zstd_dict argument is a ZstdDict instance to be used during decompression. When reading, if the level argument is not None, a TypeError will be raised.

When writing, the options argument can be a dictionary providing advanced compression parameters; see CompressionParameter for detailed information about supported parameters. The level argument is the compression level to use when writing compressed data. Only one of level or options may be passed. The zstd_dict argument is a ZstdDict instance to be used during compression.

ZstdFile supports all the members specified by io.BufferedIOBase, except for detach() and truncate(). Iteration and the with statement are supported.

The following method and attributes are also provided:

peek(size=-1)

Return buffered data without advancing the file position. At least one byte of data will be returned, unless EOF has been reached. The exact number of bytes returned is unspecified (the size argument is ignored).

備註

While calling peek() does not change the file position of the ZstdFile, it may change the position of the underlying file object (for example, if the ZstdFile was constructed by passing a file object for file).

mode

'rb' for reading and 'wb' for writing.

name

The name of the Zstandard file. Equivalent to the name attribute of the underlying file object.

Compressing and decompressing data in memory

compression.zstd.compress(data, level=None, options=None, zstd_dict=None