Java解压缩-ZIP

来源:互联网 发布:java windows linux 编辑:程序博客网 时间:2024/06/07 22:44

使用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
原创粉丝点击