Advertisement
Najeebsk

RAR-EXPLORER.pyw

Sep 28th, 2024 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.60 KB | None | 0 0
  1. import os
  2. import rarfile
  3. import tkinter as tk
  4. from tkinter import filedialog, messagebox, Scrollbar
  5. import shutil
  6. import stat
  7.  
  8. # Function to load the RAR file and list its contents
  9. def load_rar_file():
  10.     rar_path = filedialog.askopenfilename(filetypes=[("RAR files", "*.rar")])
  11.     if rar_path:
  12.         vhd_entry.delete(0, tk.END)
  13.         vhd_entry.insert(0, rar_path)
  14.  
  15.         # List files inside the RAR archive
  16.         try:
  17.             with rarfile.RarFile(rar_path) as rf:
  18.                 file_list.delete(0, tk.END)  # Clear current listbox
  19.                 for f in rf.infolist():
  20.                     file_list.insert(tk.END, f.filename)
  21.             result_field.delete(1.0, tk.END)
  22.             result_field.insert(tk.END, "RAR file loaded. Double-click a file to extract and open.")
  23.         except rarfile.BadRarFile:
  24.             result_field.delete(1.0, tk.END)
  25.             result_field.insert(tk.END, "Error: The RAR file is corrupt or invalid.")
  26.         except Exception as e:
  27.             result_field.delete(1.0, tk.END)
  28.             result_field.insert(tk.END, f"Error loading RAR file: {e}")
  29.  
  30. # Function to handle double-click event to open a selected file from the RAR archive
  31. def open_file(event):
  32.     selection = file_list.curselection()
  33.     if selection:
  34.         file_name = file_list.get(selection[0])
  35.         rar_path = vhd_entry.get()
  36.         rar_password = password_entry.get()  # Get the password entered by the user
  37.  
  38.         if rar_path.endswith('.rar'):
  39.             # Extract the selected file from the RAR archive
  40.             try:
  41.                 with rarfile.RarFile(rar_path) as rf:
  42.                     temp_dir = os.path.join(os.environ['TEMP'], "rar_temp")
  43.                     os.makedirs(temp_dir, exist_ok=True)
  44.  
  45.                     # Check if the file requires a password
  46.                     if rf.needs_password() and not rar_password:
  47.                         result_field.delete(1.0, tk.END)
  48.                         result_field.insert(tk.END, "This RAR file requires a password. Please provide it.")
  49.                         return
  50.  
  51.                     # Extract the selected file to a temp directory with the provided password (if any)
  52.                     rf.extract(file_name, temp_dir, pwd=rar_password if rar_password else None)
  53.                     extracted_file_path = os.path.join(temp_dir, file_name)
  54.  
  55.                     # Open the file using the default program
  56.                     if os.path.exists(extracted_file_path):
  57.                         os.startfile(extracted_file_path)  # Works on Windows
  58.                         result_field.delete(1.0, tk.END)
  59.                         result_field.insert(tk.END, f"File {file_name} opened successfully.")
  60.                     else:
  61.                         result_field.delete(1.0, tk.END)
  62.                         result_field.insert(tk.END, f"Failed to extract or open {file_name}.")
  63.             except rarfile.BadRarFile:
  64.                 result_field.delete(1.0, tk.END)
  65.                 result_field.insert(tk.END, "Error: The RAR file is corrupt or invalid.")
  66.             except rarfile.RarWrongPassword:
  67.                 result_field.delete(1.0, tk.END)
  68.                 result_field.insert(tk.END, "Error: Incorrect password for this RAR file.")
  69.             except Exception as e:
  70.                 result_field.delete(1.0, tk.END)
  71.                 result_field.insert(tk.END, f"Error opening file: {e}")
  72.         else:
  73.             result_field.delete(1.0, tk.END)
  74.             result_field.insert(tk.END, "Selected file is not a RAR file.")
  75.  
  76. def remove_readonly(func, path, _):
  77.     """Clear the read-only bit and retry if removal fails."""
  78.     os.chmod(path, stat.S_IWRITE)
  79.     func(path)
  80.            
  81.  
  82. def delete_temp_folder():
  83.     """Deletes the temp folder used for extracting RAR files."""
  84.     temp_dir = os.path.join(os.environ['TEMP'], "rar_temp")
  85.    
  86.     if os.path.exists(temp_dir):
  87.         try:
  88.             # Attempt to delete the folder and handle any permission issues
  89.             shutil.rmtree(temp_dir, onerror=remove_readonly)
  90.             result_field.delete(1.0, tk.END)
  91.             result_field.insert(tk.END, "Temporary folder deleted successfully.")
  92.         except Exception as e:
  93.             result_field.delete(1.0, tk.END)
  94.             result_field.insert(tk.END, f"Error deleting temp folder: {e}")
  95.     else:
  96.         result_field.delete(1.0, tk.END)
  97.         result_field.insert(tk.END, "Temp folder does not exist.")            
  98.  
  99. # Function to close the application
  100. def close_app():
  101.     root.destroy()
  102.  
  103. # Create the Tkinter main window
  104. root = tk.Tk()
  105. root.title("Najeeb RAR File Extractor")
  106. root.geometry("820x610")
  107.  
  108. # Frame for loading RAR file and password input
  109. top_frame = tk.Frame(root)
  110. top_frame.pack(pady=10)
  111.  
  112. vhd_label = tk.Label(top_frame, text="RAR File Path:", bg='#abebc6', fg='#17202a')
  113. vhd_label.grid(row=0, column=0, padx=5, pady=5)
  114.  
  115. vhd_entry = tk.Entry(top_frame, width=96)
  116. vhd_entry.grid(row=0, column=1, padx=5, pady=5)
  117.  
  118. load_button = tk.Button(top_frame, text="Load RAR", command=load_rar_file, bg='#0e6655', fg='white')
  119. load_button.grid(row=0, column=2, padx=5, pady=5)
  120.  
  121. password_label = tk.Label(top_frame, text="Password Rar (if needed):", bg='#DC143C', fg='white')
  122. password_label.grid(row=1, column=0, padx=5, pady=5)
  123.  
  124. password_entry = tk.Entry(top_frame, show="*", width=60)
  125. password_entry.grid(row=1, column=1, padx=5, pady=5)
  126.  
  127. # Frame for Listbox and Scrollbar
  128. list_frame = tk.Frame(root)
  129. list_frame.pack(pady=10)
  130.  
  131. # Listbox to display the files inside the RAR archive
  132. file_list = tk.Listbox(list_frame, height=24, width=130)
  133. file_list.pack(side=tk.LEFT, fill=tk.BOTH)
  134.  
  135. # Scrollbar for the Listbox
  136. scrollbar = tk.Scrollbar(list_frame)
  137. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  138.  
  139. # Configure the scrollbar with the listbox
  140. file_list.config(yscrollcommand=scrollbar.set)
  141. scrollbar.config(command=file_list.yview)
  142.  
  143. file_list.bind("<Double-1>", open_file)  # Bind double-click event to open file
  144.  
  145. # Result field to display messages
  146. result_field = tk.Text(root, height=2, width=98, wrap="word")
  147. result_field.pack(pady=10)
  148.  
  149. # Frame for control buttons
  150. bottom_frame = tk.Frame(root)
  151. bottom_frame.pack(pady=10)
  152.  
  153. close_button = tk.Button(bottom_frame, text="Close", command=close_app, bg='#f9e79f', fg='#DC143C')
  154. close_button.grid(row=0, column=0, padx=5, pady=5)
  155.  
  156. # Create a delete button to delete temp folder
  157. delete_button = tk.Button(bottom_frame, text="Delete Temp Folder", command=delete_temp_folder, bg='#DC143C', fg='white')  # Crimson color button
  158. delete_button.grid(row=0, column=1, columnspan=3, padx=5, pady=5)
  159.  
  160. # Start the Tkinter event loop
  161. root.mainloop()
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement