posted by Full-stack Developer 2011. 10. 17. 17:37
 package com.xxx.xxx;

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


public class CUnzip {
final static int BUFFER_SIZE = 1024 * 1000;
public boolean UnzipStart(String fname ){
File zipFile = new File("/sdcard/urdir/filename.ZIP") ;
File targetDir = new File("/sdcard/dir/");
try {
return unzip(zipFile,targetDir);
} catch (Exception e) {

return false;
}
}
private boolean unzip(File zipFile, File targetDir) {
        FileInputStream fis = null;
        ZipInputStream zis = null;
        ZipEntry zentry = null;

        try {
try {

fis = new FileInputStream(zipFile);
zis = new ZipInputStream(fis);

while ((zentry = zis.getNextEntry()) != null) {
String fileNameToUnzip = zentry.getName(); 

File targetFile = new File(targetDir, fileNameToUnzip);

if (zentry.isDirectory()) {//if is Directory
File path = new File(targetFile.getAbsolutePath());
if (!path.isDirectory()) {
path.mkdirs();
}
} else { //if is File 
File path = new File(targetFile.getParent());
if (!path.isDirectory()) {
path.mkdirs();
}
unzipEntry(zis, targetFile);
}
}
} finally {
if (zis != null) {
zis.close();
}
if (fis != null) {
fis.close();
}
}
} catch (Exception e) {
return false;
}
return true;
    }
    private static File unzipEntry(ZipInputStream zis, File targetFile) throws Exception {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(targetFile);

            byte[] buffer = new byte[BUFFER_SIZE];
            int len = 0;
            while ((len = zis.read(buffer)) != -1) {
            if(len == 0){
            return null;
            }
                fos.write(buffer, 0, len);
            }
        } finally {
            if (fos != null) {
                fos.close();
            }
        }
        return targetFile;
    }
}