壓縮過程:
前面已經(jīng)寫過一篇哈夫曼壓縮,lzw字典壓縮與哈夫曼壓縮的不同之處在于不需要把編碼寫入文件,編碼表是在讀文件中生成的,首先將0-255個ascll碼與對應(yīng)的數(shù)字存入哈希表中,作為基礎(chǔ)碼表。
這里的后綴為當(dāng)前
前綴+后綴 如果在碼表中存在,前綴等于前綴+后綴。如果不存在,將前綴+后綴所表示的字符串寫入編碼表編碼,同時將前綴寫入壓縮文件中。這里重點注意一下,一個字節(jié)所能表示的數(shù)字范圍為0-255,所以我們將一個字符的編碼變成兩個字節(jié)寫進(jìn)去,分別寫入它的高八位和低八位,比如256即為00000001 11111111 這里用到dataoutputstream dos對象中的 dos.writechar(256)方法。
兩個字節(jié)所能表示的范圍為0-65535。當(dāng)我們的編碼超過這份范圍,就需要重置編碼表,再重新編碼。
解壓過程
cw表示讀取的到的字符,pw為上一行的cw,cw再編碼表中存在:p→pw,c→cw的第一個字符,輸出cw。cw在編碼表中不存在,p→pw,c→pw的第一字符輸出p+c。
當(dāng)我們讀到65535的時候,就重置碼表,重新編碼。
代碼部分
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
|
public class yasuo { private int bianma = 256 ; // 編碼 private string perfix = "" ; // 前綴 private string suffix = "" ; // 后綴 private string zhongjian = "" ; // 中間變量 hashmap<string, integer> hm = new hashmap<string, integer>(); // 編碼表 private static string path = "d:\\java\\字典壓縮\\zidianyasuo.txt" ; // 要壓縮的文件 private static string path2 = "d:\\java\\字典壓縮\\yasuo.txt" ; // 解壓后的文件 private static string path3 = "d:\\java\\字典壓縮\\jieya.txt" ; // 解壓后的文件 public static void main(string[] args) throws ioexception { /** * 壓縮 */ yasuo yasuo = new yasuo(); yasuo.yasuo(); /** * 解壓 */ jieya jie = new jieya(); jie.jieya(path2,path3); } public void yasuo() throws ioexception { // 創(chuàng)建文件輸入流 inputstream is = new fileinputstream(path); byte [] buffer = new byte [is.available()]; // 創(chuàng)建緩存區(qū)域 is.read(buffer); // 讀入所有的文件字節(jié) string str = new string(buffer); // 對字節(jié)進(jìn)行處理 is.close(); // 關(guān)閉流 // 創(chuàng)建文件輸出流 outputstream os = new fileoutputstream(path2); dataoutputstream dos = new dataoutputstream(os); // system.out.println(str); // 把最基本的256個ascll碼放編碼表中 for ( int i = 0 ; i < 256 ; i++) { char ch = ( char ) i; string st = ch + "" ; hm.put(st, i); } for ( int i = 0 ; i < str.length(); i++) { if (bianma== 65535 ){ system.out.println( "重置" ); dos.writechar( 65535 ); //寫出一個-1作為重置的表示與碼表的打印 hm.clear(); //清空hashmap for ( int j = 0 ; j < 256 ; j++) { //重新將基本256個編碼寫入 char ch = ( char ) j; string st = ch + "" ; hm.put(st, j); } perfix= "" ; bianma= 0 ; } char ch = str.charat(i); string s = ch + "" ; suffix = s; zhongjian = perfix + suffix; if (hm.get(zhongjian) == null ) { // 如果碼表中沒有 前綴加后綴的碼表 // system.out.print(zhongjian); // system.out.println(" 對應(yīng)的編碼為 " + bianma); hm.put(zhongjian, bianma); // 向碼表添加 前綴加后綴 和 對應(yīng)的編碼 // system.out.println(" " + perfix); // system.out.println("寫入的編碼 "+hm.get(perfix)); dos.writechar(hm.get(perfix)); // 把前綴寫入壓縮文件 bianma++; perfix = suffix; } else { // 如果有下一個前綴保存 上一個前綴加后綴 perfix = zhongjian; } if (i == str.length() - 1 ) { // 把最后一個寫進(jìn)去 // system.out.print("寫入最后一個"+perfix); dos.writechar(hm.get(perfix)); // system.out.println(" "+hm.get(perfix)); } } os.close(); // 關(guān)閉流 // system.out.println(hm.tostring());// 輸出碼表 } } |
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
|
public class jieya { private arraylist<integer> list = new arraylist<integer>(); // 存高八位 private int count = 0 ; // 下標(biāo) private arraylist<integer> numlist = new arraylist<>(); // 存編碼 hashmap<string, integer> hm = new hashmap<>(); // 編碼表 hashmap<integer, string> hm1 = new hashmap<>(); // 編碼表 private string cw = "" ; private string pw = "" ; private string p = "" ; private string c = "" ; private int bianma = 256 ; public void jieya(string path, string path1) throws ioexception { // 讀取壓縮文件 inputstream is = new fileinputstream(path); byte [] buffer = new byte [is.available()]; is.read(buffer); is.close(); // 關(guān)閉流 string str = new string(buffer); // system.out.println(str); // 讀高八位 把高八位所表示的數(shù)字放入list中 for ( int i = 0 ; i < buffer.length; i += 2 ) { int a = buffer[i]; list.add(a); // 高八位存入list列表中 } for ( int i = 1 ; i < buffer.length; i += 2 ) { // 讀低八位 // system.out.println(list.get(count)+"---"); if (buffer[i] == - 1 && buffer[i - 1 ] == - 1 ) { numlist.add( 65535 ); } else { // system.out.println(i); if (list.get(count) > 0 ) { // 如果低八位對應(yīng)的高八位為1 if (buffer[i] < 0 ) { int a = buffer[i] + 256 + 256 * list.get(count); // buffer[i]+=256+256*list.get(count); numlist.add(a); // 存入numlist中 } else { int a = buffer[i] + 256 * (list.get(count)); // system.out.println(buffer[i]+" "+a + "+++"); numlist.add(a); // 存入numlist中 } } else { // 高八位為0 // system.out.println(buffer[i]); numlist.add(( int ) buffer[i]); // 存入numlist中 } count++; } } // system.out.println(list.size()+" "+count+" "+numlist.size()+"比較大小"+" // "+buffer.length); // for(int i=0;i<numlist.size();i++){ // system.out.println(numlist.get(i)+"p"); // } /** * 把0-255位字符編碼 */ for ( int i = 0 ; i < 256 ; i++) { char ch = ( char ) i; string st = ch + "" ; hm.put(st, i); hm1.put(i, st); } /** * 根據(jù)numlist隊列中的元素開始重新編碼,輸出文件 */ // 創(chuàng)建輸出流 outputstream os = new fileoutputstream(path1); // 遍歷numlist for ( int i = 0 ; i < numlist.size(); i++) { int n = numlist.get(i); if (hm.containsvalue(n) == true ) { // 如果編碼表中存在 cw = hm1.get(n); // system.out.println(cw+"*"); if (pw != "" ) { os.write(cw.getbytes( "gbk" )); p = pw; c = cw.charat( 0 ) + "" ; // c=cw的第一個 // system.out.println(c+"&"); hm.put(p + c, bianma); hm1.put(bianma, p + c); bianma++; } else { os.write(cw.getbytes( "gbk" )); // 第一個 } } else { // 編碼表中不存在 p = pw; // system.out.println(pw+"-="); c = pw.charat( 0 ) + "" ; // c=pw的第一個 hm.put(p + c, bianma); hm1.put(bianma, p + c); bianma++; os.write((p + c).getbytes( "gbk" )); cw = p + c; } pw = cw; // system.out.println(bianma); // system.out.println(cw+"=="); if (i == 65535 ) { system.out.println( "重置2" ); hm.clear(); hm1.clear(); for ( int j = 0 ; j < 256 ; j++) { char ch = ( char ) j; string st = ch + "" ; hm.put(st, j); hm1.put(j, st); } bianma = 0 ; pw = "" ; } } // system.out.println(hm1.tostring()); os.close(); } } |
不足之處:當(dāng)編碼超過65535的時候,并沒有處理好,不能重置碼表,還原出的文件在超過65535的部分就開始亂碼。還有待改善。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
原文鏈接:https://blog.csdn.net/lzq1326253299/article/details/82750568