Python3:lzma 解压 .7z 文件 [英] Python3: lzma unpack .7z file

查看:81
本文介绍了Python3:lzma 解压 .7z 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解压一个 .7z 文件.根据这个问题我可以使用lzma 包来执行此操作.

I would like to unpack a .7z file. According to this question I can use the lzma package to do this.

我期待类似的东西

import lzma
#...
with lzma.open('myFile.7z') as f:
    f.extractall('.')

把文件解压到当前目录但是好像不存在这样的东西.此外尝试像

To extract the file into the current directory but it seems something like this does not exist. Furthermore trying something like

import lzma
#...
with lzma.open('myFile.7z') as f:
    file_content = f.read()
    print(file_content)

确实产生了_lzma.LZMAError:解码器不支持输入格式.如何检查格式?我很惊讶,因为我认为 7zip 和 .7z 格式都是开源的,python 应该支持一切.

did yield _lzma.LZMAError: Input format not supported by decoder. How can I check the format? And I am quite surprised because I thought both 7zip and the .7z format are open source and python should support everything.

我看到很多答案,人们只是通过子进程调用 7zip 可执行文件,但这不是我想要的.我正在寻找一个简单的 python3 解决方案.

I saw a lot of answers where people were just calling the 7zip executable with a subprocess but this is not want I want to do. I am looking for a plain python3 solution.

推荐答案

LZMA 和 7z 是两个非常不同的野兽.

LZMA and 7z are two very different beasts.

用最简单的术语来说,LZMA 是一种无损压缩算法.这意味着,您向 LZMA 提供一些数据,它会压缩并为您提供输出.它没有文件、文件夹或如何存储它们的意义.

In the simplest of terms LZMA is a lossless compression algorithm. This means that, you feed LZMA some data, it will compress and give you the output. It has no sense of files, folders or how to store them.

另一方面,7z 是一种归档文件格式,这意味着 7z 是一种完整的包.您有一些文件和文件夹,将它们提供给 7z,它会整齐地压缩它们,并将它们存储在一个文件中(存档).请注意,7z 使用 LZMA 和其他算法的混合来压缩文件并将其存储在其 7z 存档文件中.

7z on the other hand is an archive file format, and this means that 7z is a complete package. You have a few files and folders, feed it to 7z, it will neatly compress them, and store them in a single file (archive). Please note that, 7z uses LZMA and a cocktail of other algorithms to compress and store files in its 7z archive file.

以下是维基百科对这两者的评价:

Here is what wikipedia has got to say about the two:

7z 是一种压缩存档文件格式,支持多种不同的数据压缩、加密和预处理算法.

7z is a compressed archive file format that supports several different data compression, encryption and pre-processing algorithms.

Lempel–Ziv–Markov 链算法(LZMA) 是一种用于执行无损数据压缩的算法.它自 1996 年或 1998 年开始开发3 并首次用于 7z 格式的7-Zip 存档器.

The Lempel–Ziv–Markov chain algorithm (LZMA) is an algorithm used to perform lossless data compression. It has been under development either since 1996 or 19983 and was first used in the 7z format of the 7-Zip archiver.

所以简而言之,您不能使用 lzma 来创建或提取 7z 文件.据我所知,没有办法使用 python 提取 7z 文件,除了: 请参阅下面的更新.

import os
os.system( '7z x archive.7z -oPath/to/Name' )

更新:2019 年 5 月

由于有人对在 python 中提取 7z 文件感兴趣,我认为应该进行更新.截至 2019 年(也许更早),python 的 libarchive bindings 支持 7z 格式.上面的链接给出了从 7z 存档中提取文件的示例.

Update: May 2019

Since there some interest about extracting 7z files in python, I thought an update is in order. As of 2019 (perhaps even earlier), libarchive bindings for python do support 7z format. An example for extracting files from 7z archive is given in above link.

这篇关于Python3:lzma 解压 .7z 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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