java解壓

 

  1. public static String mmUnzip(String pSource, String pPath) { 
  2.  
  3.     System.out 
  4.             .println(" mmUnzip mm  " + pPath + "  UnzipmmUnzip  " + pPath); 
  5.      
  6.     String mmSource = pSource; 
  7.     String mmPath = pPath; 
  8.     File fileM = new File(mmSource); 
  9.     if (!fileM.exists()) { 
  10.         System.out.println(" not exits  "); 
  11.         return ""
  12.     } 
  13.  
  14.     File zipFile = new File(mmSource); 
  15.     if (!zipFile.exists()) { 
  16.         System.out.println(" not exits  zipFile "); 
  17.         return ""
  18.     } 
  19.  
  20.     // zipFile.delete(); 解壓後刪除原zip刪除 
  21.  
  22.     try { 
  23.         unZipFile(zipFile, mmPath); 
  24.     } catch (ZipException e) {// 解壓不成功 
  25.         zipFile.delete(); 
  26.         e.printStackTrace(); 
  27.         return ""
  28.     } catch (IOException e) { 
  29.         zipFile.delete(); 
  30.         e.printStackTrace(); 
  31.         return ""
  32.     } 
  33.  
  34.     // zipFile.delete(); 
  35.     return "ok"

 

  1. public static void unZipFile(File zipFile, String folderPath) 
  2.             throws ZipException, IOException { 
  3.  
  4.         File desDir = new File(folderPath); 
  5.         if (!desDir.exists()) { 
  6.             desDir.mkdirs(); 
  7.         } 
  8.  
  9.         ZipFile zf = new ZipFile(zipFile); 
  10.         for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {//枚舉裏面有幾個文件 
  11.             ZipEntry entry = ((ZipEntry) entries.nextElement()); 
  12.             // 後面插入判斷是否爲文件夾,如果是不處理 
  13.             String name = entry.getName(); 
  14.             if (name.endsWith(File.separator)) 
  15.                 continue
  16.  
  17.             InputStream in = zf.getInputStream(entry); 
  18.             String str = folderPath + File.separator + entry.getName(); 
  19.             str = new String(str.getBytes("8859_1"), "GB2312"); 
  20.             File desFile = new File(str); 
  21.             if (!desFile.exists()) { 
  22.                 File fileParentDir = desFile.getParentFile(); 
  23.                 if (!fileParentDir.exists()) { 
  24.                     fileParentDir.mkdirs(); 
  25.                 } 
  26.                 desFile.createNewFile(); 
  27.             } 
  28.             OutputStream out = new FileOutputStream(desFile); 
  29.             byte buffer[] = new byte[1024]; 
  30.             int realLength; 
  31.             while ((realLength = in.read(buffer)) > 0) { 
  32.                 out.write(buffer, 0, realLength); 
  33.             } 
  34.             in.close(); 
  35.             out.close(); 
  36.         } 
  37.     } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章