Repository navigation
Expand file tree
/
Copy pathFileCompressionDecompressionTool.java
More file actions
137 lines (102 loc) · 4.2 KB
/
Copy pathFileCompressionDecompressionTool.java
File metadata and controls
137 lines (102 loc) · 4.2 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
import java.io.*;
import java.util.Scanner;
class RLECompressor {
public static String compress(String data) {
if (data == null || data.isEmpty()) {
return "";
}
StringBuilder result = new StringBuilder();
int count = 1;
for (int i = 0; i < data.length() - 1; i++) {
if (data.charAt(i) == data.charAt(i + 1)) {
count++;
} else {
result.append(data.charAt(i)).append(count);
count = 1;
}
}
result.append(data.charAt(data.length() - 1)).append(count);
return result.toString();
}
}
class RLEDecompressor {
public static String decompress(String data) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < data.length();) {
char ch = data.charAt(i);
StringBuilder num = new StringBuilder();
i++;
while (i < data.length() && Character.isDigit(data.charAt(i))) {
num.append(data.charAt(i));
i++;
}
int count = Integer.parseInt(num.toString());
for (int j = 0; j < count; j++) {
result.append(ch);
}
}
return result.toString();
}
}
class FileCompressionDecompressionTool {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("\n1. Compress File");
System.out.println("2. Decompress File");
System.out.println("3. Exit");
System.out.print("Enter choice: ");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
try {
System.out.print("Enter input file name: ");
String inFile = sc.nextLine();
System.out.print("Enter output file name: ");
String outFile = sc.nextLine();
BufferedReader reader = new BufferedReader(new FileReader(inFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
String line;
while ((line = reader.readLine()) != null) {
writer.write(RLECompressor.compress(line));
writer.newLine();
}
reader.close();
writer.close();
System.out.println("Compression completed");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
break;
case 2:
try {
System.out.print("Enter compressed file name: ");
String inFile = sc.nextLine();
System.out.print("Enter output file name: ");
String outFile = sc.nextLine();
BufferedReader reader = new BufferedReader(new FileReader(inFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
String line;
while ((line = reader.readLine()) != null) {
writer.write(RLEDecompressor.decompress(line));
writer.newLine();
}
reader.close();
writer.close();
System.out.println("Decompression completed");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
break;
case 3:
System.out.println("Program terminated");
break;
default:
System.out.println("Invalid choice");
}
} while (choice != 3);
sc.close();
}
}