ZipUtil

package com.mm.util.zip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.utils.IOUtils;

public class ZipUtil {

	/**
	 * 把多个文件或者文件夹压缩成zip
	 * 
	 * @param files
	 *            文件集合
	 * @param zipFile
	 *            压缩后文件
	 */
	public static void compress(File[] files, File zipFile) {
		if (files.length == 0) {
			return;
		}
		try {
			ZipArchiveOutputStream zipos = new ZipArchiveOutputStream(zipFile);
			zipos.setUseZip64(Zip64Mode.AsNeeded);
			// 将每个文件用ZipArchiveEntry封装
			for (File file : files) {
				if (file == null) {
					continue;
				}
				compressOneFile(file, zipos, "");
			}
			if (zipos != null) {
				zipos.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 压缩文件或者文件夹
	 * 
	 * @param srcFile
	 *            压缩文件
	 * @param destFile
	 *            压缩后文件
	 * @throws IOException
	 */
	public static void compress(File srcFile, File destFile) {
		ZipArchiveOutputStream zipos = null;
		try {
			zipos = new ZipArchiveOutputStream(new BufferedOutputStream(
					new FileOutputStream(destFile), 1024));
			compressOneFile(srcFile, zipos, "");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				zipos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 压缩文件或者文件夹
	 * 
	 * @param srcPath
	 *            源文件路径
	 * @param destPath
	 *            压缩文件路径
	 */
	public static void compress(String srcPath, String destPath) {
		File srcFile = new File(srcPath);
		File destFile = new File(destPath);
		compress(srcFile, destFile);
	}

	/**
	 * 压缩单个文件
	 * 
	 * @param srcFile
	 *            源文件
	 * @param zipos
	 *            压缩文件的输出流
	 * @param dir
	 *            在压缩包中的位置
	 * @throws IOException
	 */
	private static void compressOneFile(File srcFile,
			ZipArchiveOutputStream zipos, String dir) throws IOException {
		// 对文件夹进行处理。
		if (srcFile.isDirectory()) {
			ZipArchiveEntry entry = new ZipArchiveEntry(dir + srcFile.getName()
					+ File.separator);
			zipos.putArchiveEntry(entry);
			zipos.closeArchiveEntry();
			// 循环文件夹中的所有文件进行压缩处理。
			String[] subFiles = srcFile.list();
			for (String subFile : subFiles) {
				File file = new File(srcFile.getPath() + File.separator
						+ subFile);
				String subDir = dir + srcFile.getName() + File.separator;
				compressOneFile(file, zipos, subDir);
			}
		} else {
			InputStream is = null;
			try {
				is = new BufferedInputStream(new FileInputStream(srcFile));
				ZipArchiveEntry entry = new ZipArchiveEntry(srcFile, dir
						+ srcFile.getName());
				zipos.putArchiveEntry(entry);
				IOUtils.copy(is, zipos);
				zipos.closeArchiveEntry();
			} finally {
				if (is != null) {
					is.close();
				}
			}
		}
	}

	/**
	 * 解压zip包下所有文件
	 * 
	 * @param file
	 *            zip文件
	 * @param dir
	 *            解压后目录
	 * @throws IOException
	 */
	public static void decompressZip(File file, String dir) throws IOException {
		ZipFile zipFile = new ZipFile(file);
		InputStream is = null;
		OutputStream os = null;
		try {
			for (Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); entries
					.hasMoreElements();) {
				ZipArchiveEntry zipEntry = entries.nextElement();
				// 不存在则创建目标文件夹。
				File targetFile = new File(dir, zipEntry.getName());
				// 遇到根目录时跳过。
				if (zipEntry.getName().endsWith("/")) {
					continue;
				}
				// 如果文件夹不存在,创建文件夹。
				if (!targetFile.getParentFile().exists()) {
					targetFile.getParentFile().mkdirs();
				}
				is = zipFile.getInputStream(zipEntry);
				os = new FileOutputStream(targetFile);
				IOUtils.copy(is, os);
			}
		} finally {
			if (is != null) {
				is.close();
			}
			if (os != null) {
				os.close();
			}
			zipFile.close();
		}
	}

	/**
	 * 解压zip包下某个文件
	 * 
	 * @param file
	 *            zip文件
	 * @param fileName
	 *            文件名
	 * @param dir
	 *            解压后存放目录
	 * @throws IOException
	 */
	public static void decompressZip(File file, String fileName, String dir)
			throws IOException {
		// 不存在则创建目标文件夹。
		File targetFile = new File(dir, fileName);
		if (!targetFile.getParentFile().exists()) {
			targetFile.getParentFile().mkdirs();
		}
		ZipFile zipFile = new ZipFile(file);
		Enumeration<ZipArchiveEntry> zips = zipFile.getEntries();
		ZipArchiveEntry zip = null;
		InputStream is = null;
		OutputStream os = null;
		while (zips.hasMoreElements()) {
			zip = zips.nextElement();
			if (fileName.equals(zip.getName())) {
				try {
					is = zipFile.getInputStream(zip);
					os = new FileOutputStream(targetFile);
					IOUtils.copy(is, os);
				} finally {
					if (is != null) {
						is.close();
					}
					if (os != null) {
						os.close();
					}
					zipFile.close();
				}
			}
		}
	}
	
	public static void main(String[] args) {

		System.out.println("oj");
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值