解压缩时,FileOutputStream会抛出FileNotFoundException

Oma*_*mar 6 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;
  }
Run Code Online (Sandbox Code Playgroud)

另一个AsyncTask下载.zip:

@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;
      }
Run Code Online (Sandbox Code Playgroud)

我再次得到FileNotFoundExcpetionwith(是一个目录)消息错误!

Ern*_*ill 14

FileOutputStreamFileNotFoundException如果涉及的目录不存在,则抛出.我在这里看不到任何目录创建代码,甚至没有任何代码可以检查是否Path存在,所以这可能是正在发生的事情.


小智 8

我再次使用(是目录)消息错误获取FileNotFoundException.

File f = new File(Path+FileName);
f.mkdirs();
Run Code Online (Sandbox Code Playgroud)

尝试使用

File directory = new File(Path);
directory.mkdirs();
Run Code Online (Sandbox Code Playgroud)

然后

File file = new File(directory, FileName);
Run Code Online (Sandbox Code Playgroud)

而不是你的代码.