java - 解压缩时 FileOutputStream 抛出 FileNotFoundException

标签 java android file

我正在使用通过谷歌找到的类来解压缩 .zip 文件。.zip 包含文件和文件夹。问题是 FileOutputStream throws FileNotFoundException.. 但是文件应该从 .zip 文件中获取,所以它以前怎么可能存在?
这是我在 AsyncTask 中使用的代码:

@Override
protected Boolean doInBackground(String... params) {
    try {

        String zipFile = Path + FileName;

        FileInputStream fin = new FileInputStream(zipFile);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry()) != null) {

            if (ze.isDirectory()) {
                dirChecker(ze.getName());
            } else {
                FileOutputStream fout = new FileOutputStream(Path
                        + ze.getName()); // <-- crashes here
                while ((length = zin.read(buffer)) > 0) {
                    fout.write(buffer, 0, length);
                    publishProgress(length);

                }

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

        }
        zin.close();
    } catch (Exception e) {
        mProgressDialog.dismiss();
        return false;
    }

    return true;
  }

下载 .zip 的另一个 AsyncTask:

@Override
protected Boolean doInBackground(String... params) {
      try {
          URL url = new URL(params[0]);
          URLConnection conexion = url.openConnection();

          conexion.connect();

          int lenghtOfFile = conexion.getContentLength();
          File f = new File(Path+FileName);
          if(!f.exists())
          {
              f.mkdirs();
              if(!f.createNewFile())
              {
                  f.delete();
                  f.createNewFile();
              }
          }

          InputStream input = new BufferedInputStream(url.openStream());
          OutputStream output = new FileOutputStream(Path+FileName);

          byte data[] = new byte[1024];

          long total = 0;

          while ((count = input.read(data)) != -1) {
              total += count;
              publishProgress((int)((total*100)/lenghtOfFile));
              output.write(data, 0, count);
          }

          output.flush();
          output.close();
          input.close();
          return true;
      } catch (Exception e)     
      {
        e.printStackTrace();
        e.getCause();
        return false;
      }

我再次收到带有(是目录)消息错误的 FileNotFoundExcpetion!

最佳答案

如果涉及的目录不存在,

FileOutputStream 将抛出 FileNotFoundException。我在这里没有看到任何目录创建代码,甚至没有任何代码来检查 Path 是否存在,所以这可能就是正在发生的事情。

关于java - 解压缩时 FileOutputStream 抛出 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7353871/

相关文章:

java - 如何在 Spring Batch 中从 ItemReader 动态设置 MultiResourceItemWriter 的资源

java - 有 getter 时没有 Getter Method Error

c - fwrite 中的输出不符合预期

java - 如何使用 XMLStreamReader 多次读取同一 XML 文件

java - 如何使用 Retrofit2 处理包装在一个 JSON 对象中的 JSON 对象?

java - 必应翻译 API 无法正常工作

android - 通过 btn 单击启动 Android 默认电子邮件应用程序

android - 编译错误说资源名称必须以字母开头

c# - 在 C# 中有效地搜索关键字的目录

java - 重命名的类中的重复字段(ASM + Jar)