Advertisement
Najeebsk

RAR-EXPLORER2.0.pyw

Sep 28th, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.13 KB | None | 0 0
  1. import os
  2. import subprocess
  3. import ctypes
  4. import sys
  5. import tkinter as tk
  6. from tkinter import filedialog, Listbox, Scrollbar, Entry, Text
  7. import rarfile  # Requires 'rarfile' module: pip install rarfile
  8. import shutil
  9. import stat
  10.  
  11. def run_as_admin():
  12.     if not ctypes.windll.shell32.IsUserAnAdmin():
  13.         ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
  14.  
  15. def browse_vhd():
  16.     file_path = filedialog.askopenfilename(title="Select VHD or RAR File",
  17.                                            filetypes=(("RAR Files", "*.rar"),  ("All Files", "*.*")))
  18.     vhd_entry.delete(0, tk.END)
  19.     vhd_entry.insert(0, file_path)
  20.  
  21.     if file_path.endswith('.rar'):
  22.         load_rar(file_path)
  23.  
  24. def load_rar(file_path):
  25.     """Loads the contents of a RAR file into the listbox."""
  26.     global rar_files
  27.     rar_files = []
  28.     try:
  29.         with rarfile.RarFile(file_path) as rf:
  30.             rar_files = rf.namelist()
  31.             file_list.delete(0, tk.END)
  32.             for file in rar_files:
  33.                 file_list.insert(tk.END, file)
  34.     except Exception as e:
  35.         result_field.delete(1.0, tk.END)
  36.         result_field.insert(tk.END, f"Error loading RAR file: {e}")
  37.  
  38. def open_file(event):
  39.     """Handles the double-click event to open a selected file from the RAR archive."""
  40.     selection = file_list.curselection()
  41.     if selection:
  42.         file_name = file_list.get(selection[0])
  43.         rar_path = vhd_entry.get()
  44.         rar_password = password_entry.get()  # Get the password entered by the user
  45.  
  46.         if rar_path.endswith('.rar'):
  47.             # Extract the selected file from the RAR archive
  48.             try:
  49.                 with rarfile.RarFile(rar_path) as rf:
  50.                     temp_dir = os.path.join(os.environ['TEMP'], "rar_temp")
  51.                     os.makedirs(temp_dir, exist_ok=True)
  52.  
  53.                     # Check if the file requires a password
  54.                     if rf.needs_password():
  55.                         if not rar_password:
  56.                             result_field.delete(1.0, tk.END)
  57.                             result_field.insert(tk.END, "This RAR file requires a password. Please provide it.")
  58.                             return
  59.  
  60.                     # Extract the selected file to a temp directory with the provided password (if any)
  61.                     rf.extract(file_name, temp_dir, pwd=rar_password if rar_password else None)
  62.                     extracted_file_path = os.path.join(temp_dir, file_name)
  63.  
  64.                     # Open the file using the default program
  65.                     if os.path.exists(extracted_file_path):
  66.                         os.startfile(extracted_file_path)
  67.                     else:
  68.                         result_field.delete(1.0, tk.END)
  69.                         result_field.insert(tk.END, f"Failed to extract or open {file_name}.")
  70.             except rarfile.BadRarFile:
  71.                 result_field.delete(1.0, tk.END)
  72.                 result_field.insert(tk.END, "Error: The RAR file is corrupt or invalid.")
  73.             except rarfile.RarWrongPassword:
  74.                 result_field.delete(1.0, tk.END)
  75.                 result_field.insert(tk.END, "Error: Incorrect password for this RAR file.")
  76.             except Exception as e:
  77.                 result_field.delete(1.0, tk.END)
  78.                 result_field.insert(tk.END, f"Error opening file: {e}")
  79.  
  80. def search_files():
  81.     """Search files in the listbox."""
  82.     search_term = search_entry.get().lower()
  83.     file_list.delete(0, tk.END)
  84.     for file in rar_files:
  85.         if search_term in file.lower():
  86.             file_list.insert(tk.END, file)
  87.  
  88. def remove_readonly(func, path, _):
  89.     """Clear the read-only bit and retry if removal fails."""
  90.     os.chmod(path, stat.S_IWRITE)
  91.     func(path)
  92.  
  93. def delete_temp_folder():
  94.     """Deletes the temp folder used for extracting RAR files."""
  95.     temp_dir = os.path.join(os.environ['TEMP'], "rar_temp")
  96.    
  97.     if os.path.exists(temp_dir):
  98.         try:
  99.             # Attempt to delete the folder and handle any permission issues
  100.             shutil.rmtree(temp_dir, onerror=remove_readonly)
  101.             result_field.delete(1.0, tk.END)
  102.             result_field.insert(tk.END, "Temporary folder deleted successfully.")
  103.         except Exception as e:
  104.             result_field.delete(1.0, tk.END)
  105.             result_field.insert(tk.END, f"Error deleting temp folder: {e}")
  106.     else:
  107.         result_field.delete(1.0, tk.END)
  108.         result_field.insert(tk.END, "Temp folder does not exist.")
  109.  
  110. def main():
  111.     if ctypes.windll.shell32.IsUserAnAdmin():
  112.         global rar_files  # Use a global list to store files from RAR
  113.  
  114.         # Create the main window
  115.         root = tk.Tk()
  116.         root.title("Najeeb RAR Password File Opener")
  117.         root.configure(bg='#ADD8E6')  # Light blue background
  118.  
  119.         # Create widgets for RAR file selection and mounting/unmounting
  120.         tk.Label(root, text="RAR Password File:", bg='#ADD8E6').grid(row=0, column=0, padx=5, pady=5)
  121.         global vhd_entry
  122.         vhd_entry = Entry(root, width=50)
  123.         vhd_entry.grid(row=0, column=1, padx=5, pady=5)
  124.  
  125.         browse_button = tk.Button(root, text="Browse", command=browse_vhd, bg='#FF6347', fg='white')  # Tomato color button
  126.         browse_button.grid(row=0, column=2, padx=5, pady=5)
  127.  
  128.         password_label = tk.Label(root, text="RAR Password (if any):", bg='#ADD8E6')
  129.         password_label.grid(row=2, column=0, padx=5, pady=5)
  130.  
  131.         global password_entry
  132.         password_entry = Entry(root, width=20, show="*")
  133.         password_entry.grid(row=2, column=1, padx=5, pady=5)
  134.  
  135.         # Create a file listbox with a scrollbar
  136.         global file_list
  137.         file_list = Listbox(root, width=110, height=20)
  138.         file_list.grid(row=3, column=0, columnspan=3, padx=5, pady=5)
  139.  
  140.         scrollbar = Scrollbar(root)
  141.         scrollbar.grid(row=3, column=3, sticky='ns')
  142.  
  143.         file_list.config(yscrollcommand=scrollbar.set)
  144.         scrollbar.config(command=file_list.yview)
  145.  
  146.         # Bind double-click to open the file
  147.         file_list.bind('<Double-Button-1>', open_file)
  148.  
  149.         # Create a search entry and button
  150.         search_label = tk.Label(root, text="Search Files:", bg='#ADD8E6')
  151.         search_label.grid(row=4, column=0, padx=5, pady=5)
  152.  
  153.         global search_entry
  154.         search_entry = Entry(root, width=50)
  155.         search_entry.grid(row=4, column=1, padx=5, pady=5)
  156.  
  157.         search_button = tk.Button(root, text="Search", command=search_files, bg='#32CD32', fg='white')  # Lime green button
  158.         search_button.grid(row=4, column=2, padx=5, pady=5)
  159.  
  160.         # Create a delete button to delete temp folder
  161.         delete_button = tk.Button(root, text="Delete Temp Folder", command=delete_temp_folder, bg='#DC143C', fg='white')  # Crimson color button
  162.         delete_button.grid(row=5, column=0, columnspan=3, padx=5, pady=5)
  163.  
  164.         # Create a result text field
  165.         global result_field
  166.         result_field = Text(root, width=80, height=5)
  167.         result_field.grid(row=6, column=0, columnspan=3, padx=5, pady=5)
  168.  
  169.         root.mainloop()
  170.     else:
  171.         run_as_admin()
  172.  
  173. if __name__ == "__main__":
  174.     main()
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement