package org.bjit.utils; 


import java.io.BufferedInputStream; 

import java.io.File; 

import java.io.FileInputStream; 

import java.io.FileOutputStream; 

import java.io.InputStream; 

import java.util.Enumeration; 


import org.apache.tools.zip.ZipEntry; 

import org.apache.tools.zip.ZipFile; 

import org.apache.tools.zip.ZipOutputStream; 


public class ZipUtil { 


 /** 

 * 功能:压缩成zip 

 * 

 * @param zipFilep 

 * 压缩后的zip文件名 

 * @param path 

 * 压缩路径 

 * @throws java.lang.Exception 

 */ 

 public boolean zip(String zipFilep, String path) { 

 //System.out.println("压缩文件开始....................."); 

 File zipFile = new File(zipFilep); 

 try { 

 if (!zipFile.exists()) { 

 zipFile.createNewFile(); 

 } 

 ZipOutputStream out = new ZipOutputStream(new FileOutputStream( 

 zipFile)); 

 write(out, path, ""); 

 out.close(); 

 //System.out.println("压缩文件结束!"); 

 } catch (Exception e) { 

 e.printStackTrace(); 

 return false; 

 } 

 return true; 

 } 


 /** 

 * 功能:写压缩流 

 * 

 * @param out 

 * 压缩输出流 

 * @param path 

 * 压缩路径 

 * @param base 

 * 压缩式的基础目录 

 * @throws java.lang.Exception 

 */ 


 private void write(ZipOutputStream out, String path, String base) 

 throws Exception { 

 File file = new File(path); 

 if (file.isDirectory()) {// 文件夹,递归 

 base = base.length() == 0 ? "" : base + File.separator; 

 File[] tempFiles = file.listFiles(); 

 for (int i = 0; i < tempFiles.length; i++) { 

 write(out, tempFiles[i].getPath(), base 

 + tempFiles[i].getName()); 

 } 

 } else {// 文件,压缩 

 byte[] buff = new byte[2048]; 

 int bytesRead = -1; 

 ZipEntry entry = new ZipEntry(base); 

 out.putNextEntry(entry); 

 InputStream in = new BufferedInputStream(new FileInputStream(file)); 

 while (-1 != (bytesRead = in.read(buff, 0, buff.length))) { 

 out.write(buff, 0, bytesRead); 

 } 

 in.close(); 

 out.flush(); 

 } 

 } 


 /** 

 * 功能:解压缩 

 * 

 * @param zipFilename 

 * zip文件 

 * @param destPath 

 * 解压缩路径 

 * @throws java.lang.Exception 

 */ 

 public void unZip(String zipFilename, String destPath) throws Exception { 

 System.out.println("解压文件开始....................."); 

 ZipFile zip = new ZipFile(zipFilename, "GB2312"); 

 Enumeration enu = zip.getEntries();// 得到压缩文件夹中的所有文件 

 while (enu.hasMoreElements()) { 

 ZipEntry entry = (ZipEntry) enu.nextElement(); 

 String file = destPath + entry.getName(); 

 write(zip, entry, file); 

 } 

 System.out.println("解压文件结束!"); 

 } 


 /** 

 * 功能:将解压缩之后的文件写入目录 

 * 

 * @param zip 

 * 压缩文件 

 * @param entry 

 * 压缩文件实体——压缩文件中的文件 

 * @param fileLocation 

 * 解压缩之后的文件路径 

 * @throws Exception 

 */ 

 private void write(ZipFile zip, ZipEntry entry, String file) 

 throws Exception { 

 if (entry.isDirectory()) { 

 File f = new File(file); 

 f.mkdirs(); 

 } else { 

 File f = new File(file); 

 createDir(f); 

 FileOutputStream fos = new FileOutputStream(f); 

 byte[] buffer = new byte[8196]; 

 InputStream is = zip.getInputStream(entry); 

 for (int len = is.read(buffer, 0, buffer.length); len != -1; len = is 

 .read(buffer, 0, 8196)) { 

 fos.write(buffer, 0, len); 

 } 

 fos.close(); 

 } 

 } 


 /** 

 * 功能:创建目录 

 * 

 * @param file 

 * 文件或目录 

 */ 

 private void createDir(File file) { 

 if (file.isDirectory() && !file.exists()) { 

 file.mkdirs(); 

 } else { 

 String path = file.getPath(); 

 int i = path.lastIndexOf(File.separator); 

 path = path.substring(0, i); 

 new File(path).mkdirs(); 

 } 

 } 


 /** 

 * 功能:测试样例 

 * 

 * @param args 

 */ 

 public static void main(String[] args) { 

 try { 

 // zip("d:\\upload.zip", "d:\\upload"); 

 // unZip("d:\\upload.zip", "e:\\"); 

 } catch (Exception e) { 

 e.printStackTrace(); 

 } 

 } 

}