Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import rarfile
- import tkinter as tk
- from tkinter import filedialog, messagebox, Scrollbar
- import shutil
- import stat
- # Function to load the RAR file and list its contents
- def load_rar_file():
- rar_path = filedialog.askopenfilename(filetypes=[("RAR files", "*.rar")])
- if rar_path:
- vhd_entry.delete(0, tk.END)
- vhd_entry.insert(0, rar_path)
- # List files inside the RAR archive
- try:
- with rarfile.RarFile(rar_path) as rf:
- file_list.delete(0, tk.END) # Clear current listbox
- for f in rf.infolist():
- file_list.insert(tk.END, f.filename)
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, "RAR file loaded. Double-click a file to extract and open.")
- except rarfile.BadRarFile:
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, "Error: The RAR file is corrupt or invalid.")
- except Exception as e:
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, f"Error loading RAR file: {e}")
- # Function to handle double-click event to open a selected file from the RAR archive
- def open_file(event):
- selection = file_list.curselection()
- if selection:
- file_name = file_list.get(selection[0])
- rar_path = vhd_entry.get()
- rar_password = password_entry.get() # Get the password entered by the user
- if rar_path.endswith('.rar'):
- # Extract the selected file from the RAR archive
- try:
- with rarfile.RarFile(rar_path) as rf:
- temp_dir = os.path.join(os.environ['TEMP'], "rar_temp")
- os.makedirs(temp_dir, exist_ok=True)
- # Check if the file requires a password
- if rf.needs_password() and not rar_password:
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, "This RAR file requires a password. Please provide it.")
- return
- # Extract the selected file to a temp directory with the provided password (if any)
- rf.extract(file_name, temp_dir, pwd=rar_password if rar_password else None)
- extracted_file_path = os.path.join(temp_dir, file_name)
- # Open the file using the default program
- if os.path.exists(extracted_file_path):
- os.startfile(extracted_file_path) # Works on Windows
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, f"File {file_name} opened successfully.")
- else:
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, f"Failed to extract or open {file_name}.")
- except rarfile.BadRarFile:
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, "Error: The RAR file is corrupt or invalid.")
- except rarfile.RarWrongPassword:
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, "Error: Incorrect password for this RAR file.")
- except Exception as e:
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, f"Error opening file: {e}")
- else:
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, "Selected file is not a RAR file.")
- def remove_readonly(func, path, _):
- """Clear the read-only bit and retry if removal fails."""
- os.chmod(path, stat.S_IWRITE)
- func(path)
- def delete_temp_folder():
- """Deletes the temp folder used for extracting RAR files."""
- temp_dir = os.path.join(os.environ['TEMP'], "rar_temp")
- if os.path.exists(temp_dir):
- try:
- # Attempt to delete the folder and handle any permission issues
- shutil.rmtree(temp_dir, onerror=remove_readonly)
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, "Temporary folder deleted successfully.")
- except Exception as e:
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, f"Error deleting temp folder: {e}")
- else:
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, "Temp folder does not exist.")
- # Function to close the application
- def close_app():
- root.destroy()
- # Create the Tkinter main window
- root = tk.Tk()
- root.title("Najeeb RAR File Extractor")
- root.geometry("820x610")
- # Frame for loading RAR file and password input
- top_frame = tk.Frame(root)
- top_frame.pack(pady=10)
- vhd_label = tk.Label(top_frame, text="RAR File Path:", bg='#abebc6', fg='#17202a')
- vhd_label.grid(row=0, column=0, padx=5, pady=5)
- vhd_entry = tk.Entry(top_frame, width=96)
- vhd_entry.grid(row=0, column=1, padx=5, pady=5)
- load_button = tk.Button(top_frame, text="Load RAR", command=load_rar_file, bg='#0e6655', fg='white')
- load_button.grid(row=0, column=2, padx=5, pady=5)
- password_label = tk.Label(top_frame, text="Password Rar (if needed):", bg='#DC143C', fg='white')
- password_label.grid(row=1, column=0, padx=5, pady=5)
- password_entry = tk.Entry(top_frame, show="*", width=60)
- password_entry.grid(row=1, column=1, padx=5, pady=5)
- # Frame for Listbox and Scrollbar
- list_frame = tk.Frame(root)
- list_frame.pack(pady=10)
- # Listbox to display the files inside the RAR archive
- file_list = tk.Listbox(list_frame, height=24, width=130)
- file_list.pack(side=tk.LEFT, fill=tk.BOTH)
- # Scrollbar for the Listbox
- scrollbar = tk.Scrollbar(list_frame)
- scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- # Configure the scrollbar with the listbox
- file_list.config(yscrollcommand=scrollbar.set)
- scrollbar.config(command=file_list.yview)
- file_list.bind("<Double-1>", open_file) # Bind double-click event to open file
- # Result field to display messages
- result_field = tk.Text(root, height=2, width=98, wrap="word")
- result_field.pack(pady=10)
- # Frame for control buttons
- bottom_frame = tk.Frame(root)
- bottom_frame.pack(pady=10)
- close_button = tk.Button(bottom_frame, text="Close", command=close_app, bg='#f9e79f', fg='#DC143C')
- close_button.grid(row=0, column=0, padx=5, pady=5)
- # Create a delete button to delete temp folder
- delete_button = tk.Button(bottom_frame, text="Delete Temp Folder", command=delete_temp_folder, bg='#DC143C', fg='white') # Crimson color button
- delete_button.grid(row=0, column=1, columnspan=3, padx=5, pady=5)
- # Start the Tkinter event loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement