Skip to content
4 changes: 4 additions & 0 deletions Doc/library/tarfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ Some facts and figures:
``'x:bz2'``, :func:`tarfile.open` accepts the keyword argument
*compresslevel* (default ``9``) to specify the compression level of the file.

For modes ``'w:xz'``, ``'x:xz'``, :func:`tarfile.open` accepts the keyword
argument *preset* to specify the compression level of the file. The default
is :class:`PRESET_DEFAULT <lzma.LZMACompressor>`.

For special purposes, there is a second format for *mode*:
``'filemode|[compression]'``. :func:`tarfile.open` will return a :class:`TarFile`
object that processes its data as a stream of blocks. No random seeking will
Expand Down
20 changes: 17 additions & 3 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,15 +1582,29 @@ def test_create_taropen_pathlike_name(self):


class GzipCreateTest(GzipTest, CreateTest):
pass
def test_create_with_compresslevel(self):
with tarfile.open(tmpname, self.mode, compresslevel=1) as tobj:
tobj.add(self.file_path)

with tarfile.open(tmpname, 'r:gz', compresslevel=1) as tobj:
pass


class Bz2CreateTest(Bz2Test, CreateTest):
pass
def test_create_with_compresslevel(self):
with tarfile.open(tmpname, self.mode, compresslevel=1) as tobj:
tobj.add(self.file_path)

with tarfile.open(tmpname, 'r:bz2', compresslevel=1) as tobj:
pass


class LzmaCreateTest(LzmaTest, CreateTest):
pass
# Unlike gz and bz2, xz uses the preset keyword instead of compresslevel.
# It does not allow for preset to be specified when reading.
def test_create_with_preset(self):
with tarfile.open(tmpname, self.mode, preset=1) as tobj:
tobj.add(self.file_path)


class CreateWithXModeTest(CreateTest):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The compresslevel and preset keywords for tarfile.open are now both documented
and tested.