0

I am downloading zip file from server to internal storage. And after downloading I want to unzip that file in internal storage itself. My file is downloaded to internal storage and I can see that but while unzip I am not able to read it. Below is my unzipfile() method. Can anyone tell where I am going wrong?

public void unzipfile()
    {
        try {

            Log.d("unzipiing","unzipping");
            ContextWrapper cw = new ContextWrapper(context);
            String name_="foldername"; //Folder name in device android/data/
            File directory = cw.getDir(name_, Context.MODE_PRIVATE);
            File mypath=new File(directory,"dwnld");

            FileOutputStream fout = new FileOutputStream(mypath);

            File yourFile = new File(directory,"dwnld.zip");
            Log.d("unzipiing","filepath -" + yourFile.getPath());

            FileInputStream fin = new FileInputStream(yourFile.getPath());
            ZipInputStream zin = new ZipInputStream(fin);
            Log.d("unzipiing","zin size -" + zin.available());
           // zin.available() give -1 in console log

            BufferedInputStream in = new BufferedInputStream(zin);
            BufferedOutputStream out = new BufferedOutputStream(fout);


        byte b[] = new byte[zin.available()];
        int n;
            Log.d("unzip","n - " + in.read(b,0,1024));
        while ((n = in.read(b,0,1024)) >= 0) {
            out.write(b,0,n);
            Log.d("unzip byte"," - " + n);
        }

        out.flush();
        out.close();
            in.close();
            fin.close();
            zin.close();
        }
        catch (Exception e){
        }
    }
Yashvant K
  • 1
  • 1
  • 3
  • Where exactly is the issue is not clear yet. You can try alternative solutions to get it done: http://stackoverflow.com/questions/3382996/how-to-unzip-files-programmatically-in-android – Chintan Soni May 01 '17 at 05:59
  • Thanks for link and help. Will try that solution. But I want to know why this code is not working :-( – Yashvant K May 01 '17 at 06:04
  • You can comment your code for a while, move ahead with next set of solutions and when you get free try to debug your code. – Chintan Soni May 01 '17 at 06:07
  • You should on several places use File#exists() to check if a file or directory exists. Further there is no code in the catch blocks so you do not know what happens. Who downloaded the file? To which path? Tell us all full paths. – greenapps May 01 '17 at 11:03

1 Answers1

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

use this method for unzip

 public void unzip(String _zipFile, String _targetLocation) {

            //create target location folder if not exist
            dirChecker(_targetLocatioan);

            try {
                FileInputStream fin = new FileInputStream(_zipFile);
                ZipInputStream zin = new ZipInputStream(fin);
                ZipEntry ze = null;
                while ((ze = zin.getNextEntry()) != null) {

                    //create dir if required while unzipping
                    if (ze.isDirectory()) {
                        dirChecker(ze.getName());
                    } else {
                        FileOutputStream fout = new FileOutputStream(_targetLocation + ze.getName());
                        for (int c = zin.read(); c != -1; c = zin.read()) {
                            fout.write(c);
                        }

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

                }
                zin.close();
            } catch (Exception e) {
                System.out.println(e);
            }
    }

check the path

public void dirChecker(String filepath)
{
File file = new File(filePath);
if(file.exists())      
//Do something
else
// Do something else.
}
Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29