001: package org.enhydra.server.util;
002:
003: /**
004: * <p>Title: </p>
005: * <p>Description: Util class for unpack WAR file to destination directory</p>
006: * <p>Copyright: Copyright (c) 2002</p>
007: * <p>Company: </p>
008: * @author Damir Milovic
009: * @version 1.0
010: */
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.FileNotFoundException;
015: import java.io.FileOutputStream;
016: import java.io.IOException;
017: import java.io.InputStream;
018: import java.util.zip.ZipEntry;
019: import java.util.zip.ZipInputStream;
020:
021: import org.enhydra.server.EnhydraServerException;
022:
023: public class UnpackWar {
024:
025: /**
026: * If this constructor is used must call <code>setWar()</code> and
027: * <code>setDestDir()</code> methods.
028: */
029: public UnpackWar() {
030: }
031:
032: /**
033: * Default constructor
034: * @param warPath path to war file
035: * @param destDirPath path to destination directory for unpack files
036: */
037: public UnpackWar(String warPath, String destDirPath) {
038: setWar(new File(warPath));
039: setDestDir(new File(destDirPath));
040: }
041:
042: private File dest;
043:
044: private File source;
045:
046: /**
047: * Unpack war to destination directory.
048: */
049: public void execute() throws EnhydraServerException {
050: expandFile(source, dest);
051: }
052:
053: private void expandFile(File srcF, File dir)
054: throws EnhydraServerException {
055: log("Expanding: " + srcF + " into " + dir);
056: ZipInputStream zis = null;
057: try {
058:
059: zis = new ZipInputStream(new FileInputStream(srcF));
060: ZipEntry ze = null;
061:
062: while ((ze = zis.getNextEntry()) != null) {
063: extractFile(srcF, dir, zis, ze.getName(), ze
064: .isDirectory());
065: }
066:
067: log("expand complete");
068: } catch (IOException ioe) {
069: throw new EnhydraServerException("Error while expanding "
070: + srcF.getPath(), ioe);
071: } finally {
072: if (zis != null) {
073: try {
074: zis.close();
075: } catch (IOException e) {
076: }
077: }
078: }
079: }
080:
081: private void extractFile(File srcF, File dir,
082: InputStream compressedInputStream, String entryName,
083: boolean isDirectory) throws IOException {
084:
085: String filePath = PathUtil.makeAbsolutePath(dir
086: .getAbsolutePath(), entryName);
087: File f = new File(filePath);
088: try {
089: log("expanding " + entryName + " to " + f);
090: // create intermediary directories - sometimes zip don't add them
091: File dirF = f.getParentFile();
092: if (dirF != null) {
093: dirF.mkdirs();
094: }
095:
096: if (isDirectory) {
097: f.mkdirs();
098: } else {
099: byte[] buffer = new byte[1024];
100: int length = 0;
101: FileOutputStream fos = null;
102: try {
103: fos = new FileOutputStream(f);
104:
105: while ((length = compressedInputStream.read(buffer)) >= 0) {
106: fos.write(buffer, 0, length);
107: }
108:
109: fos.close();
110: fos = null;
111: } finally {
112: if (fos != null) {
113: try {
114: fos.close();
115: } catch (IOException e) {
116: }
117: }
118: }
119: }
120:
121: } catch (FileNotFoundException ex) {
122: log("Unable to expand to file " + f.getPath());
123: }
124:
125: }
126:
127: /**
128: * Set the destination directory. File will be unzipped into the
129: * destination directory.
130: *
131: * @param d Path to the directory.
132: */
133: public void setDestDir(File d) {
134: this .dest = d;
135: }
136:
137: /**
138: * Set the path to War-file.
139: *
140: * @param s Path to war-file.
141: */
142: public void setWar(File s) {
143: this .source = s;
144: }
145:
146: // todo: set logger
147: private void log(String message) {
148: System.out.println(message);
149: }
150:
151: //For Test only
152: public static void main(String[] args) {
153: UnpackWar test = new UnpackWar(
154: "c:/Temp/testwar/testKelpDist.war",
155: "c:/Temp/testwar/webapps/test");
156: try {
157: test.execute();
158: } catch (Exception e) {
159: e.printStackTrace();
160: }
161: }
162: }
|