资源简介
自己写的LZW压缩。因为自己搜这方面资料时,基本一个样,所以决定把自己分享下。
代码片段和文件信息
package ebk.aem.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Random;
public class LZWCompressions {
private short[] code_value;
private short[] prefix;
private short[] suffix;
private int BITS;
private final int TABLESIZE;
private final short SHIFT =4;
private final int MAX_CODE;
BufferedOutputStream bos;
public LZWCompressions()
{
this(12);
}
public LZWCompressions(int bits)
{
this.BITS = bits;
if(BITS <= 12)
TABLESIZE = 4096;
else if(BITS == 13)
TABLESIZE = 8192;
else
TABLESIZE = 16384;
code_value = new short[TABLESIZE];
prefix = new short[TABLESIZE];
suffix = new short[TABLESIZE];
MAX_CODE = (1 << BITS)-1-1; /*value of the largest index substract 1*/
}
public void Coding(File inpathFile outpath)
{
try {
FileInputStream fis = new FileInputStream(inpath);
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(outpath);
DataOutputStream dos = new DataOutputStream(fos);
short old_value = 0;
short new_value = 0;
short next_code = 256;
for(short i = 0 ;i {
code_value[i]= -1;
prefix[i] = -1;
suffix[i] = -1;
}
if(bis.available() > 0)
{
old_value =(short) bis.read();
while((new_value = (short)bis.read()) != -1)
{
// System.out.println(“old_value“+old_value);
// System.out.println(new_value);
short index = FindMatch(old_valuenew_value);
if(code_value[index] != -1)
{
old_value = code_value[index];
}
else{
if(next_code <= MAX_CODE) /*because next_code will be index(下标),next_code can‘t be greater the largest index of the array */
{
prefix[index] = old_value;
suffix[index] = new_value;
code_value[index] = next_code++;
}
dos.writeShort(old_value);
// System.out.println(old_value);
old_value = new_value; /*assign the new_value to old_value when next_code>MAX_CODE*/
}
}
dos.writeShort(old_value);
// System.out.println(old_value);
}
dos.close();
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Quadratic linear hash again
* @param pre
* @param suf
* @return
*/
private short FindMatch(short preshort suf)
{
int index;
int var = 0;
int offset = 0;
int di = 1;
index = (suf << SHIFT) ^ pre;
boolean flag = false;
while (true) {
if(var < TABLESIZE/2) /*please review principle of the quadratic linear hash again if you don‘t see this step*/
{
var++;
di = var*var;
}
if (code_value[index] == -1)
return (short) index;
i属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1800054 2013-05-31 13:35 LZW压缩\a.bmp
文件 84992 2013-07-15 21:49 LZW压缩\LZW算法说明_英文.doc
文件 77824 2013-07-19 13:17 LZW压缩\LZW算法说明中文.doc
文件 3366 2013-07-19 13:04 LZW压缩\程序源码\LZWCompressions.java
文件 4663 2013-07-19 11:44 LZW压缩\程序源码\LZWDeCompress.java
文件 1235 2013-07-19 12:58 LZW压缩\程序源码\LZWTEST.java
文件 613 2013-07-19 13:11 LZW压缩\程序说明.txt
目录 0 2013-07-19 13:16 LZW压缩\程序源码
目录 0 2013-07-19 13:18 LZW压缩
----------- --------- ---------- ----- ----
1972747 9
- 上一篇:JAVA仪器设备管理系统
- 下一篇:小芳便利店java实现
相关资源
- 微博系统(Java源码,servlet+jsp),适
- java串口通信全套完整代码-导入eclip
- jsonarray所必需的6个jar包.rar
- 三角网构TIN生成算法,Java语言实现
- java代码编写将excel数据导入到mysql数据
- Java写的cmm词法分析器源代码及javacc学
- JAVA JSP公司财务管理系统 源代码 论文
- JSP+MYSQL旅行社管理信息系统
- 推荐算法的JAVA实现
- 基于Java的酒店管理系统源码(毕业设
- java-图片识别 图片比较
- android毕业设计
- java23种设计模式+23个实例demo
- java Socket发送/接受报文
- JAVA828436
- java界面美化 提供多套皮肤直接使用
- 在线聊天系统(java代码)
- 基于Java的图书管理系统807185
- java中实现将页面数据导入Excel中
- java 企业销售管理系统
- java做的聊天系统(包括正规课程设计
- Java编写的qq聊天室
- 商店商品管理系统 JAVA写的 有界面
- JAVA开发聊天室程序
- 在linux系统下用java执行系统命令实例
- java期末考试试题两套(答案) 选择(
- JAVA3D编程示例(建模、交互)
- Java 文件加密传输
- java做的房产管理系统
- 基于jsp的bbs论坛 非常详细
川公网安备 51152502000135号
评论
共有 条评论