-
大小: 62KB文件類型: .rar金幣: 2下載: 1 次發(fā)布日期: 2021-07-22
- 語(yǔ)言: Java
- 標(biāo)簽: LZW??C實(shí)現(xiàn)??JAVA實(shí)現(xiàn)??壓縮??解壓縮??
資源簡(jiǎn)介
LZW算法說(shuō)明 及 C與 Java實(shí)現(xiàn)
在網(wǎng)上找的C語(yǔ)言版本,自己根據(jù)對(duì)算法的理解和參考C語(yǔ)言,用Java實(shí)現(xiàn)此算法的壓縮解壓縮。需要的朋友可以下載研究

代碼片段和文件信息
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;
}
}
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????5894??2009-08-08?10:17??LZW_JAVA\LZWCompression?測(cè)試版本.txt
?????文件???????2858??2009-08-08?11:54??LZW_JAVA\LzwCompression.java
?????文件???????3744??2009-08-08?11:53??LZW_JAVA\LzwDecompression.java
?????文件????????577??2009-08-08?12:03??LZW_JAVA\TestLzw.java
?????文件??????10429??2009-08-08?10:37??LZW_C\LZW?C發(fā)布版.txt
?????文件??????10884??2009-08-08?10:35??LZW_C\LZW?C測(cè)試版.txt
?????目錄??????????0??2009-08-08?12:04??LZW_JAVA
?????目錄??????????0??2009-08-08?12:05??LZW_C
?????文件?????110592??2009-08-08?12:08??LZW數(shù)據(jù)壓縮算法的原理分析_中文.doc
?????文件??????84480??2009-08-08?12:07??LZW算法說(shuō)明_英文.doc
-----------?---------??----------?-----??----
???????????????229458????????????????????10
評(píng)論
共有 條評(píng)論