如何在java中實現對zip和rar檔案的解壓

來源:互聯網
上載者:User

一、解壓rar檔案。 
由於WinRAR 是共用軟體,並不是開源的,所以解壓rar檔案的前提是系統已經安裝了winrar,比如本人的安裝路徑是: 
C://Program Files//WinRAR//winrar.exe 
然後運用java.lang.Process 的相關知識來運行系統命令列來實現解壓的。 
winrar 命令列相關參數自己可以搜尋下的網上資料很多 
具體代碼: 

Java代碼 
  1. **  
  2.  * 解壓rar檔案(需要系統安裝Winrar 軟體)  
  3.  * @author Michael sun  
  4.  */  
  5. public class UnRarFile {  
  6.     /** 
  7.      * 解壓rar檔案 
  8.      *  
  9.      * @param targetPath 
  10.      * @param absolutePath 
  11.      */  
  12.     public void unRarFile(String targetPath, String absolutePath) {  
  13.   
  14.         try {  
  15.   
  16.             // 系統安裝winrar的路徑  
  17.             String cmd = "C://Program Files//WinRAR//winrar.exe";  
  18.             String unrarCmd = cmd + " x -r -p- -o+ " + absolutePath + " "  
  19.                     + targetPath;  
  20.   
  21.             Runtime rt = Runtime.getRuntime();  
  22.             Process pre = rt.exec(unrarCmd);  
  23.             InputStreamReader isr = new InputStreamReader(pre.getInputStream());  
  24.             BufferedReader bf = new BufferedReader(isr);  
  25.             String line = null;  
  26.             while ((line = bf.readLine()) != null) {  
  27.                 line = line.trim();  
  28.                 if ("".equals(line)) {  
  29.                     continue;  
  30.                 }  
  31.                 System.out.println(line);  
  32.             }  
  33.   
  34.             bf.close();  
  35.         } catch (Exception e) {  
  36.             System.out.println("解壓發生異常");  
  37.         }  
  38.     }  
  39.   
  40.     /** 
  41.      * @param args 
  42.      */  
  43.     public static void main(String[] args) {  
  44.         String targetPath = "D://test//unrar//";  
  45.         String rarFilePath = "D://test//test.rar";  
  46.         UnRarFile unrar = new UnRarFile();  
  47.         unrar.unRarFile(targetPath, rarFilePath);  
  48.     }  
  49.   
  50. }  

二、解壓zip檔案 
由於zip是免費的,所以在jdk裡提供了相應的類對zip檔案的實現: 
java.util.zip.*,詳細情況可以參考java API Java代碼 

  1. /** 
  2.  * 解壓zip檔案 
  3.  * @author Michael sun 
  4.  */  
  5. public class UnzipFile {  
  6.   
  7.     /** 
  8.      * 解壓zip檔案 
  9.      *  
  10.      * @param targetPath 
  11.      * @param zipFilePath 
  12.      */  
  13.     public void unzipFile(String targetPath, String zipFilePath) {  
  14.   
  15.         try {  
  16.             File zipFile = new File(zipFilePath);  
  17.             InputStream is = new FileInputStream(zipFile);  
  18.             ZipInputStream zis = new ZipInputStream(is);  
  19.             ZipEntry entry = null;  
  20.             System.out.println("開始解壓:" + zipFile.getName() + "...");  
  21.             while ((entry = zis.getNextEntry()) != null) {  
  22.                 String zipPath = entry.getName();  
  23.                 try {  
  24.   
  25.                     if (entry.isDirectory()) {  
  26.                         File zipFolder = new File(targetPath + File.separator  
  27.                                 + zipPath);  
  28.                         if (!zipFolder.exists()) {  
  29.                             zipFolder.mkdirs();  
  30.                         }  
  31.                     } else {  
  32.                         File file = new File(targetPath + File.separator  
  33.                                 + zipPath);  
  34.                         if (!file.exists()) {  
  35.                             File pathDir = file.getParentFile();  
  36.                             pathDir.mkdirs();  
  37.                             file.createNewFile();  
  38.                         }  
  39.   
  40.                         FileOutputStream fos = new FileOutputStream(file);  
  41.                         int bread;  
  42.                         while ((bread = zis.read()) != -1) {  
  43.                             fos.write(bread);  
  44.                         }  
  45.                         fos.close();  
  46.   
  47.                     }  
  48.                     System.out.println("成功解壓:" + zipPath);  
  49.   
  50.                 } catch (Exception e) {  
  51.                     System.out.println("解壓" + zipPath + "失敗");  
  52.                     continue;  
  53.                 }  
  54.             }  
  55.             zis.close();  
  56.             is.close();  
  57.             System.out.println("解壓結束");  
  58.         } catch (Exception e) {  
  59.             e.printStackTrace();  
  60.         }  
  61.   
  62.     }  
  63.   
  64.     /** 
  65.      * @param args 
  66.      */  
  67.     public static void main(String[] args) {  
  68.         String targetPath = "D://test//unzip";  
  69.         String zipFile = "D://test//test.zip";  
  70.         UnzipFile unzip = new UnzipFile();  
  71.         unzip.unzipFile(targetPath, zipFile);  
  72.     }  
  73.   
  74. }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.