`

java ZIP压缩工具类

    博客分类:
  • java
阅读更多
package com.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * 
 * 类 <code>ZipUtil</code>zip压缩及解压工具类
 * 
 * @author suqun
 * @version 2014-4-10
 */
public class ZipUtil {

	/**
	 * 解压zip文件
	 * 
	 * @param filePath
	 *            原解压文件路径
	 * @param fileName
	 *            原解压文件名
	 * @param targetPath
	 *            解压后存放路径
	 *@throws Exception
	 * 
	 */
	public static void decompression(String filePath, String fileName, String targetPath) throws Exception{
		long startTime = System.currentTimeMillis();
		//定义压缩输入流,输入源zip路径
		ZipInputStream Zin = new ZipInputStream(new FileInputStream(filePath
				+ "/" + fileName));
		// 定义输入流,读取每一个ZipEntry
		BufferedInputStream Bin = new BufferedInputStream(Zin);
		// 输出文件的时候要有文件夹的操作  
		File Fout = null;
		// 每一个压缩实体
		ZipEntry entry;
		
		while ((entry = Zin.getNextEntry()) != null && !entry.isDirectory()) {// 得到一个压缩实体  
			// 定义输出的文件路径
			Fout = new File(targetPath, entry.getName());
			// 如果输出文件夹不存在  创建文件夹  
			if (!Fout.exists()) {
				(new File(Fout.getParent())).mkdirs();
			}
			// 实例化文件输出流  
			FileOutputStream out = new FileOutputStream(Fout);
			BufferedOutputStream Bout = new BufferedOutputStream(out);
			int b;
			while ((b = Bin.read()) != -1) {
				Bout.write(b);
			}
			Bout.close();
			out.close();
			System.out.println(Fout + "解压成功");
		}
		Bin.close();
		Zin.close();
		long endTime = System.currentTimeMillis();
		System.out.println("耗费时间: " + (endTime - startTime) + " ms");
	}
	
	/**
	 * 
	 * 压缩成zip文件
	 * 
	 * @param filePath 待压缩文件路径
	 * @param fileName 待压缩文件名称
	 * @param zipPath 压缩后文件路径
	 * @param zipName 压缩后文件名称
	 * @throws Exception
	 * 仅适用于文件压缩,没有对文件夹进行递归压缩
	 */
	public static void compression(String filePath,String fileName,String zipPath,String zipName) throws Exception {  
        System.out.println("压缩中...");  
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath+'/'+zipName));  
        File inputFile = new File(filePath+"/"+fileName);
        BufferedOutputStream bo = new BufferedOutputStream(out);  
        // 创建zip压缩进入点
        out.putNextEntry(new ZipEntry(inputFile.getName()));   
        System.out.println(inputFile.getName()); 
        
        FileInputStream in = new FileInputStream(inputFile);  
        BufferedInputStream bi = new BufferedInputStream(in);  
        int b;  
        while ((b = bi.read()) != -1) {  
            bo.write(b); // 将字节流写入当前zip目录  
        }  
        bi.close();  
        in.close(); // 输入流关闭  
        bo.close();  
        out.close(); // 输出流关闭  
        System.out.println("压缩完成");  
    }  
	 
	public static void main(String[] args) throws Exception {
		//decompression("D:/bill", "998310010000003_ZF_20140408_00.zip", "D:/bill");
		compression("D:/bill","I_D_20140408_00_998310010000003_ZF.txt","e:/","I_D_20140408_00_998310010000003_ZF.zip");
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics