代码如下,已经测试:
private static void unzipFile(final String dir, final File zf) { File file = null; try { ZipFile zipFile = new ZipFile(zf.toString()); Enumeration<?> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry)entries.nextElement(); String filename = entry.getName(); //目录,产生后返回。 if (entry.isDirectory()) { file = new File(dir + File.separator +filename); file.mkdirs(); continue; } //确实有这种情况。 else if (filename.indexOf('/') >= 0) { String subdir = filename.substring(0, filename.lastIndexOf('/')); file = new File(dir + File.separator +subdir); file.mkdirs(); } file = new File(dir + File.separator +filename); file.createNewFile(); InputStream is = zipFile.getInputStream(entry); FileOutputStream fos = new FileOutputStream(file); int count = 0; byte buf[] = new byte[4096]; while ((count = is.read(buf)) > 0) { fos.write(buf, 0, count); } fos.close(); is.close(); } zipFile.close(); } catch (Exception e) { System.out.println(dir+", "+zf+", "+file); e.printStackTrace(); } }