View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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  {
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 }