日期:2014-05-17  浏览次数:20761 次

java解压zip,windows环境下测试过
package com.file.compress;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


/**
 * zip 解压
 * 
 * @author pansaobin
 * 
 */
public class UnZip {

	/**
	 * @param args
	 */
	static final int BUFFER = 2048;
	
	public static void uncompress(String pathName,String fileName) throws Exception{
		
		if (!pathName.endsWith("/")) {
			pathName += "/";
		}
		String totalName = pathName + fileName;
		System.out.println("fileName: "+ totalName);
		ZipFile zipFile = new ZipFile(totalName);
		Enumeration enu = zipFile.entries();

		while (enu.hasMoreElements()) {
			ZipEntry zipEntry = (ZipEntry) enu.nextElement();
			if (zipEntry.isDirectory()) {
				new File(totalName + zipEntry.getName()).mkdirs();
				continue;
			}
			BufferedInputStream bis = new BufferedInputStream(zipFile
					.getInputStream(zipEntry));
			File file = new File(totalName + zipEntry.getName());
			File parent = file.getParentFile();
			if (parent != null && !parent.exists()) {
				parent.mkdirs();
			}
			FileOutputStream fos = new FileOutputStream(file);
			BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

			int count;
			byte[] array = new byte[BUFFER];
			while ((count = bis.read(array, 0, BUFFER)) != -1) {
				bos.write(array, 0, BUFFER);
			}
			bos.flush();
			bos.close();
			bis.close();
		}
	}
	
	
	
	public static void main(String[] args) {
		try {
			//String filename = "E:/test/commons-collections-3.1.jar.zip";
			// 下载到本地的哪个文件夹
			/*String uploadDirectory = BaoBiaoProperties
					.getProperty("uploadDirectory");
			if (!uploadDirectory.endsWith("/")) {
				uploadDirectory += "/";
			}
			// 下载到的本地路径
			String path = System.getProperty("user.dir") + uploadDirectory;*/
			
			
			String pathName = "G:/test";
			//String fileName = "E:/test/commons-collections-3.1.jar.zip";
			String fileName = "a.zip";		
			uncompress(pathName,fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}