欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java压缩文件 博客分类: java 学习笔记 javazip 

程序员文章站 2024-03-24 17:19:46
...
package org.liufei.net.util;

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.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

/**
 * 
 * @author 刘飞
 * 
 */
public class JavaZip {
	private static final Logger logger = Logger.getLogger(JavaZip.class);

	/**
	 * 压缩文件file成zip文件zipFile
	 * 
	 * @param file
	 *            要压缩的文件
	 * @param zipFile
	 *            压缩文件存放地方
	 * @throws IOException
	 */

	public static void zip(File file, File zipFile) {

		ZipOutputStream output = null;
		try {
			output = new ZipOutputStream(new FileOutputStream(zipFile));
			/**
			 * 顶层目录开始
			 */
			zipFile(output, file, "");
		} catch (IOException ex) {
			logger.error("zip file [ " + file + " ] error.", ex);
			throw new RuntimeException("zip file [ " + file + " ] error.", ex);
		} finally {
			try {
				/**
				 * 关闭流
				 */
				if (output != null) {
					output.flush();
					output.close();
				}
			} catch (IOException e) {
				logger.error("close ZipOutputStream [ file : " + zipFile
						+ " ] error.", e);
				throw new RuntimeException("zip file [ " + zipFile
						+ " ] error.", e);
			}
		}
	}

	/**
	 * 压缩文件为zip格式
	 * 
	 * @param output
	 *            ZipOutputStream对象
	 * @param file
	 *            要压缩的文件或文件夹
	 * @param basePath
	 *            条目根目录
	 * @throws IOException
	 */
	private static void zipFile(ZipOutputStream output, File file,
			String basePath) throws IOException {
		FileInputStream input = null;
		try {
			/**
			 * 文件为目录
			 */
			if (file.isDirectory()) {
				/**
				 * 得到当前目录里面的文件列表
				 */
				File list[] = file.listFiles();
				basePath = basePath + (basePath.length() == 0 ? "" : "/")
						+ file.getName();
				/**
				 * 循环递归压缩每个文件
				 */
				for (File f : list)
					zipFile(output, f, basePath);
			} else {
				/**
				 * 压缩文件
				 */
				basePath = (basePath.length() == 0 ? "" : basePath + "/")
						+ file.getName();
				output.putNextEntry(new ZipEntry(basePath));
				input = new FileInputStream(file);
				int readLen = 0;
				byte[] buffer = new byte[1024 * 8];
				while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
					output.write(buffer, 0, readLen);
			}
		} catch (Exception ex) {
			logger.error("zip file [ " + file + " ] error.", ex);
			throw new RuntimeException("zip file [ " + file + " ] error.", ex);
		} finally {
			/**
			 * 关闭流
			 */
			if (input != null)
				input.close();
		}
	}

	/**
	 * 解压zip文件
	 * 
	 * @param zip
	 *            zip文件绝对路径
	 * @param unZipDir
	 *            解压到的目录
	 * @throws IOException
	 */
	public static void unZip(String zip, String unZipDir) throws IOException {
		unZip(new File(zip), new File(unZipDir)) ;
	}

	/**
	 * 解压zip文件
	 * 
	 * @param zip
	 *            zip文件
	 * @param unZipDir
	 *            解压到的目录文件
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public static void unZip(File zip, File unZipDir) throws IOException {
		ZipFile zipFile = new ZipFile(zip, "GBK");

		byte[] buffer = new byte[2048];
		int length = -1;

		OutputStream out = null;
		InputStream in = null;

		Enumeration<ZipEntry> zee = zipFile.getEntries();
		while (zee.hasMoreElements()) {
			ZipEntry zipEntry = zee.nextElement();
			File loadFile = new File(unZipDir, zipEntry.getName());

			if (zipEntry.isDirectory()) {
				loadFile.mkdirs();
			} else {
				if (!loadFile.getParentFile().exists()) {
					loadFile.getParentFile().mkdirs();
				}
				out = new FileOutputStream(loadFile, false);

				in = zipFile.getInputStream(zipEntry);

				while ((length = in.read(buffer)) > 0) {
					out.write(buffer, 0, length);
				}
			}
		}

		if (in != null) {
			in.close();
		}
		if (out != null) {
			out.flush();
			out.close();
		}
	}
}
相关标签: java zip