static public void extractFolder(String zipFile) throws ZipException, IOException {
System.out.println(zipFile);
int BUFFER = 2048;
File file = new File(zipFile);
ZipFile zip = new ZipFile(file);
String newPath = zipFile.substring(0, zipFile.length() - 4);
new File(newPath).mkdir();
Enumeration zipFileEntries = zip.entries();
// 处理每个条目
while (zipFileEntries.hasMoreElements())
{
// 取一个zip文件条目
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
File destFile = new File(newPath, currentEntry);
//destFile = new File(newPath, destFile.getName());
File destinationParent = destFile.getParentFile();
//根据需要创建父目录结构
destinationParent.mkdirs();
if (!entry.isDirectory())
{
BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
int currentByte;
//
//建立用于写入文件的缓冲区
byte data[] = new byte[BUFFER];
// 将当前文件写入磁盘
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos,BUFFER);
// 读写,直到遇到最后一个字节
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
if (currentEntry.endsWith(".zip"))
{
// 找到一个zip文件,尝试打开
extractFolder(destFile.getAbsolutePath());
}
}
}
测试
public static void main(String[] args)throws Exception{
//将Zip文件解压缩到目标目录
test2.extractFolder("E:\\测试.zip");
}