10

I got following code from net and it looks everything is proper but i'm getting File not found exception...

I have a file called NewForestPonies.epub in sdcard

Permission :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

CODE:

    String ZipFileLocation=Environment.getExternalStorageDirectory()+"/NewForestPonies.epub";
    String unZipFileLocation=Environment.getExternalStorageDirectory()+"/DEST/";
    Decompress decomp=new Decompress(ZipFileLocation, unZipFileLocation, "zip");
    decomp.run(); 



 public Decompress(String zipFile, String location,String t) { 
    super(t);
    _zipFile = zipFile; 
    _location = location; 
} 
public void run() {
    FileInputStream fin=null;
    ZipInputStream zin=null;
    File file =null; 
    ZipEntry ze ;
    FileOutputStream fout=null;
    try{ 
        System.out.println(_zipFile );
        System.out.println(_location);
        fin = new FileInputStream(_zipFile); 
        zin = new ZipInputStream(fin); 
        ze= null; 
        byte[] buffer = new byte[1024];
        int length;
        while ((ze = zin.getNextEntry()) != null) { 
            file = new File((_location +"/" + ze.getName()));
            file.getParentFile().mkdirs();
             fout= new FileOutputStream(_location + ze.getName()); 
            while ((length = zin.read(buffer))>0) {
                fout.write(buffer, 0, length);
            }
            zin.closeEntry(); 
            fout.close();        
        }
        //MyDownloadListener.progress=70;
        zin.close();   
    }catch(Exception e) { 
        Log.e("Decompress", "unzip", e); 
    }  
    finally {

            try {
                fin.close();
                zin.close();
                fout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }




    }

} 

ERRROR:

    03-20 15:49:15.909: ERROR/Decompress(9479): java.io.FileNotFoundException: /mnt/sdcard/DEST/NewForestPonies/iTunesMetadata.plist (Not a directory)
03-20 15:49:15.909: ERROR/Decompress(9479):     at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
03-20 15:49:15.909: ERROR/Decompress(9479):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:239)
03-20 15:49:15.909: ERROR/Decompress(9479):     at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
03-20 15:49:15.909: ERROR/Decompress(9479):     at java.io.FileOutputStream.<init>(FileOutputStream.java:77)
03-20 15:49:15.909: ERROR/Decompress(9479):     at com.AndroidExplorer.Decompress.run(Decompress.java:42)
03-20 15:49:15.909: ERROR/Decompress(9479):     at com.AndroidExplorer.DecompressActivity.onCreate(DecompressActivity.java:23)
03-20 15:49:15.909: ERROR/Decompress(9479):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-20 15:49:15.909: ERROR/Decompress(9479):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
03-20 15:49:15.909: ERROR/Decompress(9479):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
03-20 15:49:15.909: ERROR/Decompress(9479):     at android.app.ActivityThread.access$1500(ActivityThread.java:122)
03-20 15:49:15.909: ERROR/Decompress(9479):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
03-20 15:49:15.909: ERROR/Decompress(9479):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 15:49:15.909: ERROR/Decompress(9479):     at android.os.Looper.loop(Looper.java:132)
03-20 15:49:15.909: ERROR/Decompress(9479):     at android.app.ActivityThread.main(ActivityThread.java:4028)
03-20 15:49:15.909: ERROR/Decompress(9479):     at java.lang.reflect.Method.invokeNative(Native Method)
03-20 15:49:15.909: ERROR/Decompress(9479):     at java.lang.reflect.Method.invoke(Method.java:491)
03-20 15:49:15.909: ERROR/Decompress(9479):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
03-20 15:49:15.909: ERROR/Decompress(9479):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-20 15:49:15.909: ERROR/Decompress(9479):     at dalvik.system.NativeStart.main(Native Method)
vnshetty
  • 20,051
  • 23
  • 64
  • 102
  • check you have file in this path /mnt/sdcard/EPUB/META-INF/container.xml – Samir Mangroliya Mar 13 '12 at 05:14
  • @Samir i have only zip filee.. do i need to create destination folders and files manually? its done in code right? – vnshetty Mar 13 '12 at 05:16
  • Do you have the right to write on the sd card? – Jerome Mar 13 '12 at 05:29
  • What your error log is suggesting is you do not have file at the correct path. See you provide the correct path for file, and see whether the path exist or not.Also see http://stackoverflow.com/questions/7697466/unzip-a-zipped-file-on-sd-card-in-android-application – Android Mar 13 '12 at 05:47

6 Answers6

4

I think the problem in your code is you are closing ZipInputStream zin in 1st while loop.

Use below code for run(), it may help you.

public void run() {
    BufferedOutputStream bufferedOutputStream = null;
    FileInputStream fileInputStream;

    File dest_file = new File(_location);
    dest_file.mkdirs(); // creates if destination directory not existed    

    try {
        fileInputStream = new FileInputStream(_zipFile);
        ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
        ZipEntry zipEntry;

        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            String zipEntryName = zipEntry.getName();
            File file = new File(_location + zipEntryName);

            if (file.exists()) {

            } else if (zipEntry.isDirectory()) {
                file.mkdirs();
            } else {
                byte buffer[] = new byte[1024];
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                bufferedOutputStream = new BufferedOutputStream(fileOutputStream, 1024);
                int count;

                while ((count = zipInputStream.read(buffer, 0, 1024)) != -1) {
                    bufferedOutputStream.write(buffer, 0, count);
                }
                bufferedOutputStream.flush();
                bufferedOutputStream.close();
            }
        }
        zipInputStream.close();
    } catch (Exception e) {
        Log.e("Decompress", "unzip", e);
    }
}
Lonergan6275
  • 1,938
  • 6
  • 32
  • 63
Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
2

Could you try creating new file first?

file = new File((_location +"/" + ze.getName()));
file.getParentFile().mkdirs();
if (!file.isFile())
    file.createNewFile();
...
0

I am guessing that the ZIP file you want to decompress is located at /mnt/sdcard/EPUB/, but in your code you are trying to access the container.xml that is probably resident in META-INF/ directory of the ZIP file (I don't have the file, so it's mostly guess here).

So what you should do is pass the location of the ZIP file (e.g. /mnt/sdcard/EPUB/book1.epub), like this:

Decompress("/mnt/sdcard/EPUB/book1.epub", "/mnt/sdcard/EPUB",t) 

After which you can open the uncompressed container.xml with your own code at /mnt/sdcard/EPUB/META-INF/container.xml

Kai
  • 15,284
  • 6
  • 51
  • 82
  • not working..do i need to create destination folders and files manually? its done through code right? – vnshetty Mar 13 '12 at 06:00
  • @vnshetty Ok, after downloading a random EPUB file & running the code, I found the problem to be this: `fout = new FileOutputStream(_location + ze.getName());` where there's no seperator inserted between the two String's, so although the correct directory is created, FileOutputStream is directed to write to a file in a non-existence dir. Try the following code and it should work: `file = new File(_location, ze.getName());` `file.getParentFile().mkdirs();` `fout = new FileOutputStream(file);` Btw, it's always safer to use new File(String, String) to build the filepath. – Kai Mar 13 '12 at 07:40
0

Check the return value of:

boolean result = file.getParentFile().mkdirs();

If it returned false the directories wouldn't have been created. This might explain why you're getting "(Not a directory)" in the exception.

The documentation of mkdirs() says:

Note that this method does not throw IOException on failure. Callers must check the return value.

Try creating the directories like this:

boolean result = (new File(_location, ze.getName())).getParentFile().mkdirs();

This avoids messing with the '/' separator characters.

Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
0
 while ((ze = zin.getNextEntry()) != null) { 
        if (ze.isDirectory()) {
          file = new File((_location, ze.getName()));
          if (!file.exists())
             file.mkdirs();
          continue;
        }
        file = new File((_location +"/" + ze.getName()));
    //   file.getParentFile().mkdirs();
         fout= new FileOutputStream(_location + ze.getName()); 
        while ((length = zin.read(buffer))>0) {
            fout.write(buffer, 0, length);
        }
        zin.closeEntry(); 
        fout.close();        
    }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

Exception shows its not a directory , so in your code-

while ((ze = zin.getNextEntry()) != null) { 
            file = new File((_location +"/" + ze.getName()));
**if(file.isDirectory())**
            file.getParentFile().mkdirs();
             fout= new FileOutputStream(_location + ze.getName()); 
            while ((length = zin.read(buffer))>0) {
                fout.write(buffer, 0, length);
            }
            zin.closeEntry(); 
            fout.close();        
        }
Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128