Repository navigation
Expand file tree
/
Copy pathFileCompressionProjectGUI.java
More file actions
175 lines (146 loc) · 5.88 KB
/
Copy pathFileCompressionProjectGUI.java
File metadata and controls
175 lines (146 loc) · 5.88 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class FileUtil {
public static String readFile(String filePath) throws IOException {
StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
}
return content.toString();
}
public static void writeFile(String filePath, String content) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
writer.write(content);
}
}
}
class HuffmanCoding {
static HashMap<Character, String> huffmanCodes = new HashMap<>();
public static void buildHuffmanTree(String text) {
HashMap<Character, Integer> frequencies = new HashMap<>();
for (char c : text.toCharArray()) {
frequencies.put(c, frequencies.getOrDefault(c, 0) + 1);
}
PriorityQueue<HuffmanNode> pq = new PriorityQueue<>();
for (char c : frequencies.keySet()) {
pq.offer(new HuffmanNode(c, frequencies.get(c)));
}
while (pq.size() > 1) {
HuffmanNode left = pq.poll();
HuffmanNode right = pq.poll();
HuffmanNode mergedNode = new HuffmanNode('-', left.frequency + right.frequency);
mergedNode.left = left;
mergedNode.right = right;
pq.offer(mergedNode);
}
if (!pq.isEmpty()) {
HuffmanNode root = pq.poll();
generateCodes(root, "");
}
}
public static void generateCodes(HuffmanNode root, String code) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
huffmanCodes.put(root.data, code);
}
generateCodes(root.left, code + "0");
generateCodes(root.right, code + "1");
}
public static String compress(String text) {
StringBuilder compressed = new StringBuilder();
for (char c : text.toCharArray()) {
compressed.append(huffmanCodes.get(c));
}
return compressed.toString();
}
static class HuffmanNode implements Comparable<HuffmanNode> {
int frequency;
char data;
HuffmanNode left, right;
public HuffmanNode(char data, int frequency) {
this.data = data;
this.frequency = frequency;
left = right = null;
}
@Override
public int compareTo(HuffmanNode node) {
return this.frequency - node.frequency;
}
}
}
class RunLengthEncoding {
public static String compress(String text) {
StringBuilder compressed = new StringBuilder();
int count = 1;
for (int i = 1; i < text.length(); i++) {
if (text.charAt(i) == text.charAt(i - 1)) {
count++;
} else {
compressed.append(text.charAt(i - 1)).append(count);
count = 1;
}
}
compressed.append(text.charAt(text.length() - 1)).append(count);
return compressed.toString();
}
}
public class FileCompressionProjectGUI extends JFrame implements ActionListener {
private JButton selectFileButton;
private JTextArea statusTextArea;
public FileCompressionProjectGUI() {
setTitle("File Compression Project");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Center the frame
setLayout(null);
selectFileButton = new JButton("Select File");
selectFileButton.setBounds(20, 20, 120, 30);
selectFileButton.addActionListener(this);
add(selectFileButton);
statusTextArea = new JTextArea();
statusTextArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(statusTextArea);
scrollPane.setBounds(20, 70, 340, 80);
add(scrollPane);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == selectFileButton) {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
String filePath = selectedFile.getAbsolutePath();
try {
// Read file content
String originalText = FileUtil.readFile(filePath);
// Compression algorithms...
String rleCompressed = RunLengthEncoding.compress(originalText);
String rleFilePath = selectedFile.getParent() + "/rle_compressed.txt";
FileUtil.writeFile(rleFilePath, rleCompressed);
HuffmanCoding.buildHuffmanTree(originalText);
String huffmanCompressed = HuffmanCoding.compress(originalText);
String huffmanFilePath = selectedFile.getParent() + "/huffman_compressed.txt";
FileUtil.writeFile(huffmanFilePath, huffmanCompressed);
statusTextArea.setText("Compression complete. Compressed files saved to:\n" +
rleFilePath + "\n" +
huffmanFilePath);
} catch (IOException ex) {
statusTextArea.setText("Error: " + ex.getMessage());
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new FileCompressionProjectGUI());
}
}