简体   繁体   English

如何使用 XZ 文件设置 tarfile 的压缩级别?

[英]How to set the compression level for tarfile with XZ files?

In Python an xz tarfile can be created:在 Python 中可以创建一个 xz tarfile:

handle = tarfile.open(dest, 'w:xz')

However this doesn't set the compression level.但是,这不会设置压缩级别。

Is it possible to set the XZ compression level with tarfile?是否可以使用 tarfile 设置 XZ 压缩级别?

The answer is a bit buried in the tarfile.open() documentation, but it says:答案有点隐藏在tarfile.open()文档中,但它说:

For modes 'w:gz', 'r:gz', 'w:bz2', 'r:bz2', 'x:gz', 'x:bz2', tarfile.open() accepts the keyword argument compresslevel (default 9) to specify the compression level of the file.对于模式 'w:gz'、'r:gz'、'w:bz2'、'r:bz2'、'x:gz'、'x:bz2',tarfile.open() 接受关键字参数 compresslevel(默认9) 指定文件的压缩级别。

Unfortunately, the documentation doesn't mention 'w:xz' as being supported.不幸的是,文档没有提到支持'w:xz' Trying it out just in case, I can confirm that it does not.试一试以防万一,我可以确认它没有。 This appears to be because the lzma module doesn't support a compresslevel argument.这似乎是因为lzma模块不支持 compresslevel 参数。 It does, however, support preset .但是,它确实支持preset Why they choose a different name here?为什么他们在这里选择不同的名字? Who knows.谁知道。 :-( It does appear they made a different choice for the default compression level. With the others, they chose the maximum level by default. With xz, they chose for the default level of compression used by the tool (level 6). :-( 看起来他们对默认压缩级别做出了不同的选择。对于其他人,他们默认选择了最大级别。对于 xz,他们选择了工具使用的默认压缩级别(级别 6)。

Which means you'd do something like this:这意味着你会做这样的事情:

handle = tarfile.open(dest, 'w:xz', preset=9)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM