2014-08-31 151 views
0

我想將包含圖像的zip文件複製到內部存儲,然後將其解壓縮。 這是我的代碼:解壓縮 - java.lang.IllegalArgumentException:文件的文件名/包含路徑分隔符

protected void copyFromAssetsToInternalStorage(String filename){ 
    AssetManager assetManager = getAssets(); 

    try { 
     InputStream input = assetManager.open(filename); 
     OutputStream output = openFileOutput(filename, Context.MODE_PRIVATE); 

     copyFile(input, output); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private void unZipFile(String filename){ 
    try { 
     ZipInputStream zipInputStream = new ZipInputStream(openFileInput(filename)); 
     ZipEntry zipEntry; 

     while((zipEntry = zipInputStream.getNextEntry()) != null){ 
      FileOutputStream zipOutputStream = openFileOutput(zipEntry.getName(), MODE_PRIVATE); 

      int length; 
      byte[] buffer = new byte[1024]; 

      while((length = zipInputStream.read(buffer)) > 0){ 
       zipOutputStream.write(buffer, 0, length); 
      } 

      zipOutputStream.close(); 
      zipInputStream.closeEntry(); 
     } 
     zipInputStream.close(); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private void copyFile(InputStream in, OutputStream out) throws IOException { 
    byte[] buffer = new byte[1024]; 
    int read; 
    while((read = in.read(buffer)) != -1){ 
     out.write(buffer, 0, read); 
    } 
} 

,我有此錯誤: java.lang.IllegalArgumentException異常:文件名/包含路徑分隔符

我應該怎麼辦?

+0

你應該做的:識別導致錯誤的行(行號是在日誌中,它可能是這樣的:'FileOutputStream中zipOutputStream = openFileOutput(zipEntry.getName() MODE_PRIVATE);');查看該行的變量值(使用調試器和該行的斷點),並調查爲什麼有一個帶有路徑分隔符的變量(即斜槓)。 (我想這是因爲它是一個ZipEntry名稱匹配的目錄條目而不是文件條目) – ben75 2014-08-31 10:29:27

回答

0

從openFileOutput documentation

名稱要打開的文件的名稱; 不能包含路徑分隔符。

希望這有助於 亞龍