如何使用python提取exearchive的内容?

2024-04-26 20:39:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个.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,或者会抱怨文件格式无效,等等


Tags: 方法代码import可执行文件内容osinstallercall
1条回答
网友
1楼 · 发布于 2024-04-26 20:39:08

自解压归档文件只是一个可执行文件,末尾有一个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)

相关问题 更多 >