import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import com....util.file.DeleteFileUtil;
/**
* Extract the zip file.
*
* @author Liangliang.Wang
*
*/
public class UnzipUtil {
/**
* Extract the zip file to the specified location.
*
* @param zins
* @param unzipDir
* @return boolean
* @throws IOException
* @throws FileNotFoundException
*/
public static boolean unzipArchive(ZipInputStream zins, String unzipDir) {
// Judge extract results
boolean flag = false;
if (zins != null && unzipDir != null && !unzipDir.isEmpty()) {
ZipEntry ze = null;
byte[] ch = new byte[256];
try {
while ((ze = zins.getNextEntry()) != null) {
File zfile = new File(unzipDir + ze.getName());
File fpath = new File(zfile.getParentFile().getPath());
if(zfile.exists()) {
DeleteFileUtil.deleteAll(zfile);
}
if (ze.isDirectory()) {
zfile.mkdirs();
zins.closeEntry();
} else {
if (!fpath.exists())
fpath.mkdirs();
zfile.createNewFile();
FileOutputStream fouts = new FileOutputStream(zfile);
int i;
try {
while ((i = zins.read(ch)) != -1) {
fouts.write(ch, 0, i);
}
zins.closeEntry();
} catch (Exception e) {
e.printStackTrace();
} finally {
fouts.close();
}
}
flag = true;
}
File file = new File(unzipDir);
File[] files = file.listFiles();
if (files == null) {
flag = false;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
zins.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
}