View Javadoc
1   /*
2    * Created on 2005-maj-13
3    */
4   package net.sourceforge.jsh3modtool.modpackage;
5   
6   import java.io.File;
7   import java.io.FileNotFoundException;
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.util.jar.JarEntry;
11  import java.util.jar.JarFile;
12  
13  /***
14   * A jar file package.
15   * The mod is packed into a jar file, this class handles the extraction of the 
16   * files from the jar file.
17   * @author redsolo
18   */
19  public JarModPackage implements ModPackage/package-summary.html">class JarModPackage implements ModPackage
20  {
21      private JarFile jarFile;
22      /***
23       * @param file the jar file.
24       * @throws IOException thrown if the file does not exist.
25       */
26      protectedJarModPackage(File file) throws IOException/package-summary.html">ong> JarModPackage(File file) throws IOException
27      {
28          super();
29          if (file == null)
30          {
31              throw new IllegalArgumentException("The jar file can not be null");
32          }
33          if (!file.exists())
34          {
35              throw new FileNotFoundException("The jar file '" + file.toString() + "' does not exist");
36          }
37          jarFile = new JarFile(file);
38      }
39  
40      public InputStream getEntry(String entryName) throws IOException
41      {
42          JarEntry jarEntry = getJarEntry(entryName);
43          if (jarEntry == null)
44          { 
45              throw new FileNotFoundException(entryName);
46          }
47          return jarFile.getInputStream(jarEntry);
48      }
49  
50      public boolean entryExists(String entryName)
51      {
52          return (getJarEntry(entryName) != null);
53      }
54  
55      /***
56       * @param entryName
57       * @return
58       */
59      private JarEntry getJarEntry(String entryName)
60      {
61          String tmpName = entryName;
62          if (entryName.charAt(0) == '/')
63          {
64              tmpName = entryName.substring(1);
65          }
66          return jarFile.getJarEntry(tmpName);
67      }
68      
69      
70  }