{"id":3789,"library":"rarfile","title":"rarfile","description":"rarfile is a Python library for reading RAR archives, providing a `zipfile`-like interface. It acts as a wrapper around external utilities like `unrar`, `7zip`, `unar`, or `unrar-free`. The project is actively maintained with regular updates and bug fixes.","status":"active","version":"4.2","language":"en","source_language":"en","source_url":"https://github.com/markokr/rarfile","tags":["archive","compression","rar","file-management"],"install":[{"cmd":"pip install rarfile","lang":"bash","label":"Install rarfile"}],"dependencies":[],"imports":[{"symbol":"RarFile","correct":"from rarfile import RarFile"}],"quickstart":{"code":"import rarfile\nimport os\n\n# NOTE: This example requires an external 'unrar' (or 7zip/unar) binary\n# to be installed and available in your system's PATH.\n# Create a dummy rar file for demonstration (requires external tool)\n# If you don't have one, replace 'example.rar' with an existing file.\n# For a real scenario, you'd have an actual .rar file.\n\n# Placeholder for a RAR file path\nrar_file_path = 'example.rar'\n\n# If you have an unrar tool, you could try creating a dummy rar file.\n# For this example, we'll assume one exists or will be created externally.\n# Example: os.system('rar a example.rar dummy_file.txt')\n# This is out of scope for the quickstart, assuming the rar exists.\n\nif not os.path.exists(rar_file_path):\n    print(f\"Warning: '{rar_file_path}' not found. Please create or provide an actual RAR file for this example.\")\n    print(\"Skipping quickstart execution due to missing RAR file.\")\nelse:\n    try:\n        with rarfile.RarFile(rar_file_path, 'r') as rf:\n            print(f\"Contents of {rar_file_path}:\")\n            for info in rf.infolist():\n                print(f\"  - {info.filename} ({info.file_size} bytes)\")\n\n            # Example: Extract a specific file (if it exists)\n            # file_to_extract = 'some_file_in_rar.txt'\n            # if file_to_extract in rf.namelist():\n            #     print(f\"\\nExtracting {file_to_extract}...\")\n            #     rf.extract(file_to_extract, path='./extracted_files')\n            #     print(f\"Extracted {file_to_extract} to ./extracted_files\")\n\n            # Example: Extract all files\n            # print(\"\\nExtracting all files...\")\n            # rf.extractall(path='./extracted_all')\n            # print(\"All files extracted to ./extracted_all\")\n\n    except rarfile.RarFileError as e:\n        print(f\"Error opening RAR file: {e}\")\n    except Exception as e:\n        print(f\"An unexpected error occurred: {e}\")\n","lang":"python","description":"This quickstart demonstrates how to open a RAR archive, list its contents, and shows commented-out examples for extracting files. It requires an external `unrar` binary (or similar) to be installed on your system and accessible via PATH for `rarfile` to function."},"warnings":[{"fix":"Install one of the supported external RAR utilities (e.g., `sudo apt-get install unrar` on Debian/Ubuntu, `brew install unrar` on macOS, or download from RARLAB for Windows) and ensure it's accessible via your system's PATH.","message":"rarfile itself does not contain RAR decompression logic. It acts as a wrapper and requires an external binary like `unrar` (the official RARLAB utility), `7zip` (`p7zip` on Linux), `unar`, or `unrar-free` to be installed on your system and available in your PATH.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Adjust code to expect or strip trailing slashes for directory names. For example, `entry.filename.rstrip('/')` if you need the name without the slash.","message":"Starting with v4.0, directory names in the `infolist()` and `namelist()` will consistently have a '/' appended at the end to match `zipfile` behavior. This might break code that expects directory names without a trailing slash or uses string matching.","severity":"breaking","affected_versions":"4.0 and later"},{"fix":"If these features are critical, consider using the external `unrar` tool directly or downgrading to a version prior to 4.0 (not recommended due to other improvements).","message":"In v4.0, the internal `RarFile.extract` implementation was rewritten to use `RarFile.open`. As a result, certain advanced features like hard links, NTFS streams, and junctions are no longer supported by `rarfile`'s internal extraction mechanism.","severity":"breaking","affected_versions":"4.0 and later"},{"fix":"Always provide the correct password using the `pwd` argument when opening encrypted RAR files: `rarfile.RarFile(filename, 'r', pwd=b'your_password')`. Note that the password should be bytes.","message":"While v4.1 improved RAR5 password handling by checking the password before attempting to read file data, ensuring correct password provision for encrypted RAR5 archives is crucial. Incorrect passwords will still lead to errors.","severity":"gotcha","affected_versions":"All versions, improved in 4.1"},{"fix":"Upgrade your Python environment to 3.8 or newer (3.12 is now supported and tested).","message":"In v4.2, support for Python 3.7 was dropped from the CI/testing matrix. While it might still function, official support and testing are no longer guaranteed for Python 3.7.","severity":"gotcha","affected_versions":"4.2 and later"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}