解压时的FileOutputStream抛出FileNotFoundException异常抛出、异常、FileOutputStream、FileNotFoundException

由网友(淘宝名字)分享简介:我使用一个类,我通过谷歌找到解压缩的.zip文件。该.ZIP中包含的文件和文件夹。问题是,的FileOutputStream 抛出 FileNotFoundException异常 ..但文件应采取从.zip文件,这样怎么能previously存在吗?这里是code我使用了的AsyncTask :I'm usin...

我使用一个类,我通过谷歌找到解压缩的.zip文件。该.ZIP中包含的文件和文件夹。问题是,的FileOutputStream 抛出 FileNotFoundException异常 ..但文件应采取从.zip文件,这样怎么能previously存在吗? 这里是code我使用了的AsyncTask

I'm using a class that I found through google to unzip a .zip file.. The .zip contains files and folder. The problem is that FileOutputStream throws FileNotFoundException.. But the file should be taken from the .zip file so how can it previously exist? Here is the code I'm using in the 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;
  }

另一个的AsyncTask ,这将下载的.zip:

Another AsyncTask which downloads the .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;
      }

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

I get again the FileNotFoundExcpetion with (is a directory) message error!.

推荐答案

的FileOutputStream 将抛出 FileNotFoundException异常如果目录(多个)涉及不存在。我没有看到任何目录创建code在这里,甚至任何code,以检查是否路径的存在,所以这可能是发生了什么事情。

FileOutputStream will throw FileNotFoundException if the directory(s) involved don't exist. I don't see any directory-creation code here, or even any code to check if Path exists, so that's probably what's going on.

阅读全文

相关推荐

最新文章