You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/index.rst
+45-22Lines changed: 45 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1470,42 +1470,65 @@ Use with tarfile module
1470
1470
1471
1471
Python's `tarfile <https://docs.python.org/3/library/tarfile.html>`_ module supports arbitrary compression algorithms by providing a file object.
1472
1472
1473
-
This code encapsulates a ``ZstdTarFile`` class using :py:class:`ZstdFile`, it can be used like `tarfile.TarFile <https://docs.python.org/3/library/tarfile.html#tarfile.TarFile>`_ class:
1474
-
1475
1473
.. sourcecode:: python
1476
1474
1477
1475
import tarfile
1478
1476
1479
-
# when using read mode (decompression), the level_or_option parameter
1480
-
# can only be a dict object, that represents decompression option. It
1481
-
# doesn't support int type compression level in this case.
1477
+
# compression
1478
+
with ZstdFile('archive.tar.zst', mode='w') as _fileobj, tarfile.open(fileobj=_fileobj, mode='w') as tar:
1479
+
# do something
1480
+
1481
+
# decompression
1482
+
with ZstdFile('archive.tar.zst', mode='r') as _fileobj, tarfile.open(fileobj=_fileobj) as tar:
1483
+
# do something
1484
+
1485
+
Alternatively, it is possible to extend the ``Tarfile`` class, so that it supports decompressing ``.tar.zst`` file automatically, as well as adding the following modes: ``r:zst``, ``w:zst`` and ``x:zst``.
1486
+
1487
+
.. sourcecode:: python
1488
+
1489
+
from tarfile import TarFile, CompressionError, ReadError
with ZstdTarFile('archive.tar.zst', mode='w', level_or_option=5) as tar:
1523
+
# compression
1524
+
with CustomTarFile.open('archive.tar.zst', mode='w:zst') as tar:
1502
1525
# do something
1503
1526
1504
-
# read .tar.zst file (decompression)
1505
-
with ZstdTarFile('archive.tar.zst', mode='r') as tar:
1527
+
# decompression
1528
+
with CustomTarFile.open('archive.tar.zst') as tar:
1506
1529
# do something
1507
1530
1508
-
When the above code is in read mode (decompression), and selectively read files multiple times, it may seek to a position before the current position, then the decompression has to be restarted from zero. If this slows down the operations, you can:
1531
+
In both implementations, when selectively reading files multiple times, it may seek to a position before the current position; then the decompression has to be restarted from zero. If this slows down the operations, you can:
1509
1532
1510
1533
#. Use :py:class:`SeekableZstdFile` class to create/read .tar.zst file.
1511
1534
#. Decompress the archive to a temporary file, and read from it. This code encapsulates the process:
0 commit comments