一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - Java教程 - Java壓縮之LZW算法字典壓縮與解壓講解

Java壓縮之LZW算法字典壓縮與解壓講解

2021-07-14 15:50凌晨兩點半的太陽 Java教程

今天小編就為大家分享一篇關(guān)于Java壓縮之LZW算法字典壓縮與解壓講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

壓縮過程:

前面已經(jīng)寫過一篇哈夫曼壓縮,lzw字典壓縮與哈夫曼壓縮的不同之處在于不需要把編碼寫入文件,編碼表是在讀文件中生成的,首先將0-255個ascll碼與對應(yīng)的數(shù)字存入哈希表中,作為基礎(chǔ)碼表。

Java壓縮之LZW算法字典壓縮與解壓講解

這里的后綴為當(dāng)前

前綴+后綴 如果在碼表中存在,前綴等于前綴+后綴。如果不存在,將前綴+后綴所表示的字符串寫入編碼表編碼,同時將前綴寫入壓縮文件中。這里重點注意一下,一個字節(jié)所能表示的數(shù)字范圍為0-255,所以我們將一個字符的編碼變成兩個字節(jié)寫進(jìn)去,分別寫入它的高八位和低八位,比如256即為00000001 11111111 這里用到dataoutputstream dos對象中的 dos.writechar(256)方法。

兩個字節(jié)所能表示的范圍為0-65535。當(dāng)我們的編碼超過這份范圍,就需要重置編碼表,再重新編碼。

解壓過程

Java壓縮之LZW算法字典壓縮與解壓講解

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 性满足久久久久久久久 | 美女脱了内裤打开腿让人羞羞软件 | 国产老太婆hd老头 | 美女张开大腿让男人桶 | 亚洲AV久久无码精品蜜桃 | 国四虎影永久 | 大陆国产vs国产对白 | 国产精品一久久香蕉产线看 | 夫妇交换小说 | 免费特黄视频 | 398av影院视频在线 | 亚洲一区二区福利视频 | 色综合视频一区二区观看 | 耽美肉文高h | 九九热在线免费观看 | 無码一区中文字幕少妇熟女网站 | 9久爱午夜视频 | 2012在线观看免费视频大全 | 贵妇的私人性俱乐部 | 亚洲国产综合精品 | 91精品国产亚洲爽啪在线影院 | 亚洲欧美国产另类视频 | 841995论坛网站2022年 | 精选国产AV精选一区二区三区 | 亚洲精品专区 | 红杏劫| 色狠狠成人综合网 | yjsp妖精视频在线观看免费 | 狠狠做五月深爱婷婷天天综合 | 美女被无套进入 | 五月丁香啪啪. | 88av视频在线观看 | 草草草视频在线观看 | 成人黄色a级片 | 互换身体全集免费观看 | 成品人视频w免费观看w | 69pao强力打造免费高速 | 日韩色在线观看 | 免费观看一级特黄三大片视频 | 男同互操 | 久久精品一区二区三区资源网 |