2017-03-02 7 views
1

私は7zipで簡単に開くことができる.exeインストーラを利用できます。インストールせずに内容を抽出することができます。pythonを使ってexe-archiveの内容を抽出する方法は?

私は、事前にコンパイルされた7z.exeとPythonのsubprocessを使用して解凍しています。しかし、今、私は純粋なコードになり、パックされたexeファイルの内容を抽出するために、任意の外部の実行可能に依存しない方法のため探しています

import os, subprocess 
subprocess.call(r'"7z.exe" x ' + "Installer.exe" + ' -o' + os.getcwd()) 

私は自己解凍型アーカイブは上7zipをアーカイブとだけ実行可能であるなど、しかし、彼らはexeファイルを抽出するために失敗する、またはファイル形式が有効でないと文句を言うだろう

答えて

1

tarfile, PyLZMA, py7zlibのようなライブラリを試してみました終わり。アーカイブの可能なすべての開始点を探してそこから開始するファイルハンドルの解凍を試みることができます:

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) 
関連する問題