package cn.liuc.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;

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

public class ZipByOriginalExtractUtil {

	public static ArrayList<String> unzipFile(String zipFilePath) {
		String path = zipFilePath.replaceAll("\\\\", "/");
		String outputDirectory = path.substring(0, path.lastIndexOf("."))+"/";
		return unZip(zipFilePath, outputDirectory);
	}

	/**
	 * 生存文件夹
	 * 
	 * @param directory
	 *            解压文件存放文件夹
	 * @param subDirectory
	 *            子文件夹(没有时可传入空字符串)
	 */
	private static void createDirectory(String directory, String subDirectory) {
		String dir[];
		File fl = new File(directory);
		try {
			if (subDirectory == "" && fl.exists() != true)
				fl.mkdir();
			else if (subDirectory != "") {
				dir = subDirectory.replace('\\', '/').split("/");
				for (int i = 0; i < dir.length; i++) {
					File subFile = new File(directory + File.separator + dir[i]);
					if (subFile.exists() == false)
						subFile.mkdir();
					directory += File.separator + dir[i];
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解压指定的ZIP文件
	 * 
	 * @param unZipFileName
	 *            文件名称字符串(包括路径)
	 * @param outputDirectory
	 *            解压后存放文件夹
	 * @return
	 */
	public static ArrayList<String> unZip(String unZipFileName,
			String outputDirectory) {
		ArrayList<String> unzipFileList = new ArrayList<String>();
		int readedBytes;
		FileOutputStream fileOutStream=null;
		byte[] buff = new byte[1024];
		// File file;
		InputStream inputStream=null;
		ZipFile zipFile = null;
		try {
			createDirectory(outputDirectory, "");
			if (System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0) {
				zipFile = new ZipFile(unZipFileName, "GBK");
			} else if (System.getProperty("os.name").toLowerCase()
					.indexOf("linux") >= 0) {
				zipFile = new ZipFile(unZipFileName, "UTF-8");
			}
			for (Enumeration<ZipEntry> entries = zipFile.getEntries(); entries
					.hasMoreElements();) {
				ZipEntry entry = (ZipEntry) entries.nextElement();
				if (entry.isDirectory()) {
					// 是文件夹。则创建之
					String name = entry.getName().substring(0,
							entry.getName().length() - 1);
					File f = new File(outputDirectory + File.separator + name);
					f.mkdir();
					// file.mkdirs();
				} else {
					// 是文件
					String fileName = entry.getName().replaceAll("\\\\", "/");
					String path = outputDirectory + "/" + fileName;
					File f = new File(path);
					f.createNewFile();
					inputStream = zipFile.getInputStream(entry);
					fileOutStream = new FileOutputStream(f);
					while ((readedBytes = inputStream.read(buff)) > 0) {
						fileOutStream.write(buff, 0, readedBytes);
					}
					fileOutStream.close();
					inputStream.close();
					unzipFileList.add(path);
				}
			}
			zipFile.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if (inputStream!=null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileOutStream!=null) {
				try {
					fileOutStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return unzipFileList;
	}

	/**
	 * 
	 * \brief 測试
	 * 
	 * @param args
	 * @throws Exception
	 * @attention 方法的使用注意事项
	 * @author liuchao
	 * @date 2013-9-13
	 * @note begin modify by 改动人 改动时间 改动内容摘要说明
	 */
	public static void main(String[] args) throws Exception {


		unzipFile("E:\\专家协同解决方式.zip");
	}

}

posted on 2017-07-05 09:49  yutingliuyl  阅读(361)  评论(0编辑  收藏  举报