-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLZWCompressionDecompression.java
More file actions
213 lines (177 loc) · 8.78 KB
/
Copy pathLZWCompressionDecompression.java
File metadata and controls
213 lines (177 loc) · 8.78 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
//APPLICATION OF BOTH DS and File Handling in JAVA
//COMPRESSES TEXT FILES ONLY
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class LZWCompressionDecompression {
// Compresses the input string data using LZW algorithm and returns an ArrayList of integer codes
public ArrayList<Integer> compress(String input) {
HashMap<String, Integer> dictionary = new HashMap<>();
for (int i = 0; i < 256; i++) {
dictionary.put("" + (char) i, i);
}
String currentPattern = "";
ArrayList<Integer> compressedData = new ArrayList<>();
int dictSize = 256;
for (char symbol : input.toCharArray()) {
String patternWithSymbol = currentPattern + symbol;
if (dictionary.containsKey(patternWithSymbol)) {
currentPattern = patternWithSymbol;
} else {
compressedData.add(dictionary.get(currentPattern));
if (dictSize < 4096) {
dictionary.put(patternWithSymbol, dictSize++);
}
currentPattern = "" + symbol;
}
}
if (!currentPattern.isEmpty()) {
compressedData.add(dictionary.get(currentPattern));
}
return compressedData;
}
// Decompresses the ArrayList of integer codes to retrieve the original string data
public String decompress(ArrayList<Integer> compressedData) {
HashMap<Integer, String> dictionary = new HashMap<>();
for (int i = 0; i < 256; i++) {
dictionary.put(i, "" + (char) i);
}
StringBuilder decompressedData = new StringBuilder();
int dictSize = 256;
String previousPattern = "" + (char) (int) compressedData.remove(0);
decompressedData.append(previousPattern);
for (int code : compressedData) {
String currentPattern;
if (dictionary.containsKey(code)) {
currentPattern = dictionary.get(code);
} else if (code == dictSize) {
currentPattern = previousPattern + previousPattern.charAt(0);
} else {
throw new IllegalArgumentException("Invalid compressed code: " + code);
}
decompressedData.append(currentPattern);
if (dictSize < 4096) {
dictionary.put(dictSize++, previousPattern + currentPattern.charAt(0));
}
previousPattern = currentPattern;
}
return decompressedData.toString();
}
// Writes compressed data to a .lzw file
public void writeCompressedFile(ArrayList<Integer> compressedData, String outputFileName) throws IOException {
try (DataOutputStream out = new DataOutputStream(new FileOutputStream(outputFileName))) {
for (int code : compressedData) {
out.writeInt(code);
}
}
}
// Reads compressed data from a .lzw file
public ArrayList<Integer> readCompressedFile(String inputFileName) throws IOException {
ArrayList<Integer> compressedData = new ArrayList<>();
try (DataInputStream in = new DataInputStream(new FileInputStream(inputFileName))) {
while (in.available() > 0) {
compressedData.add(in.readInt());
}
}
return compressedData;
}
// Reads data from a .txt file
public String readFile(String inputFileName) throws IOException {
StringBuilder data = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(inputFileName))) {
String line;
while ((line = reader.readLine()) != null) {
data.append(line).append("\n");
}
}
return data.toString();
}
// Writes decompressed data to a .txt file
public void writeFile(String data, String outputFileName) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName))) {
writer.write(data);
}
}
// Prints the file sizes and compression ratio
public void printCompressionRatio(String originalFilePath, String compressedFilePath) {
File originalFile = new File(originalFilePath);
File compressedFile = new File(compressedFilePath);
long originalSize = originalFile.length();
long compressedSize = compressedFile.length();
double compressionRatio = ((double) compressedSize / originalSize) * 100;
System.out.println("Original File Size: " + originalSize + " bytes");
System.out.println("Compressed File Size: " + compressedSize + " bytes");
System.out.printf("Compression Ratio: %.2f%%\n", compressionRatio);
}
// Prints the file sizes and decompression ratio
public void printDecompressionRatio(String originalFilePath, String decompressedFilePath) {
File originalFile = new File(originalFilePath);
File decompressedFile = new File(decompressedFilePath);
long originalSize = originalFile.length();
long decompressedSize = decompressedFile.length();
double decompressionRatio = ((double) decompressedSize / originalSize) * 100;
System.out.println("Original File Size: " + originalSize + " bytes");
System.out.println("Decompressed File Size: " + decompressedSize + " bytes");
System.out.printf("Decompression Ratio: %.2f%%\n", decompressionRatio);
}
// Main method to handle user input and perform compression/decompression
public void LZWCompression() {
LZWCompressionDecompression lzw = new LZWCompressionDecompression();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the path of the file: ");
String filePath = scanner.nextLine();
System.out.print("Choose operation - Compress (C) or Decompress (D): ");
String choice = scanner.nextLine().toUpperCase();
try {
if (choice.equals("C")) {
// Compression
String inputData = lzw.readFile(filePath);
ArrayList<Integer> compressedData = lzw.compress(inputData);
String compressedFileName = filePath.substring(0, filePath.lastIndexOf('.')) + ".lzw";
lzw.writeCompressedFile(compressedData, compressedFileName);
// Print compression ratio
lzw.printCompressionRatio(filePath, compressedFileName);
System.out.println("File compressed successfully as: " + compressedFileName);
} else if (choice.equals("D")) {
// Decompression
File fileToDecompress = new File(filePath);
if (!fileToDecompress.exists() || !filePath.endsWith(".lzw")) {
throw new FileNotFoundException("The file does not exist or is not a valid .lzw file.");
}
ArrayList<Integer> compressedData = lzw.readCompressedFile(filePath);
String decompressedData = lzw.decompress(compressedData);
String decompressedFileName = filePath.substring(0, filePath.lastIndexOf('.')) + "_decompressed.txt";
lzw.writeFile(decompressedData, decompressedFileName);
// Print decompression ratio
lzw.printDecompressionRatio(filePath, decompressedFileName);
System.out.println("File decompressed successfully as: " + decompressedFileName);
} else {
System.out.println("Invalid choice. Please enter 'C' for compress or 'D' for decompress.");
}
} catch (FileNotFoundException e) {
System.out.println("File operation error: " + e.getMessage());
} catch (IOException e) {
System.out.println("File operation error: " + e.getMessage());
} catch (IllegalArgumentException e) {
System.out.println("Decompression error: " + e.getMessage());
}
// finally {
// scanner.close();
// }
}
}
/*OUTPUT
Enter the path of the file: C:\\Users\\SIMRAN\\Desktop\\417_DBMS-Assignment 2.txt
Choose operation - Compress (C) or Decompress (D): C
Original File Size: 37827 bytes
Compressed File Size: 30708 bytes
Compression Ratio: 81.18%
File compressed successfully as: C:\\Users\\SIMRAN\\Desktop\\417_DBMS-Assignment 2.lzw
Enter the path of the file: C:\\Users\\SIMRAN\\Desktop\\417_DBMS-Assignment 2.lzw
Choose operation - Compress (C) or Decompress (D): D
Original File Size: 30708 bytes
Decompressed File Size: 37005 bytes
Decompression Ratio: 120.51%
File decompressed successfully as: C:\\Users\\SIMRAN\\Desktop\\417_DBMS-Assignment 2_decopressed.txt
*/