2011-01-29 108 views
1

我想解壓縮iPhone應用程序.ipa文件。 這實際上是正常提取的zip文件。 但是其中的實際應用程序文件是以.app結尾的文件夾(因爲所有的mac應用程序實際上都是以.app結尾的文件夾)。 現在這個時期似乎是java.util.zip的一個問題。java.util.zip在文件名/目錄名稱中存在句點問題?

public static void main(String[] args) throws IOException { 
    ZipFile zipFile = new ZipFile("file.zip"); 
    String path = ""; 

    Enumeration files = zipFile.entries(); 

    while (files.hasMoreElements()) { 
     ZipEntry entry = (ZipEntry) files.nextElement(); 
     if (entry.isDirectory()) { 
      File file = new File(path + entry.getName()); 
      file.mkdir(); 
      System.out.println("Create dir " + entry.getName()); 
     } else { 
      File f = new File(entry.getName()); 
      FileOutputStream fos = new FileOutputStream(f); //EXception occurs here 
      InputStream is = zipFile.getInputStream(entry); 
      byte[] buffer = new byte[1024]; 
      int bytesRead = 0; 
      while ((bytesRead = is.read(buffer)) != -1) { 
       fos.write(buffer, 0, bytesRead); 
      } 
      fos.close(); 
      System.out.println("Create File " + entry.getName()); 
     } 
    } 
} 

這是我的輸出:

Exception in thread "main" java.io.FileNotFoundException: Payload/SMA Jobs.app/06-magnifying-glass.png (No such file or directory) 
    at java.io.FileOutputStream.open(Native Method) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:179) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:131) 
    at Main.main(Main.java:27) 
    enter code here 

任何人知道如何處理這些時間?

回答

2

首先,你應該使用mkdirs(),而不是mkdir()。

秒,zip文件並不總是包含所有的目錄條目(或使它們按正確的順序)。最好的做法是在代碼中的兩個分支的目錄,所以加:

} else { 
     File f = new File(entry.getName()); 
     f.getParent().mkdirs(); 

(你應該增加一些檢查,以確保的getParent()不爲空,等等)。

+0

你的意思是getParentFile()我猜? – tzippy 2011-01-29 16:18:37

0

我不認爲這個時期是問題所在。看看你正試圖輸出的文件的絕對路徑,並確保它指向正確的位置。

0
if (entry.isDirectory()) { 
      File file = new File(path + entry.getName()); 
.... 
} else { 
      File f = new File(entry.getName()); 
.... 

在創建目錄,通過文件路徑爲路徑+ entry.getName() 但在創建文件,通過文件路徑爲entry.getName()

更改爲路徑+入口文件路徑之後。 getName(),代碼適用於句點文件名和普通文件名。 :)

相關問題