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
| import java.io.*; import java.util.*;
public class HuffmanCodeDemo { public static void main(String[] args) {
String srcFile = "/Users/yons/Downloads/HuffmanCodeDemo.java"; String dstFile = "/Users/yons/Downloads/临时/HuffmanCodeDemo.zip"; String dstFile2 = "/Users/yons/Downloads/临时/HuffmanCodeDemo2.java"; zipFile(srcFile, dstFile);
unZipFile(dstFile, dstFile2); }
private static void unZipFile(String zipFile, String dstFile) { try ( FileInputStream is = new FileInputStream(zipFile); ObjectInputStream ois = new ObjectInputStream(is) ) { byte[] huffmanBytes = (byte[]) ois.readObject(); Map<Byte, String> huffmanCodes = (Map<Byte, String>) ois.readObject();
byte[] decode = decode(huffmanCodes, huffmanBytes); FileOutputStream os = new FileOutputStream(dstFile); os.write(decode); } catch (Exception e) { e.printStackTrace(); }
}
private static void zipFile(String srcFile, String dstFile) { try ( FileInputStream is = new FileInputStream(srcFile); FileOutputStream os = new FileOutputStream(dstFile); ObjectOutputStream oos = new ObjectOutputStream(os) ) { byte[] b = new byte[is.available()]; is.read(b); byte[] huffmanBytes = huffmanZip(b); oos.writeObject(huffmanBytes); oos.writeObject(huffmanCodes); } catch (IOException e) { e.printStackTrace(); } }
private static String byteToBitString(boolean flag, byte b) { int temp = b; if (flag) { temp |= 256; } String string = Integer.toBinaryString(temp); if (flag) { return string.substring(string.length() - 8); } else { return string; } }
private static byte[] decode(Map<Byte, String> huffmanCodes, byte[] huffmanBytes) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < huffmanBytes.length; i++) { byte b = huffmanBytes[i]; boolean flag = (i == huffmanBytes.length - 1); String string = byteToBitString(!flag, b); stringBuilder.append(string); } Map<String, Byte> map = new HashMap<>(); for (Map.Entry<Byte, String> entry : huffmanCodes.entrySet()) { map.put(entry.getValue(), entry.getKey()); }
List<Byte> list = new ArrayList<>(); for (int s = 0; s < stringBuilder.length(); ) { int count = 1; boolean flag = true; Byte b = null;
while (flag) { String key = stringBuilder.substring(s, s + count); b = map.get(key); if (b == null) { count++; } else { flag = false; } } list.add(b); s += count; } byte[] bytes = new byte[list.size()]; for (int i = 0; i < bytes.length; i++) { bytes[i] = list.get(i); } return bytes; }
private static byte[] huffmanZip(byte[] bytes) { List<Node2> nodes = getNodes(bytes); Node2 hUffmanTree = createHUffmanTree(nodes); Map<Byte, String> codes = getCodes(hUffmanTree); return zip(bytes, codes); }
private static void perOrder(Node2 root) { if (root != null) { root.preOrder(); } }
private static byte[] zip(byte[] bytes, Map<Byte, String> huffmanCodes) { StringBuilder stringBuilder = new StringBuilder(); for (byte aByte : bytes) { stringBuilder.append(huffmanCodes.get(aByte)); }
int len; if (stringBuilder.length() % 8 == 0) { len = stringBuilder.length() / 8; } else { len = stringBuilder.length() / 8 + 1; } byte[] huffmanCodeBytes = new byte[len]; int index = 0; for (int i = 0; i < stringBuilder.length(); i += 8) { String strByte; if (i + 8 > stringBuilder.length()) { strByte = stringBuilder.substring(i); } else { strByte = stringBuilder.substring(i, i + 8); }
huffmanCodeBytes[index] = (byte) Integer.parseInt(strByte, 2); index++; } return huffmanCodeBytes; }
static Map<Byte, String> huffmanCodes = new HashMap<>(); static StringBuilder stringBuilder = new StringBuilder();
private static Map<Byte, String> getCodes(Node2 root) { if (root == null) { return null; } getCodes(root.left, "0", stringBuilder); getCodes(root.right, "1", stringBuilder); return huffmanCodes; }
private static void getCodes(Node2 node, String code, StringBuilder stringBuilder) { StringBuilder stringBuilder1 = new StringBuilder(stringBuilder); stringBuilder1.append(code); if (node != null) { if (node.data == null) { getCodes(node.left, "0", stringBuilder1); getCodes(node.right, "1", stringBuilder1); } else { huffmanCodes.put(node.data, stringBuilder1.toString()); } }
}
private static List<Node2> getNodes(byte[] bytes) { ArrayList<Node2> node2s = new ArrayList<>();
Map<Byte, Integer> count = new HashMap<>(); for (byte aByte : bytes) { Integer integer = count.get(aByte); if (integer == null) { count.put(aByte, 1); } else { count.put(aByte, integer + 1); } } for (Map.Entry<Byte, Integer> entry : count.entrySet()) { node2s.add(new Node2(entry.getKey(), entry.getValue())); } return node2s; }
private static Node2 createHUffmanTree(List<Node2> node2s) { while (node2s.size() > 1) { Collections.sort(node2s); Node2 leftNode = node2s.get(0); Node2 rightNode = node2s.get(1); Node2 parent = new Node2(null, leftNode.weight + rightNode.weight); parent.left = leftNode; parent.right = rightNode; node2s.remove(leftNode); node2s.remove(rightNode); node2s.add(parent); } return node2s.get(0);
}
}
class Node2 implements Comparable<Node2> { Byte data; int weight; Node2 left; Node2 right;
public Node2(Byte data, int weight) { this.data = data; this.weight = weight; }
@Override public int compareTo(Node2 o) { return this.weight - o.weight; }
@Override public String toString() { return "Node2{" + "data=" + data + ", weight=" + weight + '}'; }
public void preOrder() { System.out.println(this); if (this.left != null) { this.left.preOrder(); } if (this.right != null) { this.right.preOrder(); } } }
|