2014-08-31 73 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

名称要打开的文件的名称; 不能包含路径分隔符。

希望这有助于 亚龙

相关问题