天堂向右,我依然向左

天下之大,虽离家千里,何处不可往!何事不可为!
生活之路,纵坎坷曲折,当奋斗不息,则精彩纷呈!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

测试代码:
String temp = "test kilonet for all";

  System.out.println("************oraginal:\r\n" + temp);

  byte[] tempb = null;
  try {
   tempb = temp.getBytes("utf-8");
  } catch (UnsupportedEncodingException e1) {
   e1.printStackTrace();
  }
  System.out.println("************init size:" + tempb.length);
  
  
  byte[] ziptemp;
  ziptemp = ZipUtil.gZip(tempb);
  System.out.println("************ziped size:" + ziptemp.length);

  byte[] unziptemp = ZipUtil.unGZip(ziptemp);
  System.out.println("************unziped size:" + unziptemp.length);
  
  System.out.println("************unzip & ziped:\r\n" + new String(unziptemp));

 

 

代码
package com.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    
/***
     * 压缩GZip
     * 
     * 
@param data
     * 
@return
     
*/
    
public static byte[] gZip(byte[] data) {
        
byte[] b = null;
        
try {
            ByteArrayOutputStream bos 
= new ByteArrayOutputStream();
            GZIPOutputStream gzip 
= new GZIPOutputStream(bos);
            gzip.write(data);
            gzip.finish();
            gzip.close();
            b 
= bos.toByteArray();
            bos.close();
        } 
catch (Exception ex) {
            ex.printStackTrace();
        }
        
return b;
    }

    
/***
     * 解压GZip
     * 
     * 
@param data
     * 
@return
     
*/
    
public static byte[] unGZip(byte[] data) {
        
byte[] b = null;
        
try {
            ByteArrayInputStream bis 
= new ByteArrayInputStream(data);
            GZIPInputStream gzip 
= new GZIPInputStream(bis);
            
byte[] buf = new byte[1024];
            
int num = -1;
            ByteArrayOutputStream baos 
= new ByteArrayOutputStream();
            
while ((num = gzip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 
0, num);
            }
            b 
= baos.toByteArray();
            baos.flush();
            baos.close();
            gzip.close();
            bis.close();
        } 
catch (Exception ex) {
            ex.printStackTrace();
        }
        
return b;
    }

    
/***
     * 压缩Zip
     * 
     * 
@param data
     * 
@return
     
*/
    
public static byte[] zip(byte[] data) {
        
byte[] b = null;
        
try {
            ByteArrayOutputStream bos 
= new ByteArrayOutputStream();
            ZipOutputStream zip 
= new ZipOutputStream(bos);
            ZipEntry entry 
= new ZipEntry("zip");
            entry.setSize(data.length);
            zip.putNextEntry(entry);
            zip.write(data);
            zip.closeEntry();
            zip.close();
            b 
= bos.toByteArray();
            bos.close();
        } 
catch (Exception ex) {
            ex.printStackTrace();
        }
        
return b;
    }

    
/***
     * 解压Zip
     * 
     * 
@param data
     * 
@return
     
*/
    
public static byte[] unZip(byte[] data) {
        
byte[] b = null;
        
try {
            ByteArrayInputStream bis 
= new ByteArrayInputStream(data);
            ZipInputStream zip 
= new ZipInputStream(bis);
            
while (zip.getNextEntry() != null) {
                
byte[] buf = new byte[1024];
                
int num = -1;
                ByteArrayOutputStream baos 
= new ByteArrayOutputStream();
                
while ((num = zip.read(buf, 0, buf.length)) != -1) {
                    baos.write(buf, 
0, num);
                }
                b 
= baos.toByteArray();
                baos.flush();
                baos.close();
            }
            zip.close();
            bis.close();
        } 
catch (Exception ex) {
            ex.printStackTrace();
        }
        
return b;
    }

    
/**
     * 把字节数组转换成16进制字符串
     * 
     * 
@param bArray
     * 
@return
     
*/
    
public static String bytesToHexString(byte[] bArray) {
        StringBuffer sb 
= new StringBuffer(bArray.length);
        String sTemp;
        
for (int i = 0; i < bArray.length; i++) {
            sTemp 
= Integer.toHexString(0xFF & bArray[i]);
            
if (sTemp.length() < 2)
                sb.append(
0);
            sb.append(sTemp.toUpperCase());
        }
        
return sb.toString();
    }

}

 

 

posted on 2010-11-17 14:51  老舟  阅读(801)  评论(0编辑  收藏  举报