1 9 package org.jboss.portal.common.ant; 10 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.FileNotFoundException ; 14 import java.io.FileOutputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.util.Arrays ; 18 import java.util.HashSet ; 19 import java.util.Set ; 20 import java.util.zip.ZipEntry ; 21 import java.util.zip.ZipException ; 22 import java.util.zip.ZipInputStream ; 23 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.Task; 26 27 33 public class Explode extends Task 34 { 35 36 37 public static Set extensions = new HashSet (Arrays.asList(new String []{"ear","war","sar","har"})); 38 39 40 private File file; 41 42 43 private File todir; 44 45 46 private String name; 47 48 public void setFile(File file) 49 { 50 this.file = file; 51 } 52 53 public void setTodir(File todir) 54 { 55 this.todir = todir; 56 } 57 58 public void setName(String name) 59 { 60 this.name = name; 61 } 62 63 public void execute() throws BuildException 64 { 65 try 66 { 67 explode(file, todir); 68 } 69 catch (DirException e) 70 { 71 throw new BuildException(e.getMessage()); 72 } 73 } 74 75 public void explode(File file, File todir) throws BuildException, DirException 76 { 77 if (!file.exists()) 78 { 79 throw new BuildException("source file does not exists"); 80 } 81 if (!file.isFile()) 82 { 83 throw new BuildException("source file is not file"); 84 } 85 if (name == null) 86 { 87 name = file.getName(); 88 } 89 InputStream in = null; 90 try 91 { 92 in = new FileInputStream (file); 93 ZipInputStream zip = new ZipInputStream (in); 94 log("Process archive " + name); 95 explode(this, name, zip, todir); 96 } 97 catch (FileNotFoundException e) 98 { 99 throw new BuildException("Unexpected error " + e.getMessage()); 100 } 101 finally 102 { 103 if (in != null) 104 { 105 try 106 { 107 in.close(); 108 } 109 catch (IOException ignored) 110 { 111 } 112 } 113 } 114 } 115 116 126 public static void explode(Explode explode, String name, ZipInputStream zip, File todir) throws BuildException, DirException 127 { 128 if (!todir.exists()) 130 { 131 throw new BuildException("target dir does not exists"); 132 } 133 if (!todir.isDirectory()) 134 { 135 throw new BuildException("target dir is not a directory"); 136 } 137 try 138 { 139 byte[] buffer = new byte[512]; 141 142 todir = new File (todir, name); 144 145 ensureDirExist(explode, todir); 147 148 for (ZipEntry entry = zip.getNextEntry();entry != null;entry = zip.getNextEntry()) 150 { 151 File fic = new File (todir, entry.getName()); 153 int lastDot = fic.getName().lastIndexOf("."); 154 155 if (entry.isDirectory()) 156 { 157 try 159 { 160 ensureDirExist(explode, fic); 161 } 162 catch (DirException e) 163 { 164 explode.log(e.getMessage()); 165 } 166 } 167 else if (lastDot != -1 && extensions.contains(fic.getName().substring(lastDot + 1))) 168 { 169 try 171 { 172 explode.log("Process nested archive " + fic.getName()); 173 explode(explode, fic.getName(), new ZipInputStream (zip), todir); 174 } 175 catch (DirException e) 176 { 177 explode.log(e.getMessage()); 178 } 179 } 180 else 181 { 182 FileOutputStream out = null; 184 try 185 { 186 out = new FileOutputStream (fic); 187 for (int size = zip.read(buffer);size != -1;size = zip.read(buffer)) 188 { 189 out.write(buffer, 0, size); 190 } 191 } 192 catch (IOException e) 193 { 194 explode.log("Problem when writing file " + e.getMessage()); 195 } 196 finally 197 { 198 if (out != null) 199 { 200 try 201 { 202 out.close(); 203 } 204 catch (IOException ignored) 205 { 206 } 207 } 208 } 209 } 210 } 211 } 212 catch (ZipException e) 213 { 214 throw new BuildException(e); 215 } 216 catch (IOException e) 217 { 218 throw new BuildException(e); 219 } 220 } 221 222 225 private static void ensureDirExist(Explode explode, File dir) throws FileIsNotDirException, CannotCreateDirException 226 { 227 if (dir.exists()) 228 { 229 if (dir.isDirectory()) 230 { 231 } 233 else 234 { 235 throw new FileIsNotDirException(dir); 236 } 237 } 238 else 239 { 240 if (dir.mkdirs()) 241 { 242 } 244 else 245 { 246 throw new CannotCreateDirException(dir); 247 } 248 } 249 } 250 } 251 | Popular Tags |