Java解压缩-ZIP

使用jdk自带的Zip Util会产生中文乱码问题,所以直接使用Apache 的ant.jar进行解压缩zip文件,程序很简单

ant.jar的下载地址:http://ant.apache.org/bindownload.cgi

rar解压缩:http://blog.csdn.net/lohocc/article/details/41481075


package unzip;

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

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

/**
 * <p>
 * 	使用Apache的ant.jar解壓縮zip
 * </p>
 */
public class ZipFileUnZip {
	/**
	 * @param filePath zip文件路徑
	 */
	public static void unZipFile(String filePath){
		try {
			File file = new File(filePath);
			ZipFile zipFile = new ZipFile(file);
			String name = file.getName().replaceFirst(".zip", "");
			Enumeration<ZipEntry> files = zipFile.getEntries();
			//System.getProperty("sun.zip.encoding"); //ZIP编码方式
			//System.getProperty("sun.jnu.encoding"); //当前文件编码方式
			//System.getProperty("file.encoding");//當前文件內容編碼方式
			while(files.hasMoreElements()){
				ZipEntry entry = files.nextElement();
				String fileName = new String(entry.getRawName(),System.getProperty("sun.jnu.encoding"));
				File entryFile = new File("D:\\files\\"+name+"\\"+fileName);
				//如果為文件夾則建立/存在則跳過
				if(entry.isDirectory() && !(entryFile.exists())){
					entryFile.mkdirs();
					continue;
				}
				if(entry.isDirectory() && entryFile.exists()){
					continue;
				}
				if(!(entryFile.getParentFile().exists())){
					entryFile.getParentFile().mkdirs();
				}
				FileOutputStream out = new FileOutputStream(entryFile);
				InputStream fileIn = zipFile.getInputStream(entry);
				byte[] buff = new byte[1024];
				int c = 0;
				while((c=fileIn.read(buff)) != -1){
					out.write(buff,0,c);
	            }
				
				fileIn.close();
				out.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {
		//解壓縮文件
		unZipFile("D:\\files/apache-ant-1.9.4-bin.zip");
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值