GZipUtils 工具类

本文介绍了GZipUtils工具类,用于在Java中实现文件的GZIP压缩和解压操作,包括unGzip方法处理Membercache文件,gzip方法压缩数据,以及zipFile和unZipFile方法分别对文件进行压缩和解压的实用示例。
摘要由CSDN通过智能技术生成

GZipUtils 工具类

package utils;

import java.io.ByteArrayInputStream;  
import java.io.ByteArrayOutputStream;  
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;  
import java.util.zip.GZIPInputStream;  
import java.util.zip.GZIPOutputStream;  

/**   
 * 压缩文件工具类
 *
 * 
 */
public class GZipUtils {
	/** 
	  * Member cache 文件解压处理 
	  *  
	  * @param buf 
	  * @return 
	  * @throws IOException 
	  */  
	 public static byte[] unGzip(byte[] buf) throws IOException {  
	  GZIPInputStream gzi = null;  
	  ByteArrayOutputStream bos = null;  
	  try {  
	   gzi = new GZIPInputStream(new ByteArrayInputStream(buf));  
	   bos = new ByteArrayOutputStream(buf.length);  
	   int count = 0;  
	   byte[] tmp = new byte[2048];  
	   while ((count = gzi.read(tmp)) != -1) {  
	    bos.write(tmp, 0, count);  
	   }  
	   buf = bos.toByteArray();  
	  } finally {  
	   if (bos != null) {  
	    bos.flush();  
	    bos.close();  
	   }  
	   if (gzi != null)  
	    gzi.close();  
	  }  
	  return buf;  
	 }  
	  
	 /** 
	  * Member cache 文件压缩处理 
	  *  
	  * @param val 
	  * @return 
	  * @throws IOException 
	  */  
	 public static byte[] gzip(byte[] val) throws IOException {  
	  ByteArrayOutputStream bos = new ByteArrayOutputStream(val.length);  
	  GZIPOutputStream gos = null;  
	  try {  
	   gos = new GZIPOutputStream(bos);  
	   gos.write(val, 0, val.length);  
	   gos.finish();  
	   gos.flush();  
	   bos.flush();  
	   val = bos.toByteArray();  
	  } finally {  
	   if (gos != null)  
	    gos.close();  
	   if (bos != null)  
	    bos.close();  
	  }  
	  return val;  
	 }  
	 
	 /** 
	  * 对文件进行压缩 
	  *  
	  * @param source 
	  *            源文件 
	  * @param target 
	  *            目标文件 
	  * @throws IOException 
	  */  
	 public static void zipFile(String source, String target) throws IOException {  
	  FileInputStream fin = null;  
	  FileOutputStream fout = null;  
	  GZIPOutputStream gzout = null;  
	  try {  
	   fin = new FileInputStream(source);  
	   fout = new FileOutputStream(target);  
	   gzout = new GZIPOutputStream(fout);  
	   byte[] buf = new byte[1024];  
	   int num;  
	   while ((num = fin.read(buf)) != -1) {  
	    gzout.write(buf, 0, num);  
	   }  
	  } finally {  
	   if (gzout != null)  
	    gzout.close();  
	   if (fout != null)  
	    fout.close();  
	   if (fin != null)  
	    fin.close();  
	  }  
	 }  
	  
	 /** 
	  * 解压文件 
	  *  
	  * @param source源文件 
	  * @param target目标文件 
	  * @throws IOException 
	  */  
	 public static void unZipFile(String source, String target)  
	   throws IOException {  
	  FileInputStream fin = null;  
	  GZIPInputStream gzin = null;  
	  FileOutputStream fout = null;  
	  try {  
	   fin = new FileInputStream(source);  
	   gzin = new GZIPInputStream(fin);  
	   fout = new FileOutputStream(target);  
	   byte[] buf = new byte[1024];  
	   int num;  
	   while ((num = gzin.read(buf, 0, buf.length)) != -1) {  
	    fout.write(buf, 0, num);  
	   }  
	  } finally {  
	   if (fout != null)  
	    fout.close();  
	   if (gzin != null)  
	    gzin.close();  
	   if (fin != null)  
	    fin.close();  
	  }  
	 }  
	 
	 /* 
      * 字节数组解压缩后返回字符串 
      */  
     public static String uncompressToString(byte[] b) {  
         if (b == null || b.length == 0) {  
             return null;  
         }  
         ByteArrayOutputStream out = new ByteArrayOutputStream();  
         ByteArrayInputStream in = new ByteArrayInputStream(b);  
   
         try {  
             GZIPInputStream gunzip = new GZIPInputStream(in);  
             byte[] buffer = new byte[256];  
             int n;  
             while ((n = gunzip.read(buffer)) >= 0) {  
                 out.write(buffer, 0, n);  
             }  
         } catch (IOException e) {  
             e.printStackTrace();  
         }  
         return out.toString();  
     }  
	 
	 public static void main(String[] args) {
		 try {
			unZipFile("D:/test.xlsx.zip", "D:/test.xlsx");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值