-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompress.java
More file actions
427 lines (229 loc) · 9.33 KB
/
Copy pathCompress.java
File metadata and controls
427 lines (229 loc) · 9.33 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import java.io.*;
import java.util.*;
//文件压缩
public class Compress {
public static void main(String[] args) {
String[] zipfiles = {
"src/files/A Study In Scarlet.txt",
"src/files/四签名.txt",
"src/files/最后一案.txt",
"src/images/奥拉夫.png",
"src/images/bizzarStore.png",
"src/videos/Steam.mp4",
"src/videos/sxbank.mp4",
"src/music/看月亮.mp3",
"src/music/Fish in the pool.mp3"
};
String[] dstfiles = {
"src/files/A Study In Scarlet.zip",
"src/files/四签名.zip",
"src/files/最后一案.zip",
"src/images/奥拉夫.zip",
"src/images/bizzarStore.zip",
"src/videos/Steam.zip",
"src/videos/sxbank.zip",
"src/music/看月亮.zip",
"src/music/Fish in the pool.zip"
};
for(int i = 0; i < zipfiles.length; i++) {
System.out.println("压缩文本文件中...");
zipFile(zipfiles[i], dstfiles[i]);
}
}
static Map<Byte, String> huffmanCodes = new HashMap<Byte, String>();// 哈夫曼编码表
static Node huffmantree; // 哈夫曼树的根
static int endLen;// 记录最后一个字节的二进制串的长度
/**
*
* @param srcFile 解压文件源路径
* @param dstFile 解压后编码文件路径
*/
public static void zipFile(String srcFile, String dstFile) {
ObjectOutputStream oos = null;
FileInputStream is = null;
try {
is = new FileInputStream(srcFile);
byte[] b = new byte[is.available()];
is.read(b);
// for (int i = 0; i < b.length; i++) {
// System.out.print(b[i]+" ");
// }
byte[] huffmanBytes = huffmanZip(b);
// for (int i = 0; i < huffmanBytes.length; i++) {
// System.out.println(huffmanBytes[i]);
// }
oos = new ObjectOutputStream(new FileOutputStream(dstFile));
// 对象序列化
oos.writeObject(huffmanBytes);// 将编码后的字节数组存入文件
oos.writeObject(huffmanCodes);// 将哈夫曼表也存入文件
oos.writeObject(endLen);// 最后一个要处理的字节单位长度(可能不足八位)
System.out.println("压缩成功!");
System.out.println("压缩比为:");
System.out.println((double) huffmanBytes.length / b.length); // 定义为哈夫曼编码表的长度/字符构成字节数组长度
// System.out.println("WPL为: ");
// System.out.println(Wpl(huffmantree));
// System.out.println("哈弗曼编码表为:");
// for (Map.Entry<Byte, String> entry : huffmanCodes.entrySet()) {
// System.out.println(entry.getKey() + ":" + entry.getValue());
// }
System.out.println();
// System.out.println("哈弗曼编码为:");
// int count = 0;
// for (byte a : b) {
// System.out.print(huffmanCodes.get(a));
// count++;
// while (count > 100) {
// System.out.println();
// count = 0;// 使打印出来的编码更加的立体
// }
// }
// System.out.println();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
is.close();
oos.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/**
* 哈夫曼编码压缩
* @param bytes 读入的文件中字符的ASCII码构成的字节数组
* @return 压缩后的字节数组
*/
static byte[] huffmanZip(byte[] bytes) {
List<Node> nodes = getNodes(bytes);
// 哈夫曼树
huffmantree = createHuffmanTree(nodes);
// 哈夫曼编码表
Map<Byte, String> huffmanCodes = getCodes(huffmantree);
byte[] zip = zip(bytes, huffmanCodes);
return zip;
}
/**
*
* @param bytes 字符构成的字节数组
* @param huffmanCodes 哈夫曼编码表
* @return 压缩后的字节数组
*/
static byte[] zip(byte[] bytes, Map<Byte, String> huffmanCodes) {
StringBuilder stringBuilder = new StringBuilder();
for (byte b : bytes) {
stringBuilder.append(huffmanCodes.get(b)); // 获得由所有字符组成的哈夫曼编码
}
int len;
// 获取新的字节数组长度
if (stringBuilder.length() % 8 == 0) {// 如果编码长度是八的倍数
len = stringBuilder.length() / 8;// 新字节数组的长度为哈夫曼编码长度/8
} else {
len = stringBuilder.length() / 8 + 1;// 如果不是则最后一组字节算一个字节数组
}
endLen = stringBuilder.length() % 8;
byte[] by = new byte[len];
int index = 0;
// 以8作为一个字节单位处理
for (int i = 0; i < stringBuilder.length(); i += 8) {
String strByte;
if (i + 8 > stringBuilder.length()) {// 如果到了最后一组编码不足8位
strByte = stringBuilder.substring(i);// 截取剩下的编码
by[index] = (byte) Integer.parseInt(strByte, 2);// 将二进制的strByte字符串转化为十进制的字节
index++;
} else {// 如果还没有到最后一组,则每8个一组
strByte = stringBuilder.substring(i, i + 8);
by[index] = (byte) Integer.parseInt(strByte, 2);
index++;
}
}
return by;
}
/**
*
* @param node 哈夫曼树存储的结点
* @param code 结点新加的编码 取值为0或1
* @param stringBuilder 结点之前存储的编码值
*/
static void getCodes(Node node, String code, StringBuilder stringBuilder) {
StringBuilder builder = new StringBuilder(stringBuilder);
builder.append(code);
if (node != null) {
if (node.data == null) { // 如果不是叶子节点
getCodes(node.left, "0", builder);
getCodes(node.right, "1", builder);
} else {
huffmanCodes.put(node.data, builder.toString()); // 将编码存入哈夫曼编码表
}
}
}
/**
*
* @param root 哈夫曼树的根节点
* @return 哈夫曼编码表
*/
static Map<Byte, String> getCodes(Node root) {
StringBuilder stringBuilder = new StringBuilder(); // 存储编码
if (root == null) {
return null;
}
getCodes(root.left, "0", stringBuilder); // 向左进行编码
getCodes(root.right, "1", stringBuilder); // 向右进行编码
return huffmanCodes;
}
/**
* 生成哈夫曼树
* @param nodes 文件字符构成的结点序列
* @return 哈夫曼树的根节点
*/
static Node createHuffmanTree(List<Node> nodes) {
while (nodes.size() > 1) {
Collections.sort(nodes);
// 取权重最小的两棵树
Node leftNode = nodes.get(0);
Node rightNode = nodes.get(1);
// 加入新树,移除旧树
Node parent = new Node(null, leftNode.weight + rightNode.weight);
parent.left = leftNode;
parent.right = rightNode;
nodes.remove(leftNode);
nodes.remove(rightNode);
nodes.add(parent);
}
return nodes.get(0);
}
/**
* 接收字节数组
* @param bytes 读入的文件中字符的ASCII码构成的字节数组
* @return key为ASCII码,value为出现次数的结点构成的List<Node>集合
*/
static List<Node> getNodes(byte[] bytes) {
List<Node> nodes = new ArrayList<>();
Map<Byte, Integer> counts = new HashMap<>();
for (byte b : bytes) {
Integer count = counts.get(b);
if (count == null) {// 字符第一次出现
counts.put(b, 1);
} else {// 字符重复
counts.put(b, count + 1);// 对应原来的值加一
}
}
// 遍历map
for (Map.Entry<Byte, Integer> entry : counts.entrySet()) {
nodes.add(new Node(entry.getKey(), entry.getValue()));// 将键值对加到node数组中
}
return nodes;
}
static int Wpl(Node root) {
int wpl = 0;
if (root != null) {
if (root.left != null && root.right != null) {
wpl += root.weight;
}
// 递归遍历其左右子树
Wpl(root.left);
Wpl(root.right);
}
return wpl;
}
}