Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decompress a .xz file which has multiple folders/files inside, in a single go?

Tags:

python

lzma

I'm trying to uncompress a .xz file which has a few foders and files inside. I don't see a direct way to do this using lzma module. This is what I'm seeing for a decompress method :

In [1]: import lzma

In [2]: f = lzma.decompress("test.tar.xz")
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-2-3b08bf488f9d> in <module>()
----> 1 f = lzma.decompress("test.tar.xz")

error: unknown file format

Are there any other methods to un-compress this file so that it will create the resultant folder ?

like image 838
vimal Avatar asked Jun 20 '13 14:06

vimal


People also ask

What do I do with a .xz file?

Users often use XZ files to share compressed files over the Internet, via email, and on USB drives. XZ compression compresses files to smaller sizes than some alternatives, such as gzip and bzip2 compression.

What is a tar xz file?

A TAR. XZ file is a lossless data compression file format used for compressed streams. They are created by utilizing the “tar” command on Linux or UNIX operating systems, which is where the file type got its name.


1 Answers

Python 3.3

import tarfile

with tarfile.open('test.tar.xz') as f:
    f.extractall('.')

Python 2.7

Need lzma in Python 2.7

import contextlib
import lzma
import tarfile

with contextlib.closing(lzma.LZMAFile('test.tar.xz')) as xz:
    with tarfile.open(fileobj=xz) as f:
        f.extractall('.')
like image 67
falsetru Avatar answered Sep 21 '22 11:09

falsetru