.gz文件解压

GZ压缩文件解压,直接上代码:
package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
/**
 * 解压.gz文件
 * @author www
 *
 */
public class Gzip {
	
	public static final int BUFFER = 1024;
	public static final String EXT = ".gz";


	public static void main(String[] args) {
		try {
			deCompress("文件路径");
		} catch (Exception e) {
			throw new RuntimeException("解压文件失败!", e);
		}
	}
	
	/**
	 * 文件解压缩
	 * @param file
	 * @throws Exception 
	 */
	public static void deCompress(String fileName) throws Exception {
		File file = new File(fileName);
		System.out.println(file);
		decompressFile(file, false);
	}
	
	/**
	 * 文件解压缩,是否删除原文件
	 * @param file
	 * @param delete
	 * @throws Exception
	 */
	public static void decompressFile (File file,boolean delete) throws Exception{
		FileOutputStream fos = new FileOutputStream(file);
		FileInputStream fis = new FileInputStream(file.getPath().replace(EXT, ""));
		decompress(fis, fos);
		fis.close();
		fos.flush();
		fos.close();
		if(delete) {
			file.delete();
		}
	}
	
	/**
	 * 数据解压缩
	 * @param is
	 * @param os
	 * @throws Exception
	 */
	public static void decompress(InputStream is, OutputStream os) throws Exception {
		GZIPInputStream gis = new GZIPInputStream(is);
		int count;
		byte[] data = new byte[1024];
		while((count = gis.read(data,0,BUFFER))!=-1) {
			os.write(data,0,count);
		}
	}
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值