0

I do not want the overhead of copying so many files from android assets directory to my app's internal storage. That is why I have created a zip file in assets folder and I will copy that zip file on my android app's internal storage. After copying it, I will extract/unzip it on the same location and after extraction is complete I will delete this zip file.

The idea is to remove the overhead of copying so many files. Also while copying so many files I am getting IOException with some file types. Which again was a hinderance. That is why I am trying to do so as I have discussed above. But the problem is that how can I extract a zip file that is residing in my app's internal storage. I want to extract it on the same location(App's internal storage). How can I achieve this?

Any help or code is appreciated..!!!

Master
  • 2,945
  • 5
  • 34
  • 65

1 Answers1

1

You can extract a zip file using this code from this source :

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.util.Log;

public class UnzipUtil
{
    private String zipFile;
    private String location;

    public UnzipUtil(String zipFile, String location)
    {
        this.zipFile = zipFile;
        this.location = location;

        dirChecker("");
    }

    public void unzip()
    {
        try
        {
            FileInputStream fin = new FileInputStream(zipFile);
            ZipInputStream zin = new ZipInputStream(fin);
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null)
            {
                Log.v("Decompress", "Unzipping " + ze.getName());

                if(ze.isDirectory())
                {
                    dirChecker(ze.getName());
                }
                else
                {
                    FileOutputStream fout = 
                        new FileOutputStream(location + ze.getName());

                    byte[] buffer = new byte[8192];
                    int len;
                    while ((len = zin.read(buffer)) != -1)
                    {
                        fout.write(buffer, 0, len);
                    }
                    fout.close();

                    zin.closeEntry();

                }

            }
            zin.close();
        }
        catch(Exception e)
        {
            Log.e("Decompress", "unzip", e);
        }

    }

    private void dirChecker(String dir)
    {
        File f = new File(location + dir);
        if(!f.isDirectory())
        {
            f.mkdirs();
        }
    }
}

Give Write External Storage permission in AndroidManifest.xml

J. Rahmati
  • 735
  • 10
  • 37
Abhishek Agarwal
  • 1,907
  • 12
  • 21