pythonpython-3.xrar

Cant unpack a password protected rar file in python


I am trying to unpack a password protected rarfile.

with rarfile.RarFile(file_path) as rar_file:  # Use rarfile.RarFile instead of unrar.RarFile
    if pw:
        rar_file.setpassword(pw.encode())  # Set the password before calling extractall()
    rar_file.extractall(unpack_dir)

    logger.info(f"Unpacked {filename} to {unpack_dir}")

this is the error:

2023-07-22 04:01:25,657 - ERROR - An unexpected error occurred while unpacking log.rar: Failed the read enough data: req=231 got=0

  File "C:\Users\User\AppData\Roaming\Python\Python311\site-packages\rarfile.py", line 2197, in read
    raise BadRarFile("Failed the read enough data: req=%d got=%d" % (orig, len(data)))
rarfile.BadRarFile: Failed the read enough data: req=231 got=0

The file isnt corrupted and the password is correct.


Solution

  • I did not get the code to work with any library. for everyone else with the same problem I would suggest a workaround: just use os.system and the CMD commands.

    Please keep in mind that you have to add winrar to path for this to work!

    import os
    os.system(fr'rar x -pYOURPASSWODHERE "ORIGIN PATH" "PATH TO UNPACK TO")
    

    example (the password ist "MyPassword123"):

    os.system(fr'rar x -pMyPassword123 "C:\Users\User\Downloads\downloaded.rar" "C:\Users\User\Desktop\")