2

Is there a way to distribute a Python script that can unpack a .tar.xz file?

Specifically:

  • This needs to run on other people's machines, not mine, so I can't require any extra modules to have been installed.

  • I can get away with assuming the presence of Python 2.7, but not 3.x.

So that seems to amount to asking whether out-of-the-box Python 2.7 has such a feature, and as far as I can tell the answer is no, but is there anything I'm missing?

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • In which machine ? I mean windows or linux ? – Rahul K P Apr 29 '16 at 09:24
  • @RahulKP Ultimately it needs to be cross-platform. That having been said, if there isn't a truly cross-platform solution, I can go with separate solutions for Windows and Linux. In that case, I can unpack files on Linux by using tar. – rwallace Apr 29 '16 at 09:35
  • 1
    Yeah in linux you can implement with `tar` and `os`. It's very simple. And i don't have much experice in Windows. – Rahul K P Apr 29 '16 at 09:41

1 Answers1

3

First decompress the xz file into tar data and then extract the tar data:

import lzma
import tarfile

with lzma.open("file.tar.xz") as fd:
    with tarfile.open(fileobj=fd) as tar:
        content = tar.extractall('/path/to/extract/to')

For python2.7 you need to install pip27.pylzma

Cyrbil
  • 6,341
  • 1
  • 24
  • 40
  • 1
    I get an error when trying to import lzma. Ditto pylzma. I get the impression neither of those packages ships with Python 2.7. – rwallace Apr 29 '16 at 09:36
  • lzma does not ship with python27, or by pip27. pylzma does ship with pip27. – Cutton Eye Jul 05 '19 at 08:38
  • see https://stackoverflow.com/questions/17217073/how-to-decompress-a-xz-file-which-has-multiple-folders-files-inside-in-a-singl for a Python 3 answer – brad Apr 27 '22 at 19:31