Java壓縮之LZW算法字典壓縮與解壓講解
壓縮過(guò)程:
前面已經(jīng)寫(xiě)過(guò)一篇哈夫曼壓縮,LZW字典壓縮與哈夫曼壓縮的不同之處在于不需要把編碼寫(xiě)入文件,編碼表是在讀文件中生成的,首先將0-255個(gè)ASCLL碼與對(duì)應(yīng)的數(shù)字存入哈希表中,作為基礎(chǔ)碼表。

這里的后綴為當(dāng)前
前綴+后綴 如果在碼表中存在,前綴等于前綴+后綴。如果不存在,將前綴+后綴所表示的字符串寫(xiě)入編碼表編碼,同時(shí)將前綴寫(xiě)入壓縮文件中。這里重點(diǎn)注意一下,一個(gè)字節(jié)所能表示的數(shù)字范圍為0-255,所以我們將一個(gè)字符的編碼變成兩個(gè)字節(jié)寫(xiě)進(jìn)去,分別寫(xiě)入它的高八位和低八位,比如256即為00000001 11111111 這里用到DataOutputStream dos對(duì)象中的 dos.writeChar(256)方法。
兩個(gè)字節(jié)所能表示的范圍為0-65535。當(dāng)我們的編碼超過(guò)這份范圍,就需要重置編碼表,再重新編碼。
解壓過(guò)程

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);// 對(duì)字節(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);//寫(xiě)出一個(gè)-1作為重置的表示與碼表的打印
hm.clear();//清空Hashmap
for (int j = 0; j < 256; j++) {//重新將基本256個(gè)編碼寫(xiě)入
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) {// 如果碼表中沒(méi)有 前綴加后綴的碼表
// System.out.print(zhongjian);
// System.out.println(" 對(duì)應(yīng)的編碼為 " + bianma);
hm.put(zhongjian, bianma);// 向碼表添加 前綴加后綴 和 對(duì)應(yīng)的編碼
// System.out.println(" " + perfix);
// System.out.println("寫(xiě)入的編碼 "+hm.get(perfix));
dos.writeChar(hm.get(perfix)); // 把前綴寫(xiě)入壓縮文件
bianma++;
perfix = suffix;
} else {// 如果有下一個(gè)前綴保存 上一個(gè)前綴加后綴
perfix = zhongjian;
}
if (i == str.length() - 1) {// 把最后一個(gè)寫(xiě)進(jìn)去
// System.out.print("寫(xiě)入最后一個(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) {// 如果低八位對(duì)應(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ì)列中的元素開(kāi)始重新編碼,輸出文件
*/
// 創(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)編碼超過(guò)65535的時(shí)候,并沒(méi)有處理好,不能重置碼表,還原出的文件在超過(guò)65535的部分就開(kāi)始亂碼。還有待改善。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
MybatisPlus實(shí)現(xiàn)邏輯刪除功能
這篇文章主要介紹了MybatisPlus實(shí)現(xiàn)邏輯刪除功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
tomcat報(bào)錯(cuò):Wrapper cannot find servlet class ...問(wèn)題解決
這篇文章主要介紹了tomcat報(bào)錯(cuò):Wrapper cannot find servlet class ...問(wèn)題解決的相關(guān)資料,需要的朋友可以參考下2016-11-11
Java線程并發(fā)中常見(jiàn)的鎖機(jī)制詳細(xì)介紹
越來(lái)越多的互聯(lián)網(wǎng)企業(yè)面臨著用戶(hù)量膨脹而帶來(lái)的并發(fā)安全問(wèn)題。接下來(lái)通過(guò)本文給大家介紹Java線程并發(fā)中常見(jiàn)的鎖機(jī)制,感興趣的朋友一起看看吧2016-05-05
Java面試官最喜歡問(wèn)的關(guān)鍵字之volatile詳解
這篇文章主要給大家介紹了關(guān)于Java面試官最喜歡問(wèn)的關(guān)鍵字之volatile的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
深入解析堆排序的算法思想及Java代碼的實(shí)現(xiàn)演示
堆排序基于二叉堆結(jié)構(gòu)即完全二叉樹(shù),可利用最大堆和最小堆的組建方式來(lái)進(jìn)行排序,這里就來(lái)深入解析堆排序的算法思想及Java代碼的實(shí)現(xiàn)演示2016-06-06
SpringCloud基于RestTemplate微服務(wù)項(xiàng)目案例解析
這篇文章主要介紹了SpringCloud基于RestTemplate微服務(wù)項(xiàng)目案例,在寫(xiě)SpringCloud搭建微服務(wù)之前,先搭建一個(gè)不通過(guò)springcloud只通過(guò)SpringBoot和Mybatis進(jìn)行模塊之間通訊,通過(guò)一個(gè)案例給大家詳細(xì)說(shuō)明,需要的朋友可以參考下2022-05-05
Spring框架實(shí)現(xiàn)依賴(lài)注入的原理
依賴(lài)注入是由“依賴(lài)”和“注入”兩個(gè)詞匯組合而成,那么我們?cè)僖淮雾樚倜?,分別分析這兩個(gè)詞語(yǔ),這篇文章主要介紹了Spring DI依賴(lài)注入詳解,需要的朋友可以參考下2023-04-04
SpringBoot API增加version版本號(hào)方式
這篇文章主要介紹了SpringBoot API增加version版本號(hào)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
java中自定義Spring Security權(quán)限控制管理示例(實(shí)戰(zhàn)篇)
本篇文章主要介紹了java中自定義Spring Security權(quán)限控制管理示例(實(shí)戰(zhàn)篇) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02

