You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Python脚本在Linux系统中一次性解压多卷RAR归档文件

How to Extract Split RAR Archives in Linux with a Python Script (Mimicking rar x)

Got it, I've been in this situation before—dealing with split RAR archives can be a pain if you don't have the right workflow down. Below is a Python script that replicates the behavior of rar x for multi-volume RARs (like Filename-001.rar, Filename-002.rar, etc.) on Linux, plus all the details you need to get it working smoothly.

Prerequisite: Install unrar

First, you need the unrar tool installed on your system—Python doesn't have a built-in RAR handler that's reliable for split archives, and using the system's unrar aligns with how rar x works natively.

For Debian/Ubuntu-based distros:

sudo apt update && sudo apt install unrar

For Fedora/RHEL-based distros:

sudo dnf install unrar

The Python Script

This script takes the first volume of your split RAR as input, checks for common issues, and calls unrar x to handle the rest (just like the rar x command would).

import os
import subprocess
import sys

def extract_split_rar(first_rar_path):
    # Check if the specified file exists
    if not os.path.exists(first_rar_path):
        print(f"Error: File '{first_rar_path}' doesn't exist.")
        return False
    
    # Verify this is likely the first volume of the split archive
    valid_first_volume_suffixes = ("-001.rar", ".part1.rar")
    if not first_rar_path.endswith(valid_first_volume_suffixes):
        print("Warning: This might not be the first volume of the split RAR.")
        print("Make sure you're pointing to the initial volume (e.g., Filename-001.rar or Filename.part1.rar)")
        proceed = input("Want to try extracting anyway? (y/n): ").strip().lower()
        if proceed != 'y':
            return False
    
    # Run the unrar x command (matches rar x behavior: preserves directory structure)
    cmd = ["unrar", "x", first_rar_path]
    
    try:
        # Execute the command and capture output
        result = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
        print("\nExtraction finished successfully!")
        print("Command output:")
        print(result.stdout)
        return True
    except subprocess.CalledProcessError as e:
        print(f"\nExtraction failed: {e.stderr}")
        return False
    except FileNotFoundError:
        print("Error: 'unrar' isn't installed. Follow the prerequisite steps above to install it.")
        return False

if __name__ == "__main__":
    # Check for correct command-line arguments
    if len(sys.argv) != 2:
        print("Usage: python3 extract_split_rar.py <path-to-first-rar-volume>")
        print("Example: python3 extract_split_rar.py ./Documents/MyArchive-001.rar")
        sys.exit(1)
    
    first_rar_file = sys.argv[1]
    extract_split_rar(first_rar_file)

How to Use the Script

  1. Save the code above as extract_split_rar.py
  2. Make it executable (optional but convenient):
    chmod +x extract_split_rar.py
    
  3. Run it with the path to your first split RAR volume:
    python3 extract_split_rar.py ./MyArchive-001.rar
    

Key Notes

  • All volumes in one directory: Ensure all split RAR files (001, 002, etc.) are in the same folder as the first volume—unrar will automatically detect and use them.
  • Preserves directory structure: Just like rar x, the script uses unrar x which extracts files while keeping their original folder hierarchy.
  • Handles common naming schemes: Works for both -001.rar and .part1.rar style split archives.

内容的提问来源于stack exchange,提问作者tlfSnow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 14:29:07