-
Notifications
You must be signed in to change notification settings - Fork 2
/
Compress.java
142 lines (124 loc) · 3.78 KB
/
Compress.java
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
/**
* Huffman Tree Class
* Written By: Mitchell Pomery (21130887)
* Used to create mappings for use in huffman coding
**/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Compress
{
/**
* main method for the Compress program
* @param args the arguments from the command line
**/
public static void main(String [] args) {
//Timing
long startTime = System.nanoTime();
if (args.length == 3) {
if (args[0].equals("-c")) {
doCompress(args[1], args[2]);
}
else if (args[0].equals("-d")) {
doDecompress(args[1], args[2]);
}
else {
usage();
}
}
else {
usage();
}
//More Timing
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println("Time Taken: " + (duration/Math.pow(10,9)) + "s");
}
/**
* Prints the usage for the Compress program
**/
private static void usage() {
System.out.println();
System.out.println("Usage: java Compress -c input output [-t]");
System.out.println("OR");
System.out.println("Usage: java Compress -d input output [-t]");
System.out.println();
System.out.println("Options:");
System.out.println("\t-c\tCompress input file into output file");
System.out.println("\t-d\tDecompress input file into output file");
System.out.println();
}
/**
* Compress an input stream into an output stream
* @param cin the input stream to compress
* @param cout the output stream to write the compressed data to
**/
private static void doCompress(String cin, String cout) {
File f1 = new File(cin);
boolean in_exists = f1.exists();
File f2 = new File(cout);
boolean out_exists = f2.exists();
if (!in_exists)
System.out.println("Compression Input file does not exist.");
else if (out_exists)
System.out.println("Compression Output file already exists.");
else {
try {
//The only way to reset the input stream to the start seems to
//be to coles and reopen it
FileInputStream fis1 = new FileInputStream(cin);
FileInputStream fis2 = new FileInputStream(cin);
FileOutputStream fos = new FileOutputStream(cout);
///TODO Compress
Huffman hc = new Huffman();
hc.compress(fis1, fis2, fos);
fis1.close();
fis2.close();
fos.close();
} catch (FileNotFoundException ex) {
System.out.println("Error finding one of the files.");
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println("IO Exception");
System.out.println(ex.getMessage());
}
}
}
/**
* Decompress an already compressed input stream into an output stream
* @param cin the input stream to decompress
* @param cout the output stream to write the decompressed data to
**/
private static void doDecompress(String din, String dout) {
///TODO impliment Decompress starter
File f1 = new File(din);
boolean in_exists = f1.exists();
File f2 = new File(dout);
boolean out_exists = f2.exists();
if (!in_exists)
System.out.println("Decompression Input file does not exist.");
else if (out_exists)
System.out.println("Decompression Output file already exists.");
else {
try {
//The only way to reset the input stream to the start seems to
//be to coles and reopen it
FileInputStream fis = new FileInputStream(din);
FileOutputStream fos = new FileOutputStream(dout);
///TODO Compress
Huffman hc = new Huffman();
hc.decompress(fis, fos);
fis.close();
fos.close();
} catch (FileNotFoundException ex) {
System.out.println("Error finding one of the files.");
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println("IO Exception");
System.out.println(ex.getMessage());
}
}
}
}