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 模組包含:
The
open()function andZstdFileclass for reading and writing compressed files.The
ZstdCompressorandZstdDecompressorclasses for incremental (de)compression.The
compress()anddecompress()functions for one-shot (de)compression.The
train_dict()andfinalize_dict()functions and theZstdDictclass to train and manage Zstandard dictionaries.The
CompressionParameter,DecompressionParameter, andStrategyclasses for setting advanced (de)compression parameters.
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,bytesor 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
DecompressionParameterfor detailed information about supported parameters. The zstd_dict argument is aZstdDictinstance to be used during decompression. When reading, if the level argument is not None, aTypeErrorwill be raised.When writing, the options argument can be a dictionary providing advanced compression parameters; see
CompressionParameterfor 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 aZstdDictinstance to be used during compression.In binary mode, this function is equivalent to the
ZstdFileconstructor:ZstdFile(file, mode, ...). In this case, the encoding, errors, and newline parameters must not be provided.In text mode, a
ZstdFileobject is created, and wrapped in anio.TextIOWrapperinstance 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
ZstdFilecan 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 astr,bytesor path-like object). If wrapping an existing file object, the wrapped file will not be closed when theZstdFileis 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
DecompressionParameterfor detailed information about supported parameters. The zstd_dict argument is aZstdDictinstance to be used during decompression. When reading, if the level argument is not None, aTypeErrorwill be raised.When writing, the options argument can be a dictionary providing advanced compression parameters; see
CompressionParameterfor 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 aZstdDictinstance to be used during compression.ZstdFilesupports all the members specified byio.BufferedIOBase, except fordetach()andtruncate(). Iteration and thewithstatement 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).
- mode¶
'rb'for reading and'wb'for writing.
- name¶
The name of the Zstandard file. Equivalent to the
nameattribute of the underlying file object.
Compressing and decompressing data in memory¶
- compression.zstd.compress(data, level=None, options=None, zstd_dict=None