Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 810|回复: 0

[Java基础知识]java实现的LZW 压缩算法源码

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-2 05:13:43 | 显示全部楼层 |阅读模式

    1. /*
    2. * LZW.java
    3. *
    4. * Created on 01 Dec 2005
    5. *
    6. * Implementation of LZW compression/decompression algorithm
    7. */
    8. import java.io.* ;
    9. /**
    10. *
    11. * @author  Moshe Fresko
    12. * @course        Algorithmic Programming 1
    13. * @exercise        3
    14. */
    15. public class LZW implements Compression
    16. {
    17.         boolean stopped = false ;
    18.         Dict dict ;
    19.                 // The bits that should be written for each code
    20.         int numOfBits ;
    21.                 // The previous string that we should remember
    22.                 // in order to insert into the dictionary
    23.         final ByteArray emptyBA = new ByteArray() ;
    24.         ByteArray w=emptyBA ;
    25.                 // Constructor gets the number of bits to be written for each code
    26.         public LZW()
    27.         {
    28.                 numOfBits = 12 ;
    29.                         // Create a new Limited Dictionary
    30.                         // For maximum of 2^bits entries
    31.                 dict = new LimitedDict(1<< umOfBits) ;
    32.                         // Add all ascii characters to the dictionary
    33.                 for (int i=0;i<256;++i)
    34.                         dict.add(new ByteArray((byte)i)) ;
    35.         }
    36.                 // Encodes the next character.
    37.                 // If there is a code generated returns it.
    38.                 // If not returns -1.
    39.         int encodeOneChar(int n) {
    40.                 byte c = (byte) n ;
    41.                 ByteArray nw = w.conc(c) ;
    42.                 int code = dict.numFromStr(nw) ;
    43.                         // if it exists then we continue to search for a longer string
    44.                 if (code!=-1) {
    45.                         w = nw ;
    46.                         return -1 ;
    47.                 } else {
    48.                         dict.add(nw) ;
    49.                         nw = w ;
    50.                         w = new ByteArray(c) ;
    51.                         return dict.numFromStr(nw) ;
    52.                 }
    53.         }
    54.                 // If there is something left in w, returns its code
    55.         int encodeLast() {
    56.                 ByteArray nw = w ;
    57.                 w = emptyBA ;
    58.                 return dict.numFromStr(nw) ;
    59.         }
    60.                 // Write the code in bits into output stream
    61.         void writeCode(OutputStream os, int code) throws IOException
    62.         {
    63.                 for (int i=0;i< numOfBits;++i) {
    64.                         os.write(code&1) ;
    65.                         code /= 2 ;
    66.                 }
    67.         }
    68.         int readCode(InputStream is) throws IOException
    69.         {
    70.                 int num = 0 ;
    71.                 for (int i=0;i< numOfBits;++i) {
    72.                         int next = is.read() ;
    73.                         if (next<0)
    74.                                 return -1 ;
    75.                         num += next<< i ;
    76.                 }
    77.                 return num ;
    78.         }
    79.                 // We need to call the close() method of BitOutputStream,
    80.                 // but without closing the encompassing OutputStream
    81.         private class UnClosedOutputStream extends FilterOutputStream {
    82.                 public UnClosedOutputStream(OutputStream os)
    83.                         { super(os) ; }
    84.     public void write(byte b[], int off, int len) throws IOException
    85.             { out.write(b,off,len) ; }
    86.             // Does not close anything
    87.                 public void close() throws IOException
    88.                         { }
    89.         }
    90.         public void compress(InputStream is, OutputStream os) throws IOException {
    91.                 os = new BitOutputStream(new UnClosedOutputStream(os)) ;
    92.                 int next ;        // next input character
    93.                 int code ;        // next code generated
    94.                 while ((next=is.read())>=0) {
    95.                         if (stopped)
    96.                                 break ;
    97.                         code = encodeOneChar(next) ;
    98.                         if (code>=0)
    99.                                 writeCode(os,code) ;
    100.                 }
    101.                 code = encodeLast() ;
    102.                 if (code>=0)
    103.                         writeCode(os,code) ;
    104.     os.close() ;
    105.         }
    106.         ByteArray decodeOne(int code) {
    107.                         // Either "ABA" or null, w="AB"
    108.                 ByteArray str = dict.strFromNum(code) ;
    109.                 if (str==null) {
    110.                         str = w.conc(w.getAt(0)) ;
    111.                         dict.add(str) ;
    112.                 } else
    113.                         if (! w.isEmpty())
    114.                                 dict.add(w.conc(str.getAt(0))) ;
    115.             w = str ;
    116.                 return w ;
    117.         }
    118.         public void decompress(InputStream is, OutputStream os) throws IOException {
    119.                 is = new BitInputStream(is) ;
    120.                 ByteArray str ;        // Next entry
    121.                 int code ;                // Next code to be read
    122.                 while ((code=readCode(is))>=0) {
    123.                         if (stopped)
    124.                                 break ;
    125.                         str = decodeOne(code) ;
    126.                         os.write(str.getBytes()) ;
    127.                 }
    128.         }
    129.         public void stop()
    130.                 { stopped = true ; }
    131.        public static void main(String args[]){//简单的测试
    132.           LZW lzw=new LZW();
    133.           try{
    134.             lzw.compress(new FileInputStream("LZW.JAVA"),new FileOutputStream("lzw.lzw"));
    135.             lzw.decompress(new FileInputStream("lzw.lzw"),new FileOutputStream("lzw1.java"));
    136.           }catch(Exception e){}
    137.        }
    138. }
    139. 其它文件请下载。
    复制代码


      
      
       
       

         
       

         
       
      



    源码下载:http://file.javaxxz.com/2014/10/2/051343266.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-27 01:56 , Processed in 0.297079 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表