1 3 package jodd.io; 4 5 import java.io.File ; 6 import java.io.FileOutputStream ; 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 import java.io.OutputStream ; 10 import java.util.Enumeration ; 11 import java.util.zip.ZipEntry ; 12 import java.util.zip.ZipFile ; 13 14 18 public class ZipUtil { 19 20 22 24 25 33 public static void unzip(File zipFile, File destDir) throws IOException { 34 ZipFile zip = new ZipFile (zipFile); 35 Enumeration en = zip.entries(); 36 37 while (en.hasMoreElements()) { 38 ZipEntry entry = (ZipEntry ) en.nextElement(); 39 File file = (destDir != null) ? new File (destDir, entry.getName()) : new File (entry.getName()); 40 if (entry.isDirectory()) { 41 if (!file.mkdirs()) { 42 if (file.isDirectory() == false) { 43 throw new IOException ("Error creating directory: " + file); 44 } 45 } 46 } else { 47 File parent = file.getParentFile(); 48 if (parent != null && !parent.exists()) { 49 if (!parent.mkdirs()) { 50 if (file.isDirectory() == false) { 51 throw new IOException ("Error creating directory: " + parent); 52 } 53 } 54 } 55 56 InputStream in = zip.getInputStream(entry); 57 OutputStream out = null; 58 try { 59 out = new FileOutputStream (file); 60 StreamUtil.copy(in, out); 61 } finally { 62 StreamUtil.close(out); 63 StreamUtil.close(in); 64 } 65 } 66 } 67 } 68 69 } 70 | Popular Tags |