-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
254 lines (213 loc) · 7.81 KB
/
Copy pathMain.java
File metadata and controls
254 lines (213 loc) · 7.81 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package prog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main extends JFrame implements ActionListener {
// Definition of global values and items that are part of the GUI.
static public File opened_file, other_file;
static long past, future;
static JLabel redLabel, blueLabel, redScore, blueScore;
static JPanel buttonPanel, titlePanel, scorePanel;
static JButton ZH, UH, ZL, UL, EX;
public JPanel createContentPane() {
// We create a bottom JPanel to place everything on.
JPanel totalGUI = new JPanel();
totalGUI.setLayout(null);
titlePanel = new JPanel();
titlePanel.setLayout(null);
titlePanel.setLocation(90, 20);
titlePanel.setSize(170, 70);
totalGUI.add(titlePanel);
redLabel = new JLabel("Selected File Size: ");
redLabel.setLocation(43, 0);
redLabel.setSize(150, 30);
redLabel.setHorizontalAlignment(0);
titlePanel.add(redLabel);
blueLabel = new JLabel("After zip/unzip the file size: ");
blueLabel.setLocation(10, 30);
blueLabel.setSize(170, 30);
blueLabel.setHorizontalAlignment(0);
titlePanel.add(blueLabel);
scorePanel = new JPanel();
scorePanel.setLayout(null);
scorePanel.setLocation(270, 20);
scorePanel.setSize(120, 60);
totalGUI.add(scorePanel);
redScore = new JLabel("");
redScore.setLocation(0, 0);
redScore.setSize(100, 30);
redScore.setHorizontalAlignment(0);
scorePanel.add(redScore);
blueScore = new JLabel("");
blueScore.setLocation(0, 30);
blueScore.setSize(100, 30);
blueScore.setHorizontalAlignment(0);
scorePanel.add(blueScore);
buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(10, 130);
buttonPanel.setSize(5200, 150);
totalGUI.add(buttonPanel);
ZH = new JButton("ZIP HuffZ");
ZH.setLocation(0, 0);
ZH.setSize(120, 30);
ZH.addActionListener(this);
buttonPanel.add(ZH);
UH = new JButton("UNZIP HuffZ");
UH.setLocation(130, 0);
UH.setSize(120, 30);
UH.addActionListener(this);
buttonPanel.add(UH);
ZL = new JButton("ZIP LmZWp");
ZL.setLocation(260, 0);
ZL.setSize(120, 30);
ZL.addActionListener(this);
buttonPanel.add(ZL);
UL = new JButton("UNZIP LmZWp");
UL.setLocation(390, 0);
UL.setSize(120, 30);
UL.addActionListener(this);
buttonPanel.add(UL);
EX = new JButton("EXIT");
EX.setLocation(130, 70);
EX.setSize(250, 30);
EX.addActionListener(this);
buttonPanel.add(EX);
totalGUI.setOpaque(true);
return totalGUI;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ZH) {
Hzipping.beginHzipping(opened_file.getPath());
JOptionPane.showMessageDialog(null, "..........................Zipping Finished..........................",
"Status", JOptionPane.PLAIN_MESSAGE);
redScore.setText(opened_file.length() + "Bytes");
other_file = new File(opened_file.getPath() + ".huffz");
future = other_file.length();
blueScore.setText(future + "Bytes");
} else if (e.getSource() == UH) {
Hunzipping.beginHunzipping(opened_file.getPath());
JOptionPane.showMessageDialog(null,
"..........................UnZipping Finished..........................", "Status",
JOptionPane.PLAIN_MESSAGE);
redScore.setText(opened_file.length() + "Bytes");
String s = opened_file.getPath();
s = s.substring(0, s.length() - 6);
other_file = new File(s);
future = other_file.length();
blueScore.setText(future + "Bytes");
} else if (e.getSource() == ZL) {
Lzipping.beginLzipping(opened_file.getPath());
JOptionPane.showMessageDialog(null, "..........................Zipping Finished..........................",
"Status", JOptionPane.PLAIN_MESSAGE);
redScore.setText(opened_file.length() + "Bytes");
other_file = new File(opened_file.getPath() + ".LmZWp");
future = other_file.length();
blueScore.setText(future + "Bytes");
} else if (e.getSource() == UL) {
Lunzipping.beginLunzipping(opened_file.getPath());
JOptionPane.showMessageDialog(null,
"..........................UnZipping Finished..........................", "Status",
JOptionPane.PLAIN_MESSAGE);
redScore.setText(opened_file.length() + "Bytes");
String s = opened_file.getPath();
s = s.substring(0, s.length() - 6);
other_file = new File(s);
future = other_file.length();
blueScore.setText(future + "Bytes");
} else if (e.getSource() == EX) {
System.exit(0);
}
}
private static void createAndShowGUI() {
// JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("File Compresser");
// Create and set up the content pane.
Main demo = new Main();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(350, 170, 550, 300);
frame.setVisible(true);
JMenu fileMenu = new JMenu("File");
JMenuBar bar = new JMenuBar();
frame.setJMenuBar(bar);
bar.add(fileMenu);
JMenuItem openItem = new JMenuItem("Open");
fileMenu.add(openItem);
openItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = fileChooser.showOpenDialog(null);
opened_file = fileChooser.getSelectedFile();
past = opened_file.length();
redScore.setText(past + "Bytes");
blueScore.setText("NotYetCalculated");
}
});
JMenuItem exitItem = new JMenuItem("Exit");
fileMenu.add(exitItem);
exitItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
JMenu helpMenu = new JMenu("Help");
frame.setJMenuBar(bar);
bar.add(helpMenu);
JMenuItem helpItem = new JMenuItem("How To");
helpMenu.add(helpItem);
helpItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null,
"Two algorithms are used for file compression" + "\n" + "1. Huffmans Codes" + "\n"
+ "2. Lampel-Ziv-Welch" + "\n"
+ "To compress a file first open the file then click" + "\n"
+ "ZIP HuffZ to zip the file using Huffmans algo.A zipped" + "\n"
+ "file with extension .huffz will be created in the same" + "\n"
+ "folder. This is the zipped file. During unzipping just open" + "\n"
+ "this file and click UNZIP HuffZ button." + "\n" + "\n"
+ "Same will happen for LZW. Zipped file's extension will be .LmZWp" + "\n"
+ "for LZW. Always make sure that during unzipping you must use" + "\n"
+ "the same algorithm that you used for zipping ",
"How To...", JOptionPane.PLAIN_MESSAGE);
}
});
JMenuItem aboutItem = new JMenuItem("About");
helpMenu.add(aboutItem);
aboutItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null,
"File Compresser is a software to zip and unzip files" + "\n"
+ "It is developed on JAVA, as a term project of" + "\n"
+ " Level - 2, Term - 1,Dept. of CSE,BUET." + "\n"
+ "It is completed by Nahiyan Kamal (St. ID 0805006) under" + "\n"
+ "the supervision of Jesun Shahariar of Dept. of CSE, BUET.",
"About", JOptionPane.PLAIN_MESSAGE);
}
});
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}