资源简介
用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 << BITS) - 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 < TABLE_SIZE; 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 << HASHING_SHIFT) ^ hash_prefix;
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安装包的工具
相关资源
- java实现数据的备份与恢复jar包
- java3D显示一个球的代码
- 利用Java3D1.4制成的动态爬山算法
- Activiti7视频教程Java通用型工作流开发
- 基于Java的课堂随机点名器
- 基于Java的课堂_随机点名器
- 使用java实现的Bezier曲线
- java + sql 银行管理系统
- QR分解的Java实现
- ISM解释结构模型算法的JAVA
- Java+ajax写的登录
- Java多人聊天室(有登录注册)
- 根据配置文件自动构造Bean类
- 基于java-web的图书管理系统带源码
- 大文件上传的javawebuploader包括前台和
- Java-保存计算过程的计算器
- SpringBoot集成Elasticsearch已实现各种ES操
- java 抽奖系统jar 完整版
- JSP 和 JavaBean连接sql server验证登录
- 基于JAVA的小区物业管理系统 论文
- eclipse 4.7 oxy folding 代码折叠 com.cb.ec
- 用eclipse写的贪吃蛇(java)
- java统计正文中关键字个数
- Javaweb版的图书管理系统
- java-client-1.2.1.jar
- Quartz动态修改时间,java实现修改Cro
- 文件加密解密算法(Java源码)
- java连接数据库工具类--DBConnection(j
- 学生信息管理系统(java+mysql)
- 中控考勤机java源码demo
评论
共有 条评论