如何在python代码中使用lzma2?

时间:2013-05-10 08:01:37

标签: python compression 7zip lzma

我知道有一个名为pylzma的模块。但它只支持lzma,而不支持lzma2。

我目前的解决方案是使用subprocess.call()来调用7z程序。

有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用backports.lzma,有关详细信息,请参阅:Python 2.7: Compressing data with the XZ format using the "lzma" module

然后,这只是一个例子:

from backports import lzma

with open('hello.xz', 'wb') as f:
    f.write(lzma.compress(b'hello', format=lzma.FORMAT_XZ))

或更简单(默认为XZ格式):

with lzma.open('hello.xz', 'wb') as f:
    f.write(b'hello')

有关使用详情,请参阅http://docs.python.org/dev/library/lzma.html