檔案壓縮與解壓

2021-07-05 03:28:58 字數 2072 閱讀 1281

檔案的壓縮:

1.讀取檔案的內容

2.統計每個字元出現的次數

int read;

while ((read = bis.read()) != -1)//直至讀到檔案結束

// arrays為運算元組的工具類(collections為操作集合工具類)

bis.close();

3.構建哈弗曼樹,生成哈夫曼編碼

if(node.getleftnode()==null&&node.getrightnode()==null)

if(node.getleftnode()!=null)

if(node.getrightnode()!=null)

4.向檔案中寫入每個哈夫曼編碼的長度

for(int i=0;i

5.將哈夫曼編碼生成二進位制**加入壓縮檔案中

string strb = "";//哈夫曼編碼的初始值

for (int i = 0; i < allcode.length; i++)

6.讀取檔案裡的內容,將其轉化為二進位制**加入壓縮檔案中

int read;
stringbuffer sb =new stringbuffer();

while((read=bis.read())!=-1)

7.如果壓縮檔案中二進位制位數不是8的倍數,在其後面補零,直至夠8的倍數,在在其後面加入8位補零的個數。

int count=0;

while(filedata.length()%8!=0)

//將count轉化為二進位制

string countbin=integer.tobinarystring(count);

//對countbin進行補零操作

while(countbin.length()<8)

8.將壓縮檔案中的二進位制**寫入壓縮檔案中

while(filedata.length()!=0)

檔案的解壓:

1讀取壓縮檔案中的二進位制串

int read;

int i = 0,j=0;

int a=new int[(int) (file.length()-256)];

while((read=bis.read())!=-1)

else

}

2從檔案的最後讀取檔案,獲得補零的位數,移除補的零

for( i=0;i

3.擷取哈夫曼編碼,根據圖中儲存的一一對應關係翻譯壓縮檔案,生成解壓檔案

for(int i=0;i

4.將解壓後的檔案寫入解壓檔案中

//進行翻譯

int i=1;

while(str.length()!=0)

else

} bos.flush();

bos.close();

檔案壓縮與解壓

檔案壓縮與解壓思想 1 統計字元出現次數,結合最小堆的性質生成哈夫曼樹 2 對葉節點進行編碼,結點左邊編 0,右邊編1 3 讀取檔案,用哈夫曼編碼代替字元產生新的字元,即壓縮 4 讀取壓縮檔案,進行哈夫曼編碼的解讀產生相應字元,即解壓 例如,對以下資料生成哈夫曼樹,以及產生相應的哈夫曼編碼 自己寫的...

檔案壓縮與解壓縮

類 zipfile 構造方法 zipfile file file 開啟供閱讀的zip檔案,由指定的file物件給出。zipfile file file,int mode 開啟新的zipfile以使用指定模式從指定file物件讀取 zipfile string name 開啟zip檔案進行閱讀 方法摘...

檔案壓縮與解壓縮

public class zipfileutil zipoutputstream zostream null fileinputstream fistream null fileoutputstream fostream null try zostream.closeentry issuccess ...