Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import subprocess
- import ctypes
- import sys
- import tkinter as tk
- from tkinter import filedialog, Listbox, Scrollbar, Entry, Text
- import rarfile # Requires 'rarfile' module: pip install rarfile
- import shutil
- import stat
- def run_as_admin():
- if not ctypes.windll.shell32.IsUserAnAdmin():
- ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
- def browse_vhd():
- file_path = filedialog.askopenfilename(title="Select VHD or RAR File",
- filetypes=(("RAR Files", "*.rar"), ("All Files", "*.*")))
- vhd_entry.delete(0, tk.END)
- vhd_entry.insert(0, file_path)
- if file_path.endswith('.rar'):
- load_rar(file_path)
- def load_rar(file_path):
- """Loads the contents of a RAR file into the listbox."""
- global rar_files
- rar_files = []
- try:
- with rarfile.RarFile(file_path) as rf:
- rar_files = rf.namelist()
- file_list.delete(0, tk.END)
- for file in rar_files:
- file_list.insert(tk.END, file)
- except Exception as e:
- result_field.delete(1.0, tk.END)
- result_field.insert(tk.END, f"Error loading RAR file: {e}")
- def open_file(event):
- """Handles the double-click event to open a selected file from the RAR archive."""
- 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():
- if 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)
- 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}")
- def search_files():
- """Search files in the listbox."""
- search_term = search_entry.get().lower()
- file_list.delete(0, tk.END)
- for file in rar_files:
- if search_term in file.lower():
- file_list.insert(tk.END, 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.")
- def main():
- if ctypes.windll.shell32.IsUserAnAdmin():
- global rar_files # Use a global list to store files from RAR
- # Create the main window
- root = tk.Tk()
- root.title("Najeeb RAR Password File Opener")
- root.configure(bg='#ADD8E6') # Light blue background
- # Create widgets for RAR file selection and mounting/unmounting
- tk.Label(root, text="RAR Password File:", bg='#ADD8E6').grid(row=0, column=0, padx=5, pady=5)
- global vhd_entry
- vhd_entry = Entry(root, width=50)
- vhd_entry.grid(row=0, column=1, padx=5, pady=5)
- browse_button = tk.Button(root, text="Browse", command=browse_vhd, bg='#FF6347', fg='white') # Tomato color button
- browse_button.grid(row=0, column=2, padx=5, pady=5)
- password_label = tk.Label(root, text="RAR Password (if any):", bg='#ADD8E6')
- password_label.grid(row=2, column=0, padx=5, pady=5)
- global password_entry
- password_entry = Entry(root, width=20, show="*")
- password_entry.grid(row=2, column=1, padx=5, pady=5)
- # Create a file listbox with a scrollbar
- global file_list
- file_list = Listbox(root, width=110, height=20)
- file_list.grid(row=3, column=0, columnspan=3, padx=5, pady=5)
- scrollbar = Scrollbar(root)
- scrollbar.grid(row=3, column=3, sticky='ns')
- file_list.config(yscrollcommand=scrollbar.set)
- scrollbar.config(command=file_list.yview)
- # Bind double-click to open the file
- file_list.bind('<Double-Button-1>', open_file)
- # Create a search entry and button
- search_label = tk.Label(root, text="Search Files:", bg='#ADD8E6')
- search_label.grid(row=4, column=0, padx=5, pady=5)
- global search_entry
- search_entry = Entry(root, width=50)
- search_entry.grid(row=4, column=1, padx=5, pady=5)
- search_button = tk.Button(root, text="Search", command=search_files, bg='#32CD32', fg='white') # Lime green button
- search_button.grid(row=4, column=2, padx=5, pady=5)
- # Create a delete button to delete temp folder
- delete_button = tk.Button(root, text="Delete Temp Folder", command=delete_temp_folder, bg='#DC143C', fg='white') # Crimson color button
- delete_button.grid(row=5, column=0, columnspan=3, padx=5, pady=5)
- # Create a result text field
- global result_field
- result_field = Text(root, width=80, height=5)
- result_field.grid(row=6, column=0, columnspan=3, padx=5, pady=5)
- root.mainloop()
- else:
- run_as_admin()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement