Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unzip File in Android Assets

Tags:

android

zip

I put a zip file in the android assets. How do i extract the file in the android internal storage? I know how to get the file, but i don't know how to extract it. This is my code..

Util zip ;

zip = new Util();

zip.copyFileFromAsset(this, "myfile.zip", getExternalStorage()+ "/android/data/edu.binus.profile/");

Thanks for helping :D

like image 991
kyuu Avatar asked Mar 26 '13 02:03

kyuu


1 Answers

This piece of code will help you....Just pass the zipfile location and the location where you want the extracted files to be saved to this class while making an object...and call unzip method...

    public class Decompress { 
  private String zip; 
  private String loc; 

  public Decompress(String zipFile, String location) { 
    zip = zipFile; 
    loc = location; 

    dirChecker(""); 
  } 

  public void unzip() { 
    try  { 
      FileInputStream fin = new FileInputStream(zip); 
      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(loc + ze.getName()); 
          for (int c = zin.read(); c != -1; c = zin.read()) { 
            fout.write(c); 
          } 

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

      } 
      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(); 
    } 
  } 
} 
like image 71
Sreedev Avatar answered Nov 06 '22 16:11

Sreedev