-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquash.java
More file actions
106 lines (101 loc) · 3.02 KB
/
Squash.java
File metadata and controls
106 lines (101 loc) · 3.02 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
import compressor.engine.FileReader;
import compressor.engine.QueueBuilder;
import compressor.engine.TreeBuilder;
import compressor.engine.TreeNodeTuple;
import compressor.iterator.List;
import compressor.std_target_output.SquashFileWriter;
import decompressor.engine.SquashReader;
import java.io.DataOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.PriorityQueue;
public class Squash {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println(
"""
Enter the following command to compress
./squash -compress ~\\folder-path target-name
To decompress
./squash -decompress ~\\target-name.tar.sq
"""
);
return;
}
if (args[0].equals("-compress")) {
if (args.length != 3) {
System.out.println(
"""
Enter the following command to compress
./squash -compress ~\\folder-path target-name
"""
);
return;
}
File file = new File(args[1]);
if (!file.exists()) {
System.err.printf(
"%s not found, make sure the path is valid\n",
args[1]
);
}
try {
String TARGET_FILE = args[2] + ".tar.sq";
File targetFile = new File(TARGET_FILE);
targetFile.createNewFile();
SquashFileWriter sqfw = new SquashFileWriter(targetFile, TARGET_FILE);
sqfw.commit_header();
String base_dir;
ArrayList<File> files = new List(
file,
base_dir = args[1] + "\\"
).getFiles();
Map<Byte, String> embeddings = new TreeBuilder(
new QueueBuilder(files).getQueue()
).generateEmbeddings();
int map_size = embeddings.size();
DataOutputStream dos = sqfw.getWriter();
dos.writeInt(map_size);
for (Map.Entry<Byte, String> em : embeddings.entrySet()) {
dos.writeByte(em.getKey());
dos.writeUTF(em.getValue());
}
new FileReader(
files,
base_dir = args[1] + "\\",
dos,
embeddings
).mapEmbeddingsAndWriteToSquash();
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else if (args[0].equals("-decompress")) {
if (args.length != 2) {
System.out.println(
"""
Enter the following command to compress
./squash -decompress ~\\target-name.tar.sq
"""
);
return;
}
File sqFile = new File(args[1]);
if (!sqFile.exists()) {
System.err.printf("Squash file %s does not exist", args[1]);
System.exit(1);
}
try {
String[] folderTokens = args[1].split("[.]");
new SquashReader(sqFile).readAndWriteFile(
"unsquashed_" +
(folderTokens[folderTokens.length - 3]).replaceAll("/", "")
);
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
System.exit(1);
}
}
}
}