Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileOutputStream throws FileNotFoundException when UnZipping

Tags:

java

file

android

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;
  }

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;
      }

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

like image 234
Omar Avatar asked Sep 08 '11 20:09

Omar


People also ask

Does FileOutputStream overwrite?

By default, FileOutputStream creates new file or overwrite when we try to write into a file. If you want to append with the existing content, then you have to use “append” flag in the FileOutputStream constructor.

What is true about FileOutputStream in Java?

FileOutputStream is an outputstream for writing data/streams of raw bytes to file or storing data to file. FileOutputStream is a subclass of OutputStream. To write primitive values into a file, we use FileOutputStream class.

Where does FileOutputStream write to?

FileOutputStream(File file) Creates a file output stream to write to the file represented by the specified File object. FileOutputStream(File file, boolean append) Creates a file output stream to write to the file represented by the specified File object.

How do you create a FileOutputStream file in Java?

FileOutputStream fout=new FileOutputStream(“file. txt”); Reading data from DataInputStream: The next step is to read data from DataInputStream and write it into FileOutputStream .


2 Answers

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.

like image 191
Ernest Friedman-Hill Avatar answered Nov 02 '22 04:11

Ernest Friedman-Hill


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

File f = new File(Path+FileName);
f.mkdirs();

Try using

File directory = new File(Path);
directory.mkdirs();

and then

File file = new File(directory, FileName);

instead of your code.

like image 41
yseven Avatar answered Nov 02 '22 04:11

yseven