压缩、解压文件工具类

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.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

/**
 * 压缩、解压文件工具类
 * 
 * @author  
 * 
 */
public class ZipUtil {

	/**
	 * 默认编编码方式
	 */
	private static final String ENCODING = "GBK";
	/**
	 * 默认压缩到的目录
	 */
	private static final String DEFALUT_DEST = "c:/dest.zip";

	/**
	 * 压缩文件
	 * 
	 * @param source
	 *            源路径,文件或者目录
	 * @param destinct
	 *            目标路径
	 * @throws IOException
	 */
	@SuppressWarnings("rawtypes")
	public static void zip(String source, String dest) throws IOException {
		List fileList = loadFilename(new File(source));
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
				new File(dest)));

		zos.setEncoding(ENCODING);
		byte[] buffere = new byte[8192];
		int length;
		BufferedInputStream bis;

		for (int i = 0; i < fileList.size(); i++) {
			File file = (File) fileList.get(i);
			zos.putNextEntry(new ZipEntry(getEntryName(source, file)));
			bis = new BufferedInputStream(new FileInputStream(file));

			while (true) {
				length = bis.read(buffere);
				if (length == -1)
					break;
				zos.write(buffere, 0, length);
			}
			bis.close();
			zos.closeEntry();
		}
		zos.close();
	}
	
	/**
	 * 压缩文件到默认的指定路径下
	 * 
	 * @param source
	 *            源路径,文件或者目录
	 * @throws IOException
	 */
	public static void zip(String source) throws IOException {
		zip(source, DEFALUT_DEST);
	}

	/**
	 * 解压ZIP文件
	 * 
	 * @param source
	 *            源路径
	 * @param dest
	 *            目标路径
	 * @throws IOException
	 */
	@SuppressWarnings("rawtypes")
	public static void unZip(String source, String dest) throws IOException {
		ZipFile zip = new ZipFile(source);
		Enumeration en = zip.getEntries();
		ZipEntry entry = null;
		byte[] buffer = new byte[8192];
		int length = -1;
		InputStream input = null;
		BufferedOutputStream bos = null;
		File file = null;

		while (en.hasMoreElements()) {
			entry = (ZipEntry) en.nextElement();
			if (entry.isDirectory()) {
				continue;
			}

			input = zip.getInputStream(entry);
			file = new File(dest, entry.getName());
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			bos = new BufferedOutputStream(new FileOutputStream(file));

			while (true) {
				length = input.read(buffer);
				if (length == -1)
					break;
				bos.write(buffer, 0, length);
			}
			bos.close();
			input.close();
		}
		zip.close();
	}

	/**
	 * 递归获得该文件下所有文件名(不包括目录名)
	 * 
	 * @param file
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private static List loadFilename(File file) {
		List filenameList = new ArrayList();
		if (file.isFile()) {
			filenameList.add(file);
		}
		if (file.isDirectory()) {
			for (File f : file.listFiles()) {
				filenameList.addAll(loadFilename(f));
			}
		}
		return filenameList;
	}

	/**
	 * 获得zip entry 字符串
	 * 
	 * @param base
	 * @param file
	 * @return
	 */
	private static String getEntryName(String base, File file) {
		File baseFile = new File(base);
		String filename = file.getPath();
		if (baseFile.getParentFile().getParentFile() == null) {
			return filename.substring(baseFile.getParent().length());
		}
		return filename.substring(baseFile.getParent().length() + 1);
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值