資源簡介
用Java語言實現LZW壓縮解壓文件,實現的效率較高,而且壓縮時間也不是很長

代碼片段和文件信息
package?com.lzg.FileCompress;
import?java.io.BufferedInputStream;
import?java.io.BufferedOutputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.OutputStream;
public?class?LzwCompression?{
private?final?int?BITS;
private?final?int?TABLE_SIZE;
private?final?int?HASHING_SHIFT?=?4;
private?final?int?MAX_VALUE?;
private?final?int?MAX_CODE?;
private?final?int?EOF?=?-1;
private?BufferedInputStream?input?=?null;
private?BufferedOutputStream?output?=?null;
private?int?output_bit_count?=?0;
private?int?output_bit_buffer?=?0;
private?short[]?code_value;
private?short[]?prefix_code;
private?short[]?append_character;
public?LzwCompression()?{
this(12);
}
public?LzwCompression(int?bits)?{
BITS?=?bits;
if?(BITS?<=?12)?{
TABLE_SIZE?=?5021;
}?else?if?(BITS?==?13)?{
TABLE_SIZE?=?9029;
}?else
TABLE_SIZE?=?18041;
code_value?=?new?short[TABLE_SIZE];
prefix_code?=?new?short[TABLE_SIZE];
append_character?=?new?short[TABLE_SIZE];
MAX_VALUE?=?(1?< MAX_CODE?=?MAX_VALUE?-?1;
}
public?void?compress(InputStream?is?OutputStream?os)?throws?IOException?{
input?=?new?BufferedInputStream(is);
output?=?new?BufferedOutputStream(os);
short?next_code?=?0;
short?character?=?0;
short?string_code?=?0;
short?index?=?0;
next_code?=?256;
for?(short?i?=?0;?i? code_value[i]?=?-1;
string_code?=?(short)?input.read();
while?((character?=?(short)?input.read())?!=?EOF)?{
index?=?find_match(string_code?character);
if?(code_value[index]?!=?-1)?{
string_code?=?code_value[index];
}?else?{
if?(next_code?<=?MAX_CODE)?{
code_value[index]?=?next_code++;
prefix_code[index]?=?string_code;
append_character[index]?=?character;
}
output_code(string_code);
string_code?=?character;
}
}
output_code(string_code);
output_code((short)?MAX_VALUE);
output_code((short)?0);
output.close();
input.close();
}
private?short?find_match(short?hash_prefix?short?hash_character)?{
int?index?=?0;
int?offset?=?0;
index?=?(hash_character?<
if?(index?==?0)
offset?=?1;
else
offset?=?TABLE_SIZE?-?index;
while?(true)?{
if?(code_value[index]?==?-1)
return?(short)?index;
if?(prefix_code[index]?==?hash_prefix
&&?append_character[index]?==?hash_character)
return?(short)?index;
index?-=?offset;
if?(index?0)
index?+=?TABLE_SIZE;
}
}
private?void?output_code(short?code)?throws?IOException{
output_bit_buffer?|=?code?<(32?-?BITS?-?output_bit_count);
output_bit_count?+=?BITS;
while?(output_bit_count?>=?8)?{
output.write(output_bit_buffer?>>?24);
output_bit_buffer?<<=?8;
output_bit_count?-=?8;
}
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2889??2011-11-27?16:10??Lzw實現壓縮算法\LzwCompression.java
?????文件???????3791??2011-11-27?14:33??Lzw實現壓縮算法\LzwDecompression.java
?????文件????????608??2011-11-27?14:42??Lzw實現壓縮算法\TestLzw.java
?????目錄??????????0??2011-11-30?09:38??Lzw實現壓縮算法
-----------?---------??----------?-----??----
?????????????????7288????????????????????4
- 上一篇:一個鎖機小軟件
- 下一篇:apkutil解析apk安裝包的工具
評論
共有 條評論