LoginSignup
4
3

More than 5 years have passed since last update.

7-Zipの解凍パスワードを自動入力

Last updated at Posted at 2018-02-14

パスワード入力ダイアログが表示された状態でスクリプト実行。
引数はコマンドラインで与える。

パスワードが正しければ「OK」をクリックするところまで自動化。

from pywinauto.application import Application
import zipfile
import sys

fmpath=r'C:\Program Files\7-Zip\7zFM.exe'

def checkpw(path, pw):
    with zipfile.ZipFile(zipfilepath) as z:
        # choosing file of minimum size (> 0)
        fs = 0
        for i in z.infolist():
            if not i.is_dir():
                if fs == 0 or i.compress_size < fs:
                    fn = i.filename
                    fs = i.compress_size
        if fs > 0:
            try:
                with z.open(fn, pwd=pw.encode('utf-8')) as f:
                    return True
            except RuntimeError:
                pass
        return False

if __name__ == '__main__':
    app = Application().connect(path=fmpath)
    pathlist=app.FM.Edit.texts()
    if len(pathlist) > 0:
        zipfilepath=app.FM.Edit.texts()[0].rstrip('\\')

    pw=sys.argv[1]
    if checkpw(zipfilepath, pw):
        app.Dialog.Edit.set_text(pw)
        app.Dialog.OK.click()
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3