|
1 | 1 | package HuffmanCompression; |
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.List; |
3 | 6 | import java.util.Map; |
4 | 7 |
|
5 | | -import static HuffmanCompression.Compressor.encode; |
6 | | -import static HuffmanCompression.HuffmanTree.*; |
7 | | -import static HuffmanCompression.SerializeHuffmanTable.*; |
8 | | - |
9 | 8 |
|
10 | 9 | public class Application { |
| 10 | + private static File file; |
| 11 | + private static File compressedFile; |
11 | 12 |
|
12 | 13 | public static void main(String[] args) { |
13 | 14 |
|
14 | | - String s = "abracadabra dabra dabra dabra and cadabra or shwabra 123 321 222 333 ___ ----###### ___ ////--- AAA BBB CCCC"; |
15 | | - System.out.println("String :"); |
16 | | - System.out.println(s); |
17 | | - Node tree = growTree(s); |
18 | | - |
19 | | - Map<Character, String> tableOfCodes = HuffmanTree.createTable(tree); |
20 | | - |
21 | | - System.out.println("create table from string"); |
22 | | - for (Map.Entry entry : tableOfCodes.entrySet()) { |
23 | | - System.out.println("key = " + entry.getKey() + " Val = " + entry.getValue()); |
| 15 | + String message = null; |
| 16 | + List<String> data = null; |
| 17 | + if (args[0].equals("--compress")) { |
| 18 | + compressedFile = new File(args[1]); |
| 19 | + Compressor compressor = Compressor.instance(); |
| 20 | + try { |
| 21 | + StringBuffer sb = new StringBuffer(); |
| 22 | + data = FileWork.readFile(compressedFile); |
| 23 | + int i = data.size(); |
| 24 | + for (String string : data) { |
| 25 | + if (i==1) { |
| 26 | + sb.append(string); |
| 27 | + break; |
| 28 | + } |
| 29 | + sb.append(string); |
| 30 | + i--; |
| 31 | + } |
| 32 | + message = sb.toString(); |
| 33 | +// System.out.println(message); |
| 34 | + String encoded = compressor.encode(message); |
| 35 | + FileWork.writeData(encoded, compressedFile.getPath(), args[0]); |
| 36 | + } catch (IOException e) { |
| 37 | + e.printStackTrace(); |
| 38 | + } |
24 | 39 | } |
25 | 40 |
|
26 | | - System.out.println("Encoded string"); |
27 | | - String encoded = encode(s,tableOfCodes); |
28 | | - System.out.println(encoded); |
29 | | - |
30 | | - System.out.println("Serialize table "); |
31 | | - String y = serialize(tableOfCodes); |
32 | | - System.out.println(y); |
33 | | - |
34 | | - System.out.println("deserealize table"); |
35 | | - Map<Character,String> deserialize = deserialize(y); |
36 | | - |
37 | | - for (Map.Entry entry : deserialize.entrySet()) { |
38 | | - System.out.println("key = " + entry.getKey() + " Val = " + entry.getValue()); |
| 41 | + if (args[0].equals("--decompress")) { |
| 42 | + file = new File(args[1]); |
| 43 | + Decompressor decompressor = Decompressor.instance(); |
| 44 | + try { |
| 45 | + data = FileWork.readFile(file); |
| 46 | + int i = data.size(); |
| 47 | + String encodedTable = null; |
| 48 | + String encodedData = null; |
| 49 | + StringBuffer sb = new StringBuffer(); |
| 50 | + for (String s : data) { |
| 51 | + if (i == 1) { |
| 52 | + encodedData = s; |
| 53 | + encodedTable = sb.toString(); |
| 54 | + break; |
| 55 | + } |
| 56 | + sb.append(s); |
| 57 | + sb.append("\n"); |
| 58 | + i--; |
| 59 | + } |
| 60 | + System.out.println(encodedData); |
| 61 | + System.out.println(encodedTable); |
| 62 | + Map<Character, String> decodedTable = decompressor.deco0mpressTable(encodedTable); |
| 63 | + String decodedData = decompressor.decode(encodedData, decodedTable); |
| 64 | +// System.out.println(decodedData); |
| 65 | + FileWork.writeData(decodedData, file.getPath(), args[0]); |
| 66 | + } catch (IOException e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
39 | 69 | } |
40 | | - |
41 | | - System.out.println("decoded string"); |
42 | | - String decode = Decompressor.decode(encoded,deserialize); |
43 | | - System.out.println(decode); |
44 | | - |
45 | 70 | } |
46 | 71 | } |
0 commit comments