可循环解压掉压缩文件中所有zip的文件
/**
* 迭代解压ZIP文件,可将压缩包中包含的压缩文件组个解压
* @param zipPath 压缩文件路径
* @param unZipPath 解压后释放文件的目录
* @return 是否操作成功
*/
private static boolean unZip(String zipPath, String unZipPath) {
int count = -1;
File file = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
byte buf[] = new byte[2048];
try {
unZipPath = unZipPath
+ File.separator
+ zipPath.substring(
zipPath.lastIndexOf(File.separator) + 1, zipPath
.lastIndexOf("."));
ZipFile zipFile = new ZipFile(zipPath);
Enumeration<?> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String fileName = entry.getName();
// 如果为目录条目,执行下列语句
if (entry.isDirectory()) {
File dir = new File(unZipPath + File.separator
+ entry.getName());
if (!dir.exists()) {
dir.mkdirs();
}
continue;
}
String unZipDir = "";
// 非目录
if (fileName.indexOf("/") > 0) {
unZipDir = fileName.substring(0, fileName.lastIndexOf("/"));
fileName = fileName
.substring(fileName.lastIndexOf("/") + 1);
}
File dir = new File(unZipPath + File.separator + unZipDir);
if (!dir.exists()) {
dir.mkdirs();
}
file = new File(unZipPath + File.separator + unZipDir, fileName);
is = new BufferedInputStream(zipFile.getInputStream(entry));
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, 1024);
while ((count = is.read(buf)) > -1) {
bos.write(buf, 0, count);
}
fos.close();
is.close();
String fileType = fileName.substring(
fileName.lastIndexOf(".") + 1, fileName.length());
if ("zip".equals(fileType.toLowerCase())) {
if (unZip(unZipPath + File.separator + fileName, unZipPath)) {
file.delete();
}
}
}
zipFile.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
通过 Apache Ant 中tool.zip包实现对ZIP文件的解压缩,支持中文