Skip to content

Commit 6018337

Browse files
committed
add some new features
1 parent 29bdc3f commit 6018337

File tree

3 files changed

+66
-15
lines changed

3 files changed

+66
-15
lines changed

src/HuffmanCompression/Application.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public static void main(String[] args) throws UnexpectedFileFormat, UnexpectedOp
4848

4949
Map<Character, String> table = HuffmanTree.createTable(resultData);
5050
byte[] encodedBytes = compressor.encodeToByte(resultData);
51-
5251
byte[] serializeTable = SerializeHuffmanTable.serializeToByteArray(table);
5352
int summarylength = encodedBytes.length + serializeTable.length;
5453
byte[] summaryBytes = new byte[summarylength];
@@ -66,6 +65,13 @@ public static void main(String[] args) throws UnexpectedFileFormat, UnexpectedOp
6665
} else if (option.equals(DECOMPRESS_OPTION)) {
6766
//////
6867
System.out.println("decompress");
68+
69+
List<byte[]> list = FileWork.readCompressedFiles(file);
70+
byte[] tableData = list.get(0);
71+
byte[] dataData = list.get(1);
72+
Map<Character, String> table = SerializeHuffmanTable.deserializeFromByteArray(tableData);
73+
74+
6975
// Decompressor decompressor = Decompressor.instance();
7076
// byte[] bytes = FileWork.readFileBytes(file);
7177
// byte[] tableBytes = FileWork.getTableBytes(bytes);

src/HuffmanCompression/FileWork.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import java.nio.file.Files;
88
import java.nio.file.Path;
99
import java.nio.file.Paths;
10-
import java.util.BitSet;
11-
import java.util.HashMap;
12-
import java.util.List;
13-
import java.util.Map;
10+
import java.util.*;
1411

1512
import static HuffmanCompression.SerializeHuffmanTable.deserializeFromByteArray;
1613
import static HuffmanCompression.SerializeHuffmanTable.serializeToByteArray;
@@ -91,29 +88,36 @@ private static String getFileExtension(File file) {
9188
}
9289
}
9390

91+
private static void writeData(File file, byte[] dataBytes, String extension) throws IOException {
92+
File compressed = new File(file.getAbsolutePath() + extension);
93+
compressed.createNewFile();
94+
Path compressedPath = Paths.get(compressed.getAbsolutePath());
95+
Files.write(compressedPath, dataBytes);
96+
}
97+
98+
private static void writeMeta(File file, byte[] dataBytes) throws IOException {
99+
File compressed = new File(file.getAbsolutePath() + ".meta");
100+
compressed.createNewFile();
101+
Path compressedPath = Paths.get(compressed.getAbsolutePath());
102+
Files.write(compressedPath, dataBytes);
103+
}
104+
94105
public static void writeFile(byte[] data, String path, String arg) throws IOException, UnexpectedFileFormat {
95106
File file = new File(path);
96107

97108
if (arg.equals(COMPRESS_OPTION)) {
98-
toFileWrite(file, data, COMPRESS_EXTENSION);
109+
writeData(file, data, COMPRESS_EXTENSION);
99110
}
100111

101112
if (arg.equals(DECOMPRESS_OPTION)) {
102113
if (!getFileExtension(file).equals(COMPRESS_EXTENSION)) {
103114
throw new UnexpectedFileFormat("File format is unexpected");
104115
}
105-
toFileWrite(file, data, DECOMPRESS_EXTENSION);
116+
writeData(file, data, DECOMPRESS_EXTENSION);
117+
writeMeta(file,data);
106118
}
107119
}
108120

109-
110-
private static void toFileWrite(File file, byte[] dataBytes, String extension) throws IOException {
111-
File compressed = new File(file.getAbsolutePath() + extension);
112-
compressed.createNewFile();
113-
Path compressedPath = Paths.get(compressed.getAbsolutePath());
114-
Files.write(compressedPath, dataBytes);
115-
}
116-
117121
public static List<String> readFileStrings(File file) throws IOException {
118122
List<String> string = Files.readAllLines(Paths.get(file.getAbsolutePath()));
119123
return string;
@@ -123,6 +127,18 @@ public static byte[] readFileBytes(File file) throws IOException {
123127
return Files.readAllBytes(Paths.get(file.getAbsolutePath()));
124128
}
125129

130+
public static List<byte[]> readCompressedFiles(File file) throws UnexpectedFileFormat, IOException {
131+
List<byte[]> list = new ArrayList<>();
132+
File tableFile = new File(file.getAbsolutePath()+".meta");
133+
File dataFile = new File(file.getAbsolutePath());
134+
if (!dataFile.exists() || !tableFile.exists()){
135+
throw new UnexpectedFileFormat("cant find table or meta file");
136+
}
137+
list.add(Files.readAllBytes(Paths.get(tableFile.getAbsolutePath())));
138+
list.add(Files.readAllBytes(Paths.get(dataFile.getAbsolutePath())));
139+
return list;
140+
}
141+
126142
public static byte[] getTableBytes(byte[] data) {
127143
byte[] tableLength = new byte[4];
128144
System.arraycopy(data, 0, tableLength, 0, tableLength.length);
@@ -208,4 +224,5 @@ public int getLength() {
208224
}
209225

210226

227+
211228
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package HuffmanCompression;
2+
3+
import java.math.BigInteger;
4+
5+
public class StringWork {
6+
public static void main(String[] args) {
7+
byte[] bytes = new byte[]{1, 2, 3};
8+
9+
10+
byte b = 1;
11+
StringBuilder sb = new StringBuilder();
12+
for (int i = 0; i <bytes.length; i++) {
13+
b = bytes[i];
14+
15+
for (int j = 7; j >= 0; --j) {
16+
sb.append(b >>> j & 1);
17+
}
18+
}
19+
System.out.println(sb.toString());
20+
21+
22+
// // Create a BigInteger using the byte array
23+
// BigInteger bi = new BigInteger(bytes);
24+
//
25+
// String s = bi.toString(2);
26+
// System.out.println(s);
27+
}
28+
}

0 commit comments

Comments
 (0)