Java实现zip文件解压[到指定目录]

原创文章地址;http://blog.csdn.net/ljheee/article/details/52736091

  1. package com.ljheee.ziptool.core;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.nio.charset.Charset;  
  8. import java.util.Enumeration;  
  9. import java.util.zip.ZipEntry;  
  10. import java.util.zip.ZipFile;  
  11. /** 
  12.  * 实现文件[夹]解压 
  13.  * @author ljheee 
  14.  * 
  15.  */  
  16. public class UnZipFile {  
  17.   
  18.     /** 
  19.      * 解压到指定目录 
  20.      * @param zipPath   
  21.      * @param descDir  
  22.      */  
  23.     public static void unZipFiles(String zipPath, String descDir) throws IOException {  
  24.         unZipFiles(new File(zipPath), descDir);  
  25.     }  
  26.   
  27.     /** 
  28.      * 解压文件到指定目录 
  29.      * 解压后的文件名,和之前一致 
  30.      * @param zipFile   待解压的zip文件 
  31.      * @param descDir   指定目录 
  32.      */  
  33.     @SuppressWarnings("rawtypes")  
  34.     public static void unZipFiles(File zipFile, String descDir) throws IOException {  
  35.           
  36.         ZipFile zip = new ZipFile(zipFile,Charset.forName("GBK"));//解决中文文件夹乱码  
  37.         String name = zip.getName().substring(zip.getName().lastIndexOf('\\')+1, zip.getName().lastIndexOf('.'));  
  38.           
  39.         File pathFile = new File(descDir+name);  
  40.         if (!pathFile.exists()) {  
  41.             pathFile.mkdirs();  
  42.         }  
  43.           
  44.         for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {  
  45.             ZipEntry entry = (ZipEntry) entries.nextElement();  
  46.             String zipEntryName = entry.getName();  
  47.             InputStream in = zip.getInputStream(entry);  
  48.             String outPath = (descDir + name +"/"+ zipEntryName).replaceAll("\\*", "/");  
  49.               
  50.             // 判断路径是否存在,不存在则创建文件路径  
  51.             File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));  
  52.             if (!file.exists()) {  
  53.                 file.mkdirs();  
  54.             }  
  55.             // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压  
  56.             if (new File(outPath).isDirectory()) {  
  57.                 continue;  
  58.             }  
  59.             // 输出文件路径信息  
  60. //          System.out.println(outPath);  
  61.   
  62.             FileOutputStream out = new FileOutputStream(outPath);  
  63.             byte[] buf1 = new byte[1024];  
  64.             int len;  
  65.             while ((len = in.read(buf1)) > 0) {  
  66.                 out.write(buf1, 0, len);  
  67.             }  
  68.             in.close();  
  69.             out.close();  
  70.         }  
  71.         System.out.println("******************解压完毕********************");  
  72.         return;  
  73.     }  
  74.       
  75.     //测试  
  76.     public static void main(String[] args) {  
  77.         try {  
  78.             unZipFiles(new File("E:/Study/Java.zip"), "E:/Study/abc/");  
  79.         } catch (IOException e) {  
  80.             e.printStackTrace();  
  81.         }  
  82.     }  
  83.   
  84. }  
posted @ 2017-09-17 11:29  panzzi  阅读(765)  评论(0)    收藏  举报