Search This Blog

Saturday, November 23, 2013

Unzipping the zip file with the location using ZipInputStream and ZIpEntry

Make a File with the name Decompress.java and add the following code snippet in it.

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;

/**
 * 
 * @author jon
 */
public class Decompress {
private String _zipFile;
private String _location;

public Decompress(String zipFile, String location) {
_zipFile = zipFile;
_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());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}

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

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

}

private void _dirChecker(String dir) {
File f = new File(_location + dir);

if (!f.isDirectory()) {
f.mkdirs();
}
}

private void deleteDir(String dir) {
// TODO Auto-generated method stub
File f = new File(dir);
// Util.iLog("delete dir :" + dir);
if (f.exists()) {
f.delete();
}
}
}


Now call these class with the zip file location and unzip location with the following code snippet

Decompress d = new Decompress(zipfileloc, Unziploc);
d.unzip();

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.