Search code examples
javaandroidxmlzipunzip

Android unzip function not working


The following unzip finction doesn't work for all zip files.

My zip file pattern is as follows-

  1. The Zip file contains one xml file and one folder(name- "images").
  2. The name of the xml file is same as the zip file name.
  3. The folder("images") may or may not contain any files.

I have validated the xml file before putting it into the zip file.

It throws exception at this line for some zip files-

FileOutputStream fout = new ileOutputStream(path.substring(0,path.length()-4)+"/"+filename);

The function is:

public boolean unZip(String path)
{       
    InputStream is;
    ZipInputStream zis;
    try 
    {
        String filename;
        is = new FileInputStream(path);
        zis = new ZipInputStream(new BufferedInputStream(is));   
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        int count;

        while ((ze = zis.getNextEntry()) != null) 
        {
            filename = ze.getName();
            if (ze.isDirectory()) {
                File fmd = new File(path.substring(0,path.length()-4)+"/"+filename);
                fmd.mkdirs();
                continue;
            }

            FileOutputStream fout = new FileOutputStream(path.substring(0,path.length()-4)+"/"+filename);

            while ((count = zis.read(buffer)) != -1) 
            {
                fout.write(buffer, 0, count);             
            }

            fout.close();               
            zis.closeEntry();
        }

        zis.close();
    } 
    catch(IOException e)
    {
        e.printStackTrace();
        return false;
    }

    return true;
}

Exception at Logcat view


Solution

  • This method works fine. It was a permission issue while creating the zips in Linux platform. But function starts working properly when I changed the file permission.