Java壓縮之LZW算法字典壓縮與解壓講解
壓縮過程:
前面已經(jīng)寫過一篇哈夫曼壓縮,LZW字典壓縮與哈夫曼壓縮的不同之處在于不需要把編碼寫入文件,編碼表是在讀文件中生成的,首先將0-255個(gè)ASCLL碼與對應(yīng)的數(shù)字存入哈希表中,作為基礎(chǔ)碼表。
這里的后綴為當(dāng)前
前綴+后綴 如果在碼表中存在,前綴等于前綴+后綴。如果不存在,將前綴+后綴所表示的字符串寫入編碼表編碼,同時(shí)將前綴寫入壓縮文件中。這里重點(diǎn)注意一下,一個(gè)字節(jié)所能表示的數(shù)字范圍為0-255,所以我們將一個(gè)字符的編碼變成兩個(gè)字節(jié)寫進(jìn)去,分別寫入它的高八位和低八位,比如256即為00000001 11111111 這里用到DataOutputStream dos對象中的 dos.writeChar(256)方法。
兩個(gè)字節(jié)所能表示的范圍為0-65535。當(dāng)我們的編碼超過這份范圍,就需要重置編碼表,再重新編碼。
解壓過程
CW表示讀取的到的字符,PW為上一行的CW,CW再編碼表中存在:P→PW,C→CW的第一個(gè)字符,輸出CW。CW在編碼表中不存在,P→PW,C→PW的第一字符輸出P+C。
當(dāng)我們讀到65535的時(shí)候,就重置碼表,重新編碼。
代碼部分
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個(gè)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);//寫出一個(gè)-1作為重置的表示與碼表的打印 hm.clear();//清空Hashmap for (int j = 0; j < 256; j++) {//重新將基本256個(gè)編碼寫入 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 {// 如果有下一個(gè)前綴保存 上一個(gè)前綴加后綴 perfix = zhongjian; } if (i == str.length() - 1) {// 把最后一個(gè)寫進(jìn)去 // System.out.print("寫入最后一個(gè)"+perfix); dos.writeChar(hm.get(perfix)); // System.out.println(" "+hm.get(perfix)); } } os.close();// 關(guān)閉流 // System.out.println(hm.toString());// 輸出碼表 } }
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隊(duì)列中的元素開始重新編碼,輸出文件 */ // 創(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的第一個(gè) // System.out.println(c+"&"); hm.put(p + c, bianma); hm1.put(bianma, p + c); bianma++; } else { os.write(cw.getBytes("gbk"));// 第一個(gè) } } else {// 編碼表中不存在 p = pw; // System.out.println(pw+"-="); c = pw.charAt(0) + "";// c=pw的第一個(gè) 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的時(shí)候,并沒有處理好,不能重置碼表,還原出的文件在超過65535的部分就開始亂碼。還有待改善。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
Spring Boot mybatis-config 和 log4j 輸出sql 日志的方式
這篇文章主要介紹了Spring Boot mybatis-config 和 log4j 輸出sql 日志的方式,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07解決lambda表達(dá)式內(nèi)出現(xiàn)異常無法throw拋出的問題
這篇文章主要介紹了lambda表達(dá)式內(nèi)出現(xiàn)異常無法throw拋出的解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Spring Boot接口設(shè)計(jì)防篡改、防重放攻擊詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot接口設(shè)計(jì)防篡改、防重放攻擊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07C#使用MySQLConnectorNet和MySQLDriverCS操作MySQL的方法
這篇文章主要介紹了C#使用MySQLConnectorNet和MySQLDriverCS操作MySQL的方法,相比普通方法能夠在Windows下簡化很多操作步驟,需要的朋友可以參考下2016-04-04Java之SpringCloud Eurka注冊錯(cuò)誤解決方案
這篇文章主要介紹了Java之SpringCloud Eurka注冊錯(cuò)誤解決方案,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Java語言實(shí)現(xiàn)簡單FTP軟件 FTP遠(yuǎn)程文件管理模塊實(shí)現(xiàn)(10)
這篇文章主要為大家詳細(xì)介紹了Java語言實(shí)現(xiàn)簡單FTP軟件,F(xiàn)TP遠(yuǎn)程文件管理模塊的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04