java zip压缩解压 工具类

/**
 * 创建于:2015年11月16日 上午10:35:33
 * 所属项目:
 * 文件名称:ZipUtil.java
 * 作者:test
 * 版权信息:
 */
package zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

	public static void main(String[] args) {
		unzip("E:/Exs.zip", "E:/Exstxt");
		//zip("E:/Exs3.zip", "E:/申请.docx");
	}

	/**
	 * 解压zip文件
	 * @param needZipFilePath 需要解压的zip文件名
	 * @param unzipFolerPath 解压的目标路径
	 */
	public static void unzip(String needZipFilePath, String unzipFolerPath) {
		ZipInputStream zipInputStream = null;
		try {
			File unzipFoler = new File(unzipFolerPath);
			if (!unzipFoler.exists()) {
				unzipFoler.mkdirs();
			}

			zipInputStream = new ZipInputStream(new FileInputStream(needZipFilePath));
			ZipEntry zipEntry = null;
			byte[] ch = new byte[1024];
			while ((zipEntry = zipInputStream.getNextEntry()) != null) {
				File unzipFile = new File(unzipFoler, zipEntry.getName());
				if (zipEntry.isDirectory()) {
					if (!unzipFile.exists()) {
						unzipFile.mkdirs();
					}
				} else {
					FileOutputStream fouts = null;
					try {
						fouts = new FileOutputStream(unzipFile);
						int i;
						while ((i = zipInputStream.read(ch)) != -1) {
							fouts.write(ch, 0, i);
						}
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					} finally {
						if (fouts != null) {
							try {
								fouts.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					}
				}
				zipInputStream.closeEntry();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (zipInputStream != null) {
				try {
					zipInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 压缩成zip文件
	 * @param zipFileName 压缩后的zip文件名
	 * @param inputFileName 待压缩的文件或者文件夹名
	 */
	public static void zip(String zipFileName, String inputFileName) {
		zip(zipFileName, new File(inputFileName));
	}

	/**
	 * 压缩成zip文件
	 * @param zipFileName 压缩后的zip文件名
	 * @param inputFile 待压缩的文件
	 */
	public static void zip(String zipFileName, File inputFile) {
		ZipOutputStream out = null;
		try {
			out = new ZipOutputStream(new FileOutputStream(zipFileName));
			zip(out, inputFile, "");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(out != null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 压缩成zip文件
	 * @param outputStream ZipOutputStream
	 * @param file 待压缩文件
	 * @param base 待压缩文件相对路径
	 * @throws IOException 操作outputStream时发生IO异常
	 */
	public static void zip(ZipOutputStream outputStream, File file, String base) throws IOException {
		if (file.isDirectory()) {
			File[] fl = file.listFiles();
			if(base.length() > 0){
				outputStream.putNextEntry(new ZipEntry(base + "/"));
			}
			base = base.length() == 0 ? "" : base + "/";
			for (int i = 0; i < fl.length; i++) {
				zip(outputStream, fl[i], base + fl[i].getName());
			}
		} else {
			base = base.length() == 0 ? file.getName() : base;
			outputStream.putNextEntry(new ZipEntry(base));
			FileInputStream inputStream = null;
			try {
				inputStream = new FileInputStream(file);
				int b;
				while ((b = inputStream.read()) != -1) {
					outputStream.write(b);
				}
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally{
				if(inputStream != null){
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值