1 package org.enhydra.server.util; 2 3 11 12 import java.io.File ; 13 import java.io.FileInputStream ; 14 import java.io.FileNotFoundException ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.util.zip.ZipEntry ; 19 import java.util.zip.ZipInputStream ; 20 21 import org.enhydra.server.EnhydraServerException; 22 23 public class UnpackWar { 24 25 29 public UnpackWar() { 30 } 31 32 37 public UnpackWar(String warPath, String destDirPath) { 38 setWar(new File (warPath)); 39 setDestDir(new File (destDirPath)); 40 } 41 42 private File dest; 43 44 private File source; 45 46 49 public void execute() throws EnhydraServerException{ 50 expandFile(source, dest); 51 } 52 53 private void expandFile(File srcF, File dir) throws EnhydraServerException{ 54 log("Expanding: " + srcF + " into " + dir); 55 ZipInputStream zis = null; 56 try { 57 58 zis = new ZipInputStream (new FileInputStream (srcF)); 59 ZipEntry ze = null; 60 61 while ((ze = zis.getNextEntry()) != null) { 62 extractFile(srcF, dir, zis, 63 ze.getName(), 64 ze.isDirectory()); 65 } 66 67 log("expand complete"); 68 } catch (IOException ioe) { 69 throw new EnhydraServerException("Error while expanding " + srcF.getPath(), 70 ioe); 71 } finally { 72 if (zis != null) { 73 try { 74 zis.close(); 75 } catch (IOException e) {} 76 } 77 } 78 } 79 80 private void extractFile(File srcF, File dir, 81 InputStream compressedInputStream, 82 String entryName, 83 boolean isDirectory) 84 throws IOException { 85 86 String filePath = PathUtil.makeAbsolutePath(dir.getAbsolutePath(), entryName); 87 File f = new File (filePath); 88 try { 89 log("expanding " + entryName + " to " + f); 90 File dirF = f.getParentFile(); 92 if ( dirF != null ) { 93 dirF.mkdirs(); 94 } 95 96 if (isDirectory) { 97 f.mkdirs(); 98 } else { 99 byte[] buffer = new byte[1024]; 100 int length = 0; 101 FileOutputStream fos = null; 102 try { 103 fos = new FileOutputStream (f); 104 105 while ((length = 106 compressedInputStream.read(buffer)) >= 0) { 107 fos.write(buffer, 0, length); 108 } 109 110 fos.close(); 111 fos = null; 112 } finally { 113 if (fos != null) { 114 try { 115 fos.close(); 116 } catch (IOException e) {} 117 } 118 } 119 } 120 121 122 } catch (FileNotFoundException ex) { 123 log("Unable to expand to file " + f.getPath()); 124 } 125 126 } 127 128 134 public void setDestDir(File d) { 135 this.dest = d; 136 } 137 138 143 public void setWar(File s) { 144 this.source = s; 145 } 146 147 private void log(String message){ 149 System.out.println(message); 150 } 151 public static void main(String [] args){ 153 UnpackWar test = new UnpackWar("c:/Temp/testwar/testKelpDist.war", "c:/Temp/testwar/webapps/test"); 154 try{ 155 test.execute(); 156 }catch(Exception e){ 157 e.printStackTrace(); 158 } 159 } 160 } | Popular Tags |