【问题标题】:Android unzip open failed: ENOENT (No such file or directory)Android解压打开失败:ENOENT(没有这样的文件或目录)
【发布时间】:2013-10-21 01:55:55
【问题描述】:

我正在使用以下code 解压缩一组文件(也包含文件夹):

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 = new FileOutputStream(path + filename) 上的代码失败,错误为:

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

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

【问题讨论】:

    标签: android zip unzip


    【解决方案1】:

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

    添加写入外部存储权限

    使用权限 android:name="android.permission.WRITE_EXTERNAL_STORAGE"

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。经过几次调查,我发现了这一点。在您的代码中添加以下单行:

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

      【讨论】:

        【解决方案3】:

        有时在创建父目录之前提取文件已被提取,例如: 目录 B 中的文件 A。但未创建 B 目录,下面列出的文件索引导致问题:

        • dir_b/file_a.txt
        • dir_b/
        • dir_b/file_c.txt

        所以,要确定文件解压前创建的目录,需要先创建父目录,例如:

        val targetFile = File(tempOutputDir, zipEntry.name)
        if (zipEntry.isDirectory) {
            targetFile.mkdirs()
        } else {
            try {
                try {
                    targetFile.parentFile?.mkdirs()  // <-- ADD THIS LINE
                } catch (exception: Exception) {
                    Log.e("ExampleApp", exception.localizedMessage, exception)
                }
                val bufferOutputStream = BufferedOutputStream(
                    FileOutputStream(targetFile)
                )
                val buffer = ByteArray(1024)
                var read = zipInputStream.read(buffer)
                while (read != -1) {
                    bufferOutputStream.write(buffer, 0, read)
                    read = zipInputStream.read(buffer)
                }
                bufferOutputStream.close()
            } catch (exception: Exception) {
                Log.e("ExampleApp", exception.localizedMessage, exception)
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多