2013-10-21 63 views
2

我使用以下code解压缩一组文件(包含文件夹以及):的Android解压打开失败:ENOENT(没有这样的文件或目录)

private boolean unpackZip(String path, String zipname) 
{  
    InputStream is; 
    ZipInputStream zis; 
    try 
    { 
     String filename; 
     is = new FileInputStream(path + zipname); 
     zis = new ZipInputStream(new BufferedInputStream(is));  
     ZipEntry ze; 
     byte[] buffer = new byte[1024]; 
     int count; 

     while ((ze = zis.getNextEntry()) != null) 
     { 
      // zapis do souboru 
      filename = ze.getName(); 

      // Need to create directories if not exists, or 
      // it will generate an Exception... 
      if (ze.isDirectory()) { 
       File fmd = new File(path + filename); 
       fmd.mkdirs(); 
       continue; 
      } 

      FileOutputStream fout = new FileOutputStream(path + filename); 

      // cteni zipu a zapis 
      while ((count = zis.read(buffer)) != -1) 
      { 
       fout.write(buffer, 0, count);    
      } 

      fout.close();    
      zis.closeEntry(); 
     } 

     zis.close(); 
    } 
    catch(IOException e) 
    { 
     e.printStackTrace(); 
     return false; 
    } 

    return true; 
} 

的代码失败的FileOutputStream中FOUT =新FileOutputStream中(路径+文件名)出现错误:

java.io.FileNotFoundException: /storage/emulated/0/BASEFOLDER/FOLDER1/FILE.png 

BASEFOLDER已经存在,这正是我想解压的文件夹。如果我手动(或以编程方式)创建FOLDER1,代码运行良好,并成功解压缩。我相信它会崩溃,因为第一个文件(ze)被命名为FOLDER1/FILE.png,而FOLDER1尚未创建。我如何解决这个问题?我知道其他人已经使用此代码,我发现它不太可能随机不适合我...

回答

0

你在AndroidManifest.xml文件中有这个吗?

添加写入到外部存储权限

使用许可权的android:NAME = “android.permission.WRITE_EXTERNAL_STORAGE”

0

我有同样的问题。经过多次调查,我发现。在您的代码中输入以下单行:

if (ze.isDirectory()) { 
     File fmd = new File(path + filename); 
     fmd.mkdirs(); 
     zis.closeEntry(); // <<<<<< ADD THIS LINE 
     continue; 
    } 
相关问题