View Javadoc

1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006, 2007 Imola Informatica.
3    *  All rights reserved. This program and the accompanying materials
4    *  are made available under the terms of the LGPL License v2.1
5    *  which accompanies this distribution, and is available at
6    *  http://www.gnu.org/licenses/lgpl.html
7    *******************************************************************************/
8   package it.imolinfo.jbi4ejb.webservice.generator;
9   
10  import java.io.File;
11  import java.io.FileOutputStream;
12  import java.io.IOException;
13  import java.io.InputStream;
14  import java.util.Enumeration;
15  import java.util.jar.JarEntry;
16  import java.util.jar.JarFile;
17  import java.util.zip.ZipFile;
18  
19  /*
20   * 
21   * 
22   */
23  
24  /**
25   * Helper method to unjar files.
26   * Some method are taken from: Taken from:http://www.koders.com/java/fidB15D2DD90588A86609A0E986A7987FA5DF5C454A.aspx
27   * TODO :Verify license
28   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
29   */
30  public final class JarUtil {
31  
32      /** The BUFFER_LENGTH. */
33      private static final int BUFFER_LENGTH = 1024;
34      
35      
36     /**
37       * Instantiates a new jar util.
38       */
39     private JarUtil() {}
40  
41      /**
42       * Extracts the given jar-file to the specified directory. The target
43       * directory will be cleaned before the jar-file will be extracted.
44       * 
45       * @param jarFile
46       *            The jar file which should be unpacked
47       * @param targetDir
48       *            The directory to which the jar-content should be extracted.
49       * 
50       * @throws IOException
51       *             when a file could not be written or the jar-file could not
52       *             read.
53       */
54      public static void unjar(File jarFile, File targetDir) 
55      throws  IOException 
56      {
57          // clear target directory:
58          if (targetDir.exists()) {
59              targetDir.delete();
60          }
61          // create new target directory:
62          targetDir.mkdirs(); 
63          // read jar-file:
64          String targetPath = targetDir.getAbsolutePath() + File.separatorChar;
65          byte[] buffer = new byte[ BUFFER_LENGTH * BUFFER_LENGTH ];
66          JarFile input = new JarFile( jarFile, false, ZipFile.OPEN_READ );
67          Enumeration<JarEntry> enumeration = input.entries();
68          for (; enumeration.hasMoreElements();) {
69              JarEntry entry = (JarEntry) enumeration.nextElement();
70              if (!entry.isDirectory()) {
71                  // do not copy anything from the package cache:
72                  if (entry.getName().indexOf("package cache") == -1) {
73                      String path = targetPath + entry.getName();
74                      File file = new File( path );
75                      if (!file.getParentFile().exists()) {
76                          file.getParentFile().mkdirs();
77                      }
78                      FileOutputStream out = new FileOutputStream( file);
79                      InputStream in = input.getInputStream(entry);
80                      int read;
81                      while ( (read = in.read( buffer )) != -1) {
82                          out.write( buffer, 0, read );
83                      }
84                      in.close();
85                      out.close();
86                  }
87              }
88          }
89  
90      }
91  
92      /**
93       * Extracts the given resource from a jar-file to the specified directory.
94       * 
95       * @param jarFile
96       *            The jar file which should be unpacked
97       * @param resource
98       *            The name of a resource in the jar
99       * @param targetDir
100      *            The directory to which the jar-content should be extracted.
101      * 
102      * @throws IOException
103      *             when a file could not be written or the jar-file could not
104      *             read.
105      */
106     public static void unjar(File jarFile, String resource, File targetDir)
107     throws IOException {
108         // clear target directory:
109         if (targetDir.exists()) {
110             targetDir.delete();
111         }
112         // create new target directory:
113         targetDir.mkdirs(); 
114         // read jar-file:
115         String targetPath = targetDir.getAbsolutePath() + File.separatorChar;
116         byte[] buffer = new byte[ BUFFER_LENGTH * BUFFER_LENGTH ];
117         JarFile input = new JarFile( jarFile, false, ZipFile.OPEN_READ );
118         Enumeration<JarEntry> enumeration = input.entries();
119         for (; enumeration.hasMoreElements();) {
120             JarEntry entry = (JarEntry) enumeration.nextElement();
121             if (!entry.isDirectory()) {
122                 // do not copy anything from the package cache:
123                 if (entry.getName().equals(resource)) {
124                     String path = targetPath + entry.getName();
125                     File file = new File( path );
126                     if (!file.getParentFile().exists()) {
127                         file.getParentFile().mkdirs();
128                     }
129                     FileOutputStream out = new FileOutputStream( file);
130                     InputStream in = input.getInputStream(entry);
131                     int read;
132                     while ( (read = in.read( buffer )) != -1) {
133                         out.write( buffer, 0, read );
134                     }
135                     in.close();
136                     out.close();
137                 }
138             }
139         }
140     }
141 
142 }