使用完整的目录结构将zipfile提取到outdir
/*from n o w j a v a . c o m*/ //package com.nowjava; import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main { /*** * Extract zipfile to outdir with complete directory structure * @param zipfile Input .zip file * @param outputDirectory Output directory */ public static boolean extract(File zipfile, File outputDirectory) { boolean isSuccessful = false; try { isSuccessful = extract(new FileInputStream(zipfile), outputDirectory); } catch (IOException e) { e.printStackTrace(); } return isSuccessful; /** 来 自 N o w J a v a . c o m - 时 代 Java **/ } /*** * Extract zipfile to outdir with complete directory structure * @param zipfile Input .zip file * @param outputDirectory Output directory */ public static boolean extract(InputStream zipfile, File outputDirectory) { boolean isSuccessful = false; try { ZipInputStream zin = new ZipInputStream(zipfile); ZipEntry entry; String name, dir; while ((entry = zin.getNextEntry()) != null) { name = entry.getName(); if (entry.isDirectory()) { mkdirs(outputDirectory, name); continue; } /* this part is necessary because file entry can come before * directory entry where is file located * i.e.: * /foo/foo.txt * /foo/ */ dir = dirpart(name); if (dir != null) { mkdirs(outputDirectory, dir); } extractFile(zin, outputDirectory, name); } zin.close(); // After the extraction is done - update the flag isSuccessful = true; } catch (IOException e) { e.printStackTrace(); } return isSuccessful; } private static void mkdirs(File outputDirectory, String path) { File d = new File(outputDirectory, path); if (!d.exists()) { d.mkdirs(); } } private static String dirpart(String name) { int s = name.lastIndexOf(File.separatorChar); return s == -1 ? null : name.substring(0, s); } private static void extractFile(ZipInputStream in, File outputDirectory, String name)