1

I am trying to unzip a zip file. This file contains several other files.The method I am using is:

private void unzipFile(String inputFileName,String outputDirName){
    int dataCount=0;
    byte[] dataArr=null;
    File inputZipFile=null,targetDir=null,unzipFile=null;
    ZipInputStream zipIPStream=null;
    ZipEntry zipEntry=null;
    BufferedOutputStream bfrOPStream=null;
    FileOutputStream unzipOPStream=null;
    try{
        inputZipFile=new File(inputFileName);
        targetDir=new File(outputDirName);
        zipIPStream=new ZipInputStream(new BufferedInputStream(new FileInputStream(inputZipFile)));
        while((zipEntry=zipIPStream.getNextEntry())!=null){
            dataArr=new byte[2048];
            // zipEntry=zipIPStream.getNextEntry();
            System.out.println("ZipEntry:"+zipEntry.getName());
            unzipFile=new File(targetDir.getAbsolutePath()+"/"+zipEntry.getName());
            System.out.println("Checking the path:"+unzipFile.getPath());
            if(!unzipFile.getParentFile().exists()){
                unzipFile.getParentFile().mkdirs();
            }
            unzipOPStream=new FileOutputStream(targetDir.getPath()+"/"+zipEntry.getName());
            bfrOPStream=new BufferedOutputStream(unzipOPStream,2048);
            while((dataCount=zipIPStream.read(dataArr,0,2048))!=-1){
             unzipOPStream.write(dataArr,0,dataCount);
            }
            unzipOPStream.flush();
            unzipOPStream.close();
        }
    }catch(IOException ioe){
        ioe.printStackTrace();
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(zipIPStream!=null){
            try{
                zipIPStream.close();
            }catch(Exception e){

            }
        }
    }
}

The method is throwing the following error

10-22 13:33:09.896: W/System.err(446): java.io.FileNotFoundException: /mnt/sdcard/iR/testbook/Caterpillar-Insect-Animal-Macro-HD-600x375.jpg (Not a directory)
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Richa Laad
  • 259
  • 5
  • 21
  • in which line the error occurs? it seems that it's trying to read this .jpg as a directory which is not correct.. – ColdFire Oct 22 '12 at 11:40
  • it seems that a .jpg is not a directory. – njzk2 Oct 22 '12 at 11:54
  • I got the solution. I was trying to unzip the file in the same directory in which it was kept. I changed the directory and now method is working fine. – Richa Laad Oct 22 '12 at 12:19

1 Answers1

1

Please see below link for download and extract zip files, it will solve your problem.

For Downloading Zip File:-

Download Zip File

For Extract Zip File:-

Extract Zip File

And see below link for more information to solve this problem.

Download and Extract Zip File in Android

And the zip file is make using winrar software only otherwise this will give you above error.

Community
  • 1
  • 1
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128