如何一次解压缩具有多个文件夹/文件的.xz文件? [英] How to decompress a .xz file which has multiple folders/files inside, in a single go?

查看:565
本文介绍了如何一次解压缩具有多个文件夹/文件的.xz文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解压缩一个.xz文件,该文件中包含一些推子和文件.我看不到使用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 ?

推荐答案

Python 3.3

import tarfile

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

Python 2.7

在Python 2.7中需要 lzma

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('.')

这篇关于如何一次解压缩具有多个文件夹/文件的.xz文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆