gh-135252: Document Zstandard integration across zipfile, shutil, and… · python/cpython@938a5d7 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit 938a5d7

Browse files
authored
gh-135252: Document Zstandard integration across zipfile, shutil, and tarfile (#135311)
Document Zstandard integration across zipfile, shutil, and tarfile
1 parent da79ac9 commit 938a5d7

File tree

4 files changed

+77
-19
lines changed

4 files changed

+77
-19
lines changed

Doc/library/compression.zstd.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,14 @@ Advanced parameter control
523523
.. attribute:: compression_level
524524

525525
A high-level means of setting other compression parameters that affect
526-
the speed and ratio of compressing data. Setting the level to zero uses
527-
:attr:`COMPRESSION_LEVEL_DEFAULT`.
526+
the speed and ratio of compressing data.
527+
528+
Regular compression levels are greater than ``0``. Values greater than
529+
``20`` are considered "ultra" compression and require more memory than
530+
other levels. Negative values can be used to trade off faster compression
531+
for worse compression ratios.
532+
533+
Setting the level to zero uses :attr:`COMPRESSION_LEVEL_DEFAULT`.
528534

529535
.. attribute:: window_log
530536

Doc/library/shutil.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
618618
*format* is the archive format: one of
619619
"zip" (if the :mod:`zlib` module is available), "tar", "gztar" (if the
620620
:mod:`zlib` module is available), "bztar" (if the :mod:`bz2` module is
621-
available), or "xztar" (if the :mod:`lzma` module is available).
621+
available), "xztar" (if the :mod:`lzma` module is available), or "zstdtar"
622+
(if the :mod:`compression.zstd` module is available).
622623

623624
*root_dir* is a directory that will be the root directory of the
624625
archive, all paths in the archive will be relative to it; for example,
@@ -673,6 +674,8 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
673674
- *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
674675
- *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
675676
- *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
677+
- *zstdtar*: Zstandard compressed tar-file (if the :mod:`compression.zstd`
678+
module is available).
676679

677680
You can register new formats or provide your own archiver for any existing
678681
formats, by using :func:`register_archive_format`.
@@ -716,8 +719,8 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
716719
*extract_dir* is the name of the target directory where the archive is
717720
unpacked. If not provided, the current working directory is used.
718721

719-
*format* is the archive format: one of "zip", "tar", "gztar", "bztar", or
720-
"xztar". Or any other format registered with
722+
*format* is the archive format: one of "zip", "tar", "gztar", "bztar",
723+
"xztar", or "zstdtar". Or any other format registered with
721724
:func:`register_unpack_format`. If not provided, :func:`unpack_archive`
722725
will use the archive file name extension and see if an unpacker was
723726
registered for that extension. In case none is found,
@@ -789,6 +792,8 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
789792
- *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
790793
- *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
791794
- *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
795+
- *zstdtar*: Zstandard compressed tar-file (if the :mod:`compression.zstd`
796+
module is available).
792797

793798
You can register new formats or provide your own unpacker for any existing
794799
formats, by using :func:`register_unpack_format`.

Doc/library/tarfile.rst

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ higher-level functions in :ref:`shutil <archiving-operations>`.
1818

1919
Some facts and figures:
2020

21-
* reads and writes :mod:`gzip`, :mod:`bz2` and :mod:`lzma` compressed archives
22-
if the respective modules are available.
21+
* reads and writes :mod:`gzip`, :mod:`bz2`, :mod:`compression.zstd`, and
22+
:mod:`lzma` compressed archives if the respective modules are available.
2323

2424
* read/write support for the POSIX.1-1988 (ustar) format.
2525

@@ -47,6 +47,10 @@ Some facts and figures:
4747
or paths outside of the destination. Previously, the filter strategy
4848
was equivalent to :func:`fully_trusted <fully_trusted_filter>`.
4949

50+
.. versionchanged:: 3.14
51+
52+
Added support for Zstandard compression using :mod:`compression.zstd`.
53+
5054
.. function:: open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)
5155

5256
Return a :class:`TarFile` object for the pathname *name*. For detailed
@@ -71,6 +75,8 @@ Some facts and figures:
7175
+------------------+---------------------------------------------+
7276
| ``'r:xz'`` | Open for reading with lzma compression. |
7377
+------------------+---------------------------------------------+
78+
| ``'r:zst'`` | Open for reading with Zstandard compression.|
79+
+------------------+---------------------------------------------+
7480
| ``'x'`` or | Create a tarfile exclusively without |
7581
| ``'x:'`` | compression. |
7682
| | Raise a :exc:`FileExistsError` exception |
@@ -88,6 +94,10 @@ Some facts and figures:
8894
| | Raise a :exc:`FileExistsError` exception |
8995
| | if it already exists. |
9096
+------------------+---------------------------------------------+
97+
| ``'x:zst'`` | Create a tarfile with Zstandard compression.|
98+
| | Raise a :exc:`FileExistsError` exception |
99+
| | if it already exists. |
100+
+------------------+---------------------------------------------+
91101
| ``'a' or 'a:'`` | Open for appending with no compression. The |
92102
| | file is created if it does not exist. |
93103
+------------------+---------------------------------------------+
@@ -99,6 +109,8 @@ Some facts and figures:
99109
+------------------+---------------------------------------------+
100110
| ``'w:xz'`` | Open for lzma compressed writing. |
101111
+------------------+---------------------------------------------+
112+
| ``'w:zst'`` | Open for Zstandard compressed writing. |
113+
+------------------+---------------------------------------------+
102114

103115
Note that ``'a:gz'``, ``'a:bz2'`` or ``'a:xz'`` is not possible. If *mode*
104116
is not suitable to open a certain (compressed) file for reading,
@@ -115,6 +127,15 @@ Some facts and figures:
115127
For modes ``'w:xz'``, ``'x:xz'`` and ``'w|xz'``, :func:`tarfile.open` accepts the
116128
keyword argument *preset* to specify the compression level of the file.
117129

130+
For modes ``'w:zst'``, ``'x:zst'`` and ``'w|zst'``, :func:`tarfile.open`
131+
accepts the keyword argument *level* to specify the compression level of
132+
the file. The keyword argument *options* may also be passed, providing
133+
advanced Zstandard compression parameters described by
134+
:class:`~compression.zstd.CompressionParameter`. The keyword argument
135+
*zstd_dict* can be passed to provide a :class:`~compression.zstd.ZstdDict`,
136+
a Zstandard dictionary used to improve compression of smaller amounts of
137+
data.
138+
118139
For special purposes, there is a second format for *mode*:
119140
``'filemode|[compression]'``. :func:`tarfile.open` will return a :class:`TarFile`
120141
object that processes its data as a stream of blocks. No random seeking will
@@ -146,6 +167,9 @@ Some facts and figures:
146167
| ``'r|xz'`` | Open an lzma compressed *stream* for |
147168
| | reading. |
148169
+-------------+--------------------------------------------+
170+
| ``'r|zst'`` | Open a Zstandard compressed *stream* for |
171+
| | reading. |
172+
+-------------+--------------------------------------------+
149173
| ``'w|'`` | Open an uncompressed *stream* for writing. |
150174
+-------------+--------------------------------------------+
151175
| ``'w|gz'`` | Open a gzip compressed *stream* for |
@@ -157,6 +181,9 @@ Some facts and figures:
157181
| ``'w|xz'`` | Open an lzma compressed *stream* for |
158182
| | writing. |
159183
+-------------+--------------------------------------------+
184+
| ``'w|zst'`` | Open a Zstandard compressed *stream* for |
185+
| | writing. |
186+
+-------------+--------------------------------------------+
160187

161188
.. versionchanged:: 3.5
162189
The ``'x'`` (exclusive creation) mode was added.

Doc/library/zipfile.rst

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,28 @@ The module defines the following items:
129129

130130
.. versionadded:: 3.3
131131

132+
.. data:: ZIP_ZSTANDARD
133+
134+
The numeric constant for Zstandard compression. This requires the
135+
:mod:`compression.zstd` module.
136+
132137
.. note::
133138

134-
The ZIP file format specification has included support for bzip2 compression
135-
since 2001, and for LZMA compression since 2006. However, some tools
136-
(including older Python releases) do not support these compression
137-
methods, and may either refuse to process the ZIP file altogether,
138-
or fail to extract individual files.
139+
In APPNOTE 6.3.7, the method ID ``20`` was assigned to Zstandard
140+
compression. This was changed in APPNOTE 6.3.8 to method ID ``93`` to
141+
avoid conflicts, with method ID ``20`` being deprecated. For
142+
compatibility, the :mod:`!zipfile` module reads both method IDs but will
143+
only write data with method ID ``93``.
144+
145+
.. versionadded:: 3.14
146+
147+
.. note::
139148

149+
The ZIP file format specification has included support for bzip2 compression
150+
since 2001, for LZMA compression since 2006, and Zstandard compression since
151+
2020. However, some tools (including older Python releases) do not support
152+
these compression methods, and may either refuse to process the ZIP file
153+
altogether, or fail to extract individual files.
140154

141155
.. seealso::
142156

@@ -176,10 +190,11 @@ ZipFile Objects
176190

177191
*compression* is the ZIP compression method to use when writing the archive,
178192
and should be :const:`ZIP_STORED`, :const:`ZIP_DEFLATED`,
179-
:const:`ZIP_BZIP2` or :const:`ZIP_LZMA`; unrecognized
180-
values will cause :exc:`NotImplementedError` to be raised. If
181-
:const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or :const:`ZIP_LZMA` is specified
182-
but the corresponding module (:mod:`zlib`, :mod:`bz2` or :mod:`lzma`) is not
193+
:const:`ZIP_BZIP2`, :const:`ZIP_LZMA`, or :const:`ZIP_ZSTANDARD`;
194+
unrecognized values will cause :exc:`NotImplementedError` to be raised. If
195+
:const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2`, :const:`ZIP_LZMA`, or
196+
:const:`ZIP_ZSTANDARD` is specified but the corresponding module
197+
(:mod:`zlib`, :mod:`bz2`, :mod:`lzma`, or :mod:`compression.zstd`) is not
183198
available, :exc:`RuntimeError` is raised. The default is :const:`ZIP_STORED`.
184199

185200
If *allowZip64* is ``True`` (the default) zipfile will create ZIP files that
@@ -194,6 +209,10 @@ ZipFile Objects
194209
(see :class:`zlib <zlib.compressobj>` for more information).
195210
When using :const:`ZIP_BZIP2` integers ``1`` through ``9`` are accepted
196211
(see :class:`bz2 <bz2.BZ2File>` for more information).
212+
When using :const:`ZIP_ZSTANDARD` integers ``-131072`` through ``22`` are
213+
commonly accepted (see
214+
:attr:`CompressionParameter.compression_level <compression.zstd.CompressionParameter.compression_level>`
215+
for more on retrieving valid values and their meaning).
197216

198217
The *strict_timestamps* argument, when set to ``False``, allows to
199218
zip files older than 1980-01-01 at the cost of setting the
@@ -415,9 +434,10 @@ ZipFile Objects
415434
read or append. *pwd* is the password used for encrypted files as a :class:`bytes`
416435
object and, if specified, overrides the default password set with :meth:`setpassword`.
417436
Calling :meth:`read` on a ZipFile that uses a compression method other than
418-
:const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or
419-
:const:`ZIP_LZMA` will raise a :exc:`NotImplementedError`. An error will also
420-
be raised if the corresponding compression module is not available.
437+
:const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2`,
438+
:const:`ZIP_LZMA`, or :const:`ZIP_ZSTANDARD` will raise a
439+
:exc:`NotImplementedError`. An error will also be raised if the
440+
corresponding compression module is not available.
421441

422442
.. versionchanged:: 3.6
423443
Calling :meth:`read` on a closed ZipFile will raise a :exc:`ValueError`.

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.