-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf.java
80 lines (71 loc) · 2.94 KB
/
cf.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
import FileProcess.Compression;
import FileProcess.FileIO;
import FileProcess.OverwriteProtector;
import FileProcess.StopWatch;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Usage: Usage: java cf InputFile OutputFile(optional).
* <p>
* Class for command line interface.
* <p>
* OutputFile Structure:
* <p>
* 1. header: MagicNumber(2 bytes)--fileStructureHeader(Strings of file path ending with STRING_END_SIGN)--HEADER_END_SIGN
* <p>
* 2. continuous FileBlocks or DirectoryBlocks, one for a path in source file.
* <p>
* FileBocks: SINGLE_FILE_MAGIC--StringOfPath--STRING_END_SIGN--CanonicalCodeTable--encodedBits(ending with special EOF)
* <p>
* DirectoryBlocks: DIRECTORY_MAGIC--StringOfPath--STRING_END_SIGN
* <p>
* PS:
* String is stored in file in C style instead of Java style. String = chars + STRING_END_SIGN
*/
public final class cf {
// Command line main application function.
public static void main(String[] args) throws IOException {
StopWatch stopWatch = new StopWatch();
long original_size;
// Handle command line arguments
// set output file
File outputFile;
if (args.length == 1) {
outputFile = new File(FileIO.changeFileExtension(args[0]));
} else if (args.length == 2) {
outputFile = new File(args[1]);
} else {
System.err.println("Usage: java cf <InputFile> [OutputFile](optional)");
System.exit(1);
return;
}
// set input file
File inputFile = new File(args[0]);
if (inputFile.equals(outputFile)) {
System.err.println("InputPath equals outputPath. Program terminated.");
System.exit(1);
}
// Overwrite Protection
OverwriteProtector protector = new OverwriteProtector();
if (outputFile.exists()) {
protector.askUser(outputFile);
}
try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
original_size = Compression.compress(inputFile.getParentFile(), inputFile, os);
}
// Print Summary
long compressed_size = outputFile.length();
double compression_percentage = Math.round((((double) compressed_size / original_size) * 10000)) / 100.0;
System.out.println("Compression finished");
if (original_size == 0) {
System.out.println("Compression ratio: No non-empty file");
} else {
System.out.println("Compression ratio: " + compression_percentage + "%");
}
System.out.println("Time Usage: " + stopWatch.getRunningSeconds() + " seconds");
System.out.println("Original Size: " + original_size / 1000.0 + "KB");
System.out.println("Compressed Size: " + compressed_size / 1000.0 + "KB");
}
}