1 17 package org.columba.core.io; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileOutputStream ; 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.util.zip.ZipEntry ; 25 import java.util.zip.ZipInputStream ; 26 27 32 public class ZipFileIO { 33 34 37 private ZipFileIO() { 38 } 40 41 49 public static void extract(File file, File destination) throws IOException { 50 ZipInputStream in = null; 51 OutputStream out = null; 52 try { 53 in = new ZipInputStream (new FileInputStream (file)); 55 56 ZipEntry entry = null; 58 59 while ((entry = in.getNextEntry()) != null) { 60 String outFilename = entry.getName(); 61 62 if (entry.isDirectory()) { 64 new File (destination, outFilename).mkdirs(); 65 } else { 66 out = new FileOutputStream (new File (destination, 67 outFilename)); 68 69 byte[] buf = new byte[1024]; 71 int len; 72 73 while ((len = in.read(buf)) > 0) { 74 out.write(buf, 0, len); 75 } 76 77 out.close(); 79 } 80 } 81 } finally { 82 if (in != null) { 84 in.close(); 85 } 86 if (out != null) { 87 out.close(); 88 } 89 } 90 } 91 92 99 public static File getFirstFile(File zipFile) throws IOException { 100 ZipInputStream in = null; 101 try { 102 in = new ZipInputStream (new FileInputStream (zipFile)); 104 105 ZipEntry entry = null; 107 108 while ((entry = in.getNextEntry()) != null) { 109 String outFilename = entry.getName(); 110 111 if (entry.isDirectory()) { 112 return new File (outFilename); 113 } 114 } 115 } finally { 116 if (in != null) { 117 in.close(); 119 } 120 } 121 return null; 122 } 123 } 124 | Popular Tags |