我有一个.exe安装程序,可以很容易地打开与7zip;它的内容可以提取不安装。
我使用预编译的7z.exe和python的subprocess
来提取它。
import os, subprocess
subprocess.call(r'"7z.exe" x ' + "Installer.exe" + ' -o' + os.getcwd())
不过,现在我正在寻找一个方法,它将是纯代码,不依赖于任何外部可执行文件,以提取压缩exe的内容。
我尝试过类似
tarfile, PyLZMA, py7zlib
的库,但是它们无法提取exe,或者会抱怨文件格式无效,等等。最佳答案:
自解压归档文件只是一个可执行文件,末尾有一个7zip归档文件您可以查找存档文件的所有可能开头,并尝试从那里开始解压缩文件句柄:
HEADER = b'7z\xBC\xAF\x27\x1C'
def try_decompressing_archive(filename):
with open(filename, 'rb') as handle:
start = 0
# Try decompressing the archive at all the possible header locations
while True:
handle.seek(start)
try:
return decompress_archive(handle)
except SomeDecompressionException:
# We find the next instance of HEADER, skipping the current one
start += handle.read().index(HEADER, 1)