Java压缩Zlib,Gzip,Zip支持J2ME

  1. /*  
  2. * 文件名:     ZipUtil.java  
  3. * 版权:        xxxxxxxx.com. Copyright 1999-2010, All rights reserved  
  4. * 描述:       是压缩工具类,此类根据com.jcraft.jzlib地三方提供的核心类进行.压缩和解压缩。  
  5. * 修改人:       
  6. * 修改时间:   2010-09-13  
  7. * 跟踪单号:      
  8. * 修改单号:      
  9. * 修改内容:    新增  
  10. 可以到google是去下载jzlib4me20100516.rar 也就是jzlib4me的google项目为第三方支持包.  
  11. 这个ZipUtil.java的zlib支持J2ME.也就是将zlib的压缩和解压缩的两个方法可以放到J2ME项目中.但也需要jzlib4me20100516.rar包.  
  12. */   
  13. package  com.temobi.ms.util;  
  14.   
  15. import  java.io.ByteArrayInputStream;  
  16. import  java.io.ByteArrayOutputStream;  
  17. import  java.io.DataInputStream;  
  18. import  java.io.DataOutputStream;  
  19. import  java.io.IOException;  
  20. import  java.util.zip.GZIPInputStream;  
  21. import  java.util.zip.GZIPOutputStream;  
  22. import  java.util.zip.ZipEntry;  
  23. import  java.util.zip.ZipInputStream;  
  24. import  java.util.zip.ZipOutputStream;  
  25.   
  26. import  com.jcraft.jzlib.JZlib;  
  27. import  com.jcraft.jzlib.ZInputStream;  
  28. import  com.jcraft.jzlib.ZOutputStream;  
  29. import  com.temobi.ms.resource.ConfigRes;  
  30. import  com.temobi.ms.resource.Const;  
  31.   
  32. /**  
  33. * 压缩工具包  
  34. */   
  35. public   class  ZipUtil  
  36. {  
  37.   
  38.     public   static   byte [] deflate( byte [] bContent)  
  39.     {  
  40.         MapServerLog.beforeMethod();  
  41.         try   
  42.         {  
  43.             String sys_compress = ConfigRes.getInstance().get(Const.SYS_COMPRESS);  
  44.               
  45.             byte [] temp =  null ;  
  46.               
  47.             if ( "LZIP" .equals(sys_compress))  
  48.             {  
  49.                 temp = ZipUtil.zLib(bContent);  
  50.             }  
  51.             else   
  52.             if ( "GZIP" .equals(sys_compress))  
  53.             {  
  54.                 temp = ZipUtil.gZip(bContent);  
  55.             }  
  56.             else   
  57.             if ( "ZIP" .equals(sys_compress))  
  58.             {  
  59.                 temp = ZipUtil.zip(bContent);  
  60.             }  
  61.              
  62.             MapServerLog.afterMethod();  
  63.               
  64.             return  temp;  
  65.         }  
  66.         catch  (IOException e)  
  67.         {  
  68.             MapServerLog.exceptionMethod(e);  
  69.             e.printStackTrace();  
  70.         }  
  71.           
  72.         MapServerLog.afterMethod();  
  73.         return   null ;  
  74.     }  
  75.   
  76.     public   static   byte [] inflate( byte [] bContent)  
  77.     {  
  78.         MapServerLog.beforeMethod();  
  79.         try   
  80.         {  
  81.             String sys_compress = ConfigRes.getInstance().get(Const.SYS_COMPRESS);  
  82.               
  83.             byte [] temp =  null ;  
  84.               
  85.             if ( "LZIP" .equals(sys_compress))  
  86.             {  
  87.                 temp = ZipUtil.unZLib(bContent);  
  88.             }  
  89.             else   
  90.             if ( "GZIP" .equals(sys_compress))  
  91.             {  
  92.                 temp = ZipUtil.unGZip(bContent);  
  93.             }  
  94.             else   
  95.             if ( "ZIP" .equals(sys_compress))  
  96.             {  
  97.                 temp = ZipUtil.unZip(bContent);  
  98.             }  
  99.              
  100.             MapServerLog.afterMethod();  
  101.               
  102.             return  temp;  
  103.         }  
  104.         catch  (IOException e)  
  105.         {  
  106.             MapServerLog.exceptionMethod(e);  
  107.             e.printStackTrace();  
  108.         }  
  109.           
  110.         MapServerLog.afterMethod();  
  111.         return   null ;  
  112.     }  
  113.   
  114.     // 输入数据的最大长度   
  115.     private   static   final   int  MAXLENGTH =  102400 ;  
  116.   
  117.     // 设置缓存大小   
  118.     private   static   final   int  BUFFERSIZE =  1024 ;  
  119.   
  120.     // 压缩选择方式:   
  121.     //   
  122.     // /** Try o get the best possible compression */   
  123.     // public static final int COMPRESSION_MAX = JZlib.Z_BEST_COMPRESSION;   
  124.     //   
  125.     // /** Favor speed over compression ratio */   
  126.     // public static final int COMPRESSION_MIN = JZlib.Z_BEST_SPEED;   
  127.     //   
  128.     // /** No compression */   
  129.     // public static final int COMPRESSION_NONE = JZlib.Z_NO_COMPRESSION;   
  130.     //   
  131.     // /** Default compression */   
  132.     // public static final int COMPRESSION_DEFAULT =   
  133.     // JZlib.Z_DEFAULT_COMPRESSION;   
  134.   
  135.     /**  
  136.      * ZLib压缩数据  
  137.      *   
  138.      * @param object  
  139.      * @return  
  140.      * @throws IOException  
  141.      */   
  142.     public   static   byte [] zLib( byte [] bContent)  throws  IOException  
  143.     {  
  144.   
  145.         byte [] data =  null ;  
  146.         try   
  147.         {  
  148.             ByteArrayOutputStream out = new  ByteArrayOutputStream();  
  149.             ZOutputStream zOut = new  ZOutputStream(out,  
  150.                     JZlib.Z_BEST_COMPRESSION); // 压缩级别,缺省为1级   
  151.             DataOutputStream objOut = new  DataOutputStream(zOut);  
  152.             objOut.write(bContent);  
  153.             objOut.flush();  
  154.             zOut.close();  
  155.             data = out.toByteArray();  
  156.             out.close();  
  157.   
  158.         }  
  159.         catch  (IOException e)  
  160.         {  
  161.             e.printStackTrace();  
  162.             throw  e;  
  163.         }  
  164.         return  data;  
  165.     }  
  166.   
  167.     /**  
  168.      * ZLib解压数据  
  169.      *   
  170.      * @param object  
  171.      * @return  
  172.      * @throws IOException  
  173.      */   
  174.     public   static   byte [] unZLib( byte [] bContent)  throws  IOException  
  175.     {  
  176.   
  177.         byte [] data =  new   byte [MAXLENGTH];  
  178.         try   
  179.         {  
  180.             ByteArrayInputStream in = new  ByteArrayInputStream(bContent);  
  181.             ZInputStream zIn = new  ZInputStream(in);  
  182.             DataInputStream objIn = new  DataInputStream(zIn);  
  183.   
  184.             int  len =  0 ;  
  185.             int  count =  0 ;  
  186.             while  ((count = objIn.read(data, len, len + BUFFERSIZE)) != - 1 )  
  187.             {  
  188.                 len = len + count;  
  189.             }  
  190.   
  191.             byte [] trueData =  new   byte [len];  
  192.             System.arraycopy(data, 0 , trueData,  0 , len);  
  193.   
  194.             objIn.close();  
  195.             zIn.close();  
  196.             in.close();  
  197.   
  198.             return  trueData;  
  199.   
  200.         }  
  201.         catch  (IOException e)  
  202.         {  
  203.             e.printStackTrace();  
  204.             throw  e;  
  205.         }  
  206.     }  
  207.   
  208.     /**  
  209.      * GZip压缩数据  
  210.      *   
  211.      * @param object  
  212.      * @return  
  213.      * @throws IOException  
  214.      */   
  215.     public   static   byte [] gZip( byte [] bContent)  throws  IOException  
  216.     {  
  217.   
  218.         byte [] data =  null ;  
  219.         try   
  220.         {  
  221.             ByteArrayOutputStream out = new  ByteArrayOutputStream();  
  222.   
  223.             GZIPOutputStream gOut = new  GZIPOutputStream(out, bContent.length);  // 压缩级别,缺省为1级   
  224.             DataOutputStream objOut = new  DataOutputStream(gOut);  
  225.             objOut.write(bContent);  
  226.             objOut.flush();  
  227.             gOut.close();  
  228.             data = out.toByteArray();  
  229.             out.close();  
  230.   
  231.         }  
  232.         catch  (IOException e)  
  233.         {  
  234.             e.printStackTrace();  
  235.             throw  e;  
  236.         }  
  237.         return  data;  
  238.     }  
  239.   
  240.     /**  
  241.      * GZip解压数据  
  242.      *   
  243.      * @param object  
  244.      * @return  
  245.      * @throws IOException  
  246.      */   
  247.     public   static   byte [] unGZip( byte [] bContent)  throws  IOException  
  248.     {  
  249.   
  250.         byte [] data =  new   byte [MAXLENGTH];  
  251.         try   
  252.         {  
  253.             ByteArrayInputStream in = new  ByteArrayInputStream(bContent);  
  254.             GZIPInputStream pIn = new  GZIPInputStream(in);  
  255.             DataInputStream objIn = new  DataInputStream(pIn);  
  256.   
  257.             int  len =  0 ;  
  258.             int  count =  0 ;  
  259.             while  ((count = objIn.read(data, len, len + BUFFERSIZE)) != - 1 )  
  260.             {  
  261.                 len = len + count;  
  262.             }  
  263.   
  264.             byte [] trueData =  new   byte [len];  
  265.             System.arraycopy(data, 0 , trueData,  0 , len);  
  266.   
  267.             objIn.close();  
  268.             pIn.close();  
  269.             in.close();  
  270.   
  271.             return  trueData;  
  272.   
  273.         }  
  274.         catch  (IOException e)  
  275.         {  
  276.             e.printStackTrace();  
  277.             throw  e;  
  278.         }  
  279.     }  
  280.   
  281.     /***  
  282.      * 压缩Zip  
  283.      *   
  284.      * @param data  
  285.      * @return  
  286.      * @throws IOException  
  287.      */   
  288.     public   static   byte [] zip( byte [] bContent)  throws  IOException  
  289.     {  
  290.   
  291.         byte [] b =  null ;  
  292.         try   
  293.         {  
  294.             ByteArrayOutputStream bos = new  ByteArrayOutputStream();  
  295.             ZipOutputStream zip = new  ZipOutputStream(bos);  
  296.             ZipEntry entry = new  ZipEntry( "zip" );  
  297.             entry.setSize(bContent.length);  
  298.             zip.putNextEntry(entry);  
  299.             zip.write(bContent);  
  300.             zip.closeEntry();  
  301.             zip.close();  
  302.             b = bos.toByteArray();  
  303.             bos.close();  
  304.         }  
  305.         catch  (Exception ex)  
  306.         {  
  307.             ex.printStackTrace();  
  308.         }  
  309.         return  b;  
  310.     }  
  311.   
  312.     /***  
  313.      * 解压Zip  
  314.      *   
  315.      * @param data  
  316.      * @return  
  317.      * @throws IOException  
  318.      */   
  319.     public   static   byte [] unZip( byte [] bContent)  throws  IOException  
  320.     {  
  321.         byte [] b =  null ;  
  322.         try   
  323.         {  
  324.             ByteArrayInputStream bis = new  ByteArrayInputStream(bContent);  
  325.             ZipInputStream zip = new  ZipInputStream(bis);  
  326.             while  (zip.getNextEntry() !=  null )  
  327.             {  
  328.                 byte [] buf =  new   byte [ 1024 ];  
  329.                 int  num = - 1 ;  
  330.                 ByteArrayOutputStream baos = new  ByteArrayOutputStream();  
  331.                 while  ((num = zip.read(buf,  0 , buf.length)) != - 1 )  
  332.                 {  
  333.                     baos.write(buf, 0 , num);  
  334.                 }  
  335.                 b = baos.toByteArray();  
  336.                 baos.flush();  
  337.                 baos.close();  
  338.             }  
  339.             zip.close();  
  340.             bis.close();  
  341.         }  
  342.         catch  (Exception ex)  
  343.         {  
  344.             ex.printStackTrace();  
  345.         }  
  346.         return  b;  
  347.     }  
  348.   
  349.     public   static   void  main(String[] args)  
  350.     {  
  351.         String newContent = "" ;  
  352.   
  353.         try   
  354.         {  
  355.             String content = "水电费his大家fks打飞机速度快放假了速度快放假速度发生的飞机上的考虑防静电速度开飞机上打开了房间速度快让他文件" ;  
  356.             System.out.println(content);  
  357.             byte [] origin = content.getBytes();  
  358.             System.out.println("原始长度 length is : "  + origin.length);  
  359.   
  360.             // ZLib 压缩   
  361.             byte [] zLibCnt = zLib(origin);  
  362.             System.out.println("zLib压缩后长度 : "  + zLibCnt.length);  
  363.   
  364.             byte [] unzLibCnt = unZLib(zLibCnt);  
  365.             System.out.println("zLib解压后长度 : "  + unzLibCnt.length);  
  366.   
  367.             newContent = new  String(unzLibCnt);  
  368.             System.out.println(newContent);  
  369.   
  370.             // GZip 压缩   
  371.             byte [] gZipCnt = gZip(origin);  
  372.             System.out.println("GZip压缩后长度 : "  + gZipCnt.length);  
  373.   
  374.             byte [] ungZipCnt = unGZip(gZipCnt);  
  375.             System.out.println("GZip解压后长度 : "  + ungZipCnt.length);  
  376.   
  377.             newContent = new  String(ungZipCnt);  
  378.             System.out.println(newContent);  
  379.   
  380.             // Zip 压缩   
  381.             byte [] zipCnt = zip(origin);  
  382.             System.out.println("Zip压缩后长度 : "  + zipCnt.length);  
  383.   
  384.             byte [] unZipCnt = unZip(zipCnt);  
  385.             System.out.println("Zip解压后长度 : "  + unZipCnt.length);  
  386.   
  387.             newContent = new  String(unZipCnt);  
  388.             System.out.println(newContent);  
  389.         }  
  390.         catch  (Exception e)  
  391.         {  
  392.             e.printStackTrace();  
  393.         }  
  394.   
  395.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值