1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 {
39 public static void expand(File srcFile, File targetDir) throws IOException
40 {
41 if (targetDir.exists())
42 {
43 DirectoryHelper cleanup = new DirectoryHelper(targetDir);
44 cleanup.remove();
45 cleanup.close();
46 }
47
48 targetDir.mkdirs();
49 JarFile jarFile = new JarFile(srcFile);
50
51 try
52 {
53 Enumeration entries = jarFile.entries();
54
55 InputStream is = null;
56 OutputStream os = null;
57
58 byte[] buf = new byte[1024];
59 int len;
60
61 while (entries.hasMoreElements())
62 {
63 JarEntry jarEntry = (JarEntry) entries.nextElement();
64 String name = jarEntry.getName();
65 File entryFile = new File(targetDir, name);
66
67 if (jarEntry.isDirectory())
68 {
69 entryFile.mkdir();
70 }
71 else
72 {
73 if (!entryFile.getParentFile().exists())
74 {
75 entryFile.getParentFile().mkdirs();
76 }
77
78 entryFile.createNewFile();
79
80 try
81 {
82 is = jarFile.getInputStream(jarEntry);
83 os = new FileOutputStream(entryFile);
84
85 while ((len = is.read(buf)) > 0)
86 {
87 os.write(buf, 0, len);
88 }
89 }
90 finally
91 {
92 if (is != null)
93 {
94 is.close();
95 }
96
97 if (os != null)
98 {
99 os.close();
100 }
101 }
102 }
103 }
104 }
105 finally
106 {
107 if (jarFile != null)
108 {
109 jarFile.close();
110 }
111 }
112 }
113 }