Phi*_*mes 12 python 7zip archive extraction
我想使用PyLZMA从存档中提取文件(例如test.7z)并将其解压缩到同一目录.
我是Python的新手,不知道如何开始.我做了一些谷歌搜索并找到了一些示例和文档,但我不明白它们是如何工作的.
有人可以发布我想要做的基本代码,以便我可以开始工作和理解吗?
Bri*_*n B 15
这是一个用于处理基本功能的Python类.我用它来做我自己的工作:
import py7zlib
class SevenZFile(object):
@classmethod
def is_7zfile(cls, filepath):
'''
Class method: determine if file path points to a valid 7z archive.
'''
is7z = False
fp = None
try:
fp = open(filepath, 'rb')
archive = py7zlib.Archive7z(fp)
n = len(archive.getnames())
is7z = True
finally:
if fp:
fp.close()
return is7z
def __init__(self, filepath):
fp = open(filepath, 'rb')
self.archive = py7zlib.Archive7z(fp)
def extractall(self, path):
for name in self.archive.getnames():
outfilename = os.path.join(path, name)
outdir = os.path.dirname(outfilename)
if not os.path.exists(outdir):
os.makedirs(outdir)
outfile = open(outfilename, 'wb')
outfile.write(self.archive.getmember(name).read())
outfile.close()
Run Code Online (Sandbox Code Playgroud)