导读:/*

* LZW.java

*

* Created on 01 Dec 2005

*

* Implementation of LZW compression/decompression algorithm

*/

import java.io.* ;

/**

*

* @author Moshe Fresko

* @courseAlgorithmic Programming 1

* @exercise3

*/

public class LZW implements Compression

{

boolean stopped = false ;

Dict dict ;

// The bits that should be written for each code

int numOfBits ;

// The previous string that we should remember

// in order to insert into the dictionary

final ByteArray emptyBA = new ByteArray() ;

ByteArray w=emptyBA ;

// Constructor gets the number of bits to be written for each code

public LZW()

{

numOfBits = 12 ;

// Create a new Limited Dictionary

// For maximum of 2^bits entries

dict = new LimitedDict(1// Add all ascii characters to the dictionary

for (int i=0;idict.add(new ByteArray((byte)i)) ;

}

// Encodes the next character.

// If there is a code generated returns it.

// If not returns -1.

int encodeOneChar(int n) {

byte c = (byte) n ;

ByteArray nw = w.conc(c) ;

int code = dict.numFromStr(nw) ;

// if it exists then we continue to search for a longer string

if (code!=-1) {

w = nw ;

return -1 ;

} else {

dict.add(nw) ;

nw = w ;

w = new ByteArray(c) ;

return dict.numFromStr(nw) ;

}

}

// If there is something left in w, returns its code

int encodeLast() {

ByteArray nw = w ;

w = emptyBA ;

return dict.numFromStr(nw) ;

}

// Write the code in bits into output stream

void writeCode(OutputStream os, int code) throws IOException

{

for (int i=0;ios.write(code&1) ;

code /= 2 ;

}

}

int readCode(InputStream is) throws IOException

{

int num = 0 ;

for (int i=0;iint next = is.read() ;

if (nextreturn -1 ;

num += next}

return num ;

}

// We need to call the close() method of BitOutputStream,

// but without closing the encompassing OutputStream

private class UnClosedOutputStream extends FilterOutputStream {

public UnClosedOutputStream(OutputStream os)

{ super(os) ; }

public void write(byte b[], int off, int len) throws IOException

{ out.write(b,off,len) ; }

// Does not close anything

public void close() throws IOException

{ }

}

public void compress(InputStream is, OutputStream os) throws IOException {

os = new BitOutputStream(new UnClosedOutputStream(os)) ;

int next ;// next input character

int code ;// next code generated

while ((next=is.read())>=0) {

if (stopped)

break ;

code = encodeOneChar(next) ;

if (code>=0)

writeCode(os,code) ;

}

code = encodeLast() ;

if (code>=0)

writeCode(os,code) ;

os.close() ;

}

ByteArray decodeOne(int code) {

// Either "ABA" or null, w="AB"

ByteArray str = dict.strFromNum(code) ;

if (str==null) {

str = w.conc(w.getAt(0)) ;

dict.add(str) ;

} else

if (! w.isEmpty())

dict.add(w.conc(str.getAt(0))) ;

w = str ;

return w ;

}

public void decompress(InputStream is, OutputStream os) throws IOException {

is = new BitInputStream(is) ;

ByteArray str ;// Next entry

int code ;// Next code to be read

while ((code=readCode(is))>=0) {

if (stopped)

break ;

str = decodeOne(code) ;

os.write(str.getBytes()) ;

}

}

public void stop()

{ stopped = true ; }

public static void main(String args[]){//简单的测试

LZW lzw=new LZW();

try{

lzw.compress(new FileInputStream("LZW.JAVA"),new FileOutputStream("lzw.lzw"));

lzw.decompress(new FileInputStream("lzw.lzw"),new FileOutputStream("lzw1.java"));

}catch(Exception e){}

}

}

其它文件请下载。

lzw压缩 java_java实现的LZW 压缩算法源码 | 学步园相关推荐

  1. mysql windows编译_Windows平台下编译Mysql源码 | 学步园

    最近由于项目的关系,需要使用到Mysql数据库,而我的工作任务与数据库有很大的关系,所以,决定好好学学Mysql,同时,也把Mysql的源码下载了,希望能有利于对它的学习.这里记录一下windows平 ...

  2. java rsa enc 源码_RSA加解密源码 | 学步园

    源码: #include #include #include #include #include #include #include typedef struct{ unsigned char enc ...

  3. java实现魔方_闲来无事,用java写了一个魔方小程序。附源码 | 学步园

    闲来无事,用java写了一个魔方小程序.附源码 使用三维数组.相对来说还是简单.呵呵. import java.util.ArrayList; import java.util.List; impor ...

  4. Asp.net在线压缩和解压缩简单实现(附项目源码)

    首先服务器上要确保安装rar软件,且对相应目录有可写权限,该代码利用递归显示文件目录,实现文件在线压缩和解压并测试成功. 这里参照了一些网络文档(清清月儿),代码有详细注释,便于大家学习和参考.rar ...

  5. 【老生谈算法】matlab实现图像压缩算法源码——图像压缩

    matlab图像压缩算法详解 1.文档下载: 本算法已经整理成文档如下,有需要的朋友可以点击进行下载 序号 文档(点击下载) 本项目文档 [老生谈算法]图像压缩试验matlab.doc 2.算法详解: ...

  6. ICSharpCode.SharpZipLib 压缩、解压文件 附源码

    http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, ...

  7. 基于Huffman编码的压缩和解压缩小软件(附C++源码)

    压缩前: 将pic.png拖到.exe文件上,可得到.zLzip压缩文件: 编码过程: 压缩过程: 将.zLzip压缩文件拖回可解压缩得到原文件: 顺便一提,当原文件内字符分布均衡时,其信息熵很低,压 ...

  8. 坦克大战java_java版坦克大战游戏源码

    [java]代码库package tankwar; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; imp ...

  9. beaninfo详解源码解析 java_Java后端精选技术:源码解析Spring Cloud Zuul

    Zuul 架构图 在zuul中, 整个请求的过程是这样的,首先将请求给zuulservlet处理,zuulservlet中有一个zuulRunner对象,该对象中初始化了RequestContext: ...

最新文章

  1. 在Ubuntu 16.04.3 LTS上搭建Go语言环境实录
  2. 进击时代!王雪红的谦卑与坚守
  3. 【机器学习实战 第九章】树回归 CART算法的原理与实现 - python3
  4. Linux|UNIX下LAMP环境的搭建及常见问题[连载4]
  5. leetcode-C语言代码练习
  6. 关于Asp.net core配置信息读取的源码分析梳理
  7. xflash里的hello world程序
  8. sql 删除重复记录
  9. Servlet详细讲解
  10. mysql数据库服务器cpu_mysql数据库服务器cpu 100%
  11. Subversion代码提交中的org.apache.subversion.javahl.ClientException: svn: E200007: Commit failed异常解决...
  12. 网页CSS常用中英文字体收集
  13. dq坐标系下无功功率表达式_基于数学形态学的谐波检测
  14. 如何下载官方windows10的ios镜像文件
  15. 微信24小时到账_最新微信转账延迟24小时到账骗局
  16. linux驱动面试题2018(面试题整理,含答案)
  17. 『天涯杂谈』十大古今名人语录经典(2007版)
  18. 使用vue ui创建vue项目(基于图形化界面的方式)
  19. java中去字符串中的(全角)空格
  20. wordpress后台管理(七)说说管理:发表说说、所有说说

热门文章

  1. cookie注入讲解
  2. 数据库调优都涉及哪些方面
  3. 第八课 RNN条件生成与Attention机制
  4. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第5篇]复杂性类NP是什么意思?
  5. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第51篇]什么是基于ID的加密的安全模型,描述一个IBE方案
  6. [程序员面试金典][JAVA][第02.01题][移除重复节点][Set][双指针]
  7. [剑指offer][JAVA]面试题第[31]题[栈的压入、弹出序列][栈]
  8. php word excel,PHP 生成word 和 excel 文档
  9. 递归入门 斐波那契数列
  10. linux 使cpu使用率升高_关于linux系统CPU篇---gt;CPU使用率升高