-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffcompressGUI.py
More file actions
132 lines (97 loc) · 5.02 KB
/
Copy pathHuffcompressGUI.py
File metadata and controls
132 lines (97 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
Huffcompress GUI Interface
Welcome to Huffcompress, a file compression and decompression tool
designed with a graphical user interface (GUI) using Tkinter.
Huffcompress employs the Huffman algorithm to achieve efficient
and lossless compression of text files, providing a 2:1 compression ratio.
Running the GUI
Before launching the GUI, ensure Python 3 is installed on your system.
To run HuffcompressGUI.py, please follow these steps:
1. Navigate to the directory where the Huffman program files are located using the command line: cd path/to/huffman/program
(or)
Add the Huffman program directory to the system's PATH variable for more convenient access.
2. Then, execute the following command: python HuffcompressGUI.py
"""
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from PIL import ImageTk, Image
from os import stat, path
import platform
from compress_file import HuffFile, CompressionError, COMPRESSED_FILE_EXTENSION
# Create the main window
mainWin = tk.Tk()
mainWin.title("HuffCompress - Lossless Compression Tool")
mainWin.geometry("400x200") # Set the size of the window
mainWin.iconbitmap('./assets/logo.ico') # Set the icon logo of HuffCompress
mainWin.configure()
mainWin.resizable(False, False) # Lock the window size
# Loading our logo and placing it in the main window
logo = ImageTk.PhotoImage(Image.open('./assets/logo.png').resize((260, 180)))
logoContainer = tk.Label(image=logo)
logoContainer.configure(height="100")
logoContainer.pack(side="top", anchor="n") # Position the logo top-left side of the main window
# Styles for buttons
style1 = ttk.Style() # For compress button
style2 = ttk.Style() # For decompress button
style1.configure('W.TButton', font=('Helvetica', 16, 'bold'), foreground='#5dbea3')
style1.map('W.TButton', foreground = [('active', '!disabled', 'green')],
background = [('active', 'black')])
style2.configure('C.TButton', font=('Helvetica', 16, 'bold'), foreground='#4681f4')
style2.map('C.TButton', foreground = [('active', '!disabled', 'blue')],
background = [('active', 'black')])
# Listener for compress button
def compressFile():
# Open a file dailog for the user to select the file to be compressed
target_file = filedialog.askopenfilename(title="Choose a file to compress")
# Displays an error message and exits function when the user does not choose a file to compress
if(target_file == ''):
messagebox.showerror("Error", "No file chosen!")
return
# Size of the file before compression (in bytes)
original_size = stat(target_file)
# Compress the file
hf = HuffFile()
try:
new_path = hf.compress_file(target_file)
except CompressionError as e:
messagebox.showerror("Error", str(e))
return
os_name = platform.system()
# use os.path.join for new_path, path.basename(target_file) + COMPRESSED_FILE_EXTENSION
try:
if os_name == "Darwin":
# Size of the file after compression (in bytes)
compressed_size = stat(new_path + "/" + path.basename(target_file) + COMPRESSED_FILE_EXTENSION)
elif os_name == "Windows":
compressed_size = stat(new_path + "\\" + path.basename(target_file) + COMPRESSED_FILE_EXTENSION)
else:
messagebox.showerror("Error", "Operating System not compatible.")
except Exception as e:
messagebox.showerror("Error", "Unable to locate file: " + e)
return
# Display a compression successful message
messagebox.showinfo('Compression Successful!', f"File size reduced by {round(((original_size.st_size-compressed_size.st_size))/original_size.st_size*100)}%")
# Listener for decompress button
def decompressFile():
# Open a file dailog for the user to select the file to be decompressed
target_file = filedialog.askopenfilename(title="Choose a file to decompress", filetypes=((".huff","*.huff"),))
# Displays an error message and exits function when the user does not choose a file to compress
if(target_file == ''):
messagebox.showerror("Error", "No file chosen!")
return
# Decompress the file
hf = HuffFile()
try:
hf.decompress_file(target_file)
except Exception as e:
messagebox.showerror('Error', "An error occurred during decompression: " + e)
return
# Display a decompression successful message
messagebox.showinfo('Decompression Successful!', f"Your file has been decompressed!")
# Create the buttons for compression & decompression
compressBtn = ttk.Button(mainWin, text="Compress File", width=15, style='W.TButton', command=compressFile)
compressBtn.pack()
decompressBtn = ttk.Button(mainWin, text="Decompress File", width=15, style='C.TButton', command=decompressFile)
decompressBtn.pack()
# Run the Tkinter event loop
mainWin.mainloop()