Project Report: fawkez

Packagesummary org.jcoderz.commons.util

org.jcoderz.commons.util.JarUtils

LineHitsNoteSource
1  /*
2   * $Id: JarUtils.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.util;
34  
35  
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.io.FileOutputStream;
39  import java.io.IOException;
40  import java.io.InputStream;
41  import java.io.OutputStream;
42  import java.util.Collections;
43  import java.util.Iterator;
44  import java.util.List;
45  import java.util.jar.JarEntry;
46  import java.util.jar.JarFile;
47  import java.util.jar.JarOutputStream;
48  import java.util.zip.ZipEntry;
49  
50  
51  /**
52   * Jar Utility class.
53   *
54   * @author Michael Griffel
55   * @author Andreas Mandel
56   */
57  public final class JarUtils
58  {
59      private static final int BUFFER_SIZE = 4096;
60  
61      /**
62       * Utility class - no instances allowed.
63       */
64      private JarUtils ()
650     {
66          // no instances allowed -- provides only static helper functions
670     }
68  
69      /**
70       * Extract a jar archive into the base directory
71       * <code>baseDir</code>.
72       *
73       * @param baseDir the root directory where the archive is extracted
74       *        to.
75       * @param archive jar file.
76       * @throws IOException in case of an I/O error.
77       */
78      public static void extractJarArchive (File baseDir, File archive)
79          throws IOException
80      {
810         final JarFile archiveFile = new JarFile(archive);
820         final List archiveEntries = Collections.list(archiveFile.entries());
830         for (final Iterator iterator = archiveEntries.iterator(); iterator
840             .hasNext();)
85          {
860             InputStream in = null;
870             FileOutputStream out = null;
88              try
89              {
900                 final ZipEntry e = (ZipEntry) iterator.next();
910                 in = archiveFile.getInputStream(e);
920                 final File f = new File(baseDir, e.getName());
930                 if (e.isDirectory())
94                  {
950                     if (!f.exists())
96                      {
970                         if (!f.mkdirs())
98                          {
990                             throw new IOException("Cannot create directory "
100                                  + f);
101                          }
102                      }
103                  }
104                  else
105                  {
1060                     if (!f.getParentFile().exists())
107                      {
1080                         if (!f.getParentFile().mkdirs())
109                          {
1100                             throw new IOException("Cannot create directory "
111                                  + f.getParentFile());
112                          }
113                      }
1140                     out = new FileOutputStream(f);
1150                     copy(in, out);
116                  }
117              }
118              finally
119              {
1200                 IoUtil.close(out);
1210                 IoUtil.close(in);
1220             }
1230         }
1240     }
125  
126      /**
127       * Creates a jar archive from the directory.
128       *
129       * @param baseDir for the jar archive.
130       * @param archive the jar file.
131       * @throws IOException in case of an I/O error.
132       */
133      public static void createJarArchive (File baseDir, File archive)
134          throws IOException
135      {
1360         JarOutputStream jarArchive = null;
137          try
138          {
1390             jarArchive = new JarOutputStream(new FileOutputStream(archive));
1400             addFileToJar(baseDir, baseDir, jarArchive);
141          }
142          finally
143          {
1440             IoUtil.close(jarArchive);
1450         }
1460     }
147  
148      private static void addFileToJar (File baseDir, File file,
149          JarOutputStream archive)
150          throws IOException
151      {
1520         if (file == null)
153          {
154              // done
155          }
1560         else if (file.isDirectory())
157          {
1580             String path = FileUtils.getRelativePath(baseDir, file);
1590(1)(2)(3)            if (!path.equals("/") && !path.equals(""))
160              {
1610                 if (!path.endsWith("/"))
162                  {
1630(4)                    path += "/";
164                  }
1650                 final JarEntry entry = new JarEntry(path);
1660                 archive.putNextEntry(entry);
1670(5)                archive.closeEntry();
168              }
1690             final File[] files = file.listFiles();
1700             for (int i = 0; i < files.length; i++)
171              {
1720                 addFileToJar(baseDir, files[i], archive);
173              }
1740         }
175          else
176          {
1770             final String path = FileUtils.getRelativePath(baseDir, file);
1780             final JarEntry entry = new JarEntry(path);
1790             archive.putNextEntry(entry);
1800             InputStream in = null;
181              try
182              {
1830                 in = new FileInputStream(file);
1840                 copy(in, archive);
185              }
186              finally
187              {
1880                 IoUtil.close(in);
1890             }
1900             archive.closeEntry();
191          }
1920     }
193  
194      /**
195       * Copies the content of the input stream <code>in</code> to the
196       * output stream <code>out</code>.
197       *
198       * @param in input stream.
199       * @param out output stream.
200       * @throws IOException in case of an I/O error.
201       */
202      private static void copy (InputStream in, OutputStream out)
203          throws IOException
204      {
2050         final byte[] buffer = new byte[BUFFER_SIZE];
206          int nread;
2070         while ((nread = in.read(buffer)) != -1)
208          {
2090             out.write(buffer, 0, nread);
210          }
2110     }
212  }

Findings in this File

i (1) 159 : 0 method org.jcoderz.commons.util.JarUtils.addFileToJar(File, File, JarOutputStream) makes literal string comparisons passing the literal as an argument
c (2) 159 : 18 Position literals first in String comparisons
c (3) 159 : 39 Position literals first in String comparisons
c (4) 163 : 26 Prefer StringBuffer over += for concatenating strings
w (5) 167 : 0 Empty jar file entry created in org.jcoderz.commons.util.JarUtils.addFileToJar(File, File, JarOutputStream)