01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.jetspeed.deployment.impl;
18:
19: import org.apache.jetspeed.util.DirectoryHelper;
20:
21: import java.io.File;
22: import java.io.FileOutputStream;
23: import java.io.IOException;
24: import java.io.InputStream;
25: import java.io.OutputStream;
26:
27: import java.util.Enumeration;
28: import java.util.jar.JarEntry;
29: import java.util.jar.JarFile;
30:
31: /**
32: * JarExpander
33: *
34: * @author <a href="mailto:ate@douma.nu">Ate Douma </a>
35: * @version $Id: JarExpander.java 516448 2007-03-09 16:25:47Z ate $
36: */
37: public class JarExpander {
38: public static void expand(File srcFile, File targetDir)
39: throws IOException {
40: if (targetDir.exists()) {
41: DirectoryHelper cleanup = new DirectoryHelper(targetDir);
42: cleanup.remove();
43: cleanup.close();
44: }
45:
46: targetDir.mkdirs();
47: JarFile jarFile = new JarFile(srcFile);
48:
49: try {
50: Enumeration entries = jarFile.entries();
51:
52: InputStream is = null;
53: OutputStream os = null;
54:
55: byte[] buf = new byte[1024];
56: int len;
57:
58: while (entries.hasMoreElements()) {
59: JarEntry jarEntry = (JarEntry) entries.nextElement();
60: String name = jarEntry.getName();
61: File entryFile = new File(targetDir, name);
62:
63: if (jarEntry.isDirectory()) {
64: entryFile.mkdir();
65: } else {
66: if (!entryFile.getParentFile().exists()) {
67: entryFile.getParentFile().mkdirs();
68: }
69:
70: entryFile.createNewFile();
71:
72: try {
73: is = jarFile.getInputStream(jarEntry);
74: os = new FileOutputStream(entryFile);
75:
76: while ((len = is.read(buf)) > 0) {
77: os.write(buf, 0, len);
78: }
79: } finally {
80: if (is != null) {
81: is.close();
82: }
83:
84: if (os != null) {
85: os.close();
86: }
87: }
88: }
89: }
90: } finally {
91: if (jarFile != null) {
92: jarFile.close();
93: }
94: }
95: }
96: }
|