1 /***************************************************************
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version 2
6 * of the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 * @author: Copyright (C) Tim Carver
19 ***************************************************************/
24 import java.util.zip.*;
30 * Unpacks a Jar file into a Hashtable
33 public class JembossJarUtil
36 /** Hashtable containing the unpacked contents of the jar file */
37 private Hashtable jarStore = new Hashtable();
41 * Given the path to a jar file unpack to a hashtable
42 * @param jarFile path to jar file to unpack
43 * @throws Exception if it is not possible to read jar file
46 public JembossJarUtil(String jarFile) throws Exception
51 // extracts just sizes only
52 ClassLoader cl = this.getClass().getClassLoader();
53 ZipInputStream zis= new ZipInputStream(
54 cl.getResourceAsStream(jarFile));
56 Hashtable htSizes = new Hashtable();
58 while((ze=zis.getNextEntry())!=null)
65 byte[] b1 = new byte[1];
66 ret=zis.read(b1,rb,1);
69 htSizes.put(ze.getName(),new Integer(cnt));
73 // extract resources and put them into the hashtable
74 zis = new ZipInputStream(cl.getResourceAsStream(jarFile));
76 while ((ze=zis.getNextEntry())!=null)
81 int size=(int)ze.getSize(); // -1 means unknown size
83 size=((Integer)htSizes.get(ze.getName())).intValue();
85 byte[] b=new byte[(int)size];
88 while (((int)size - rb) > 0)
90 chunk=zis.read(b,rb,(int)size - rb);
96 // add to internal resource hashtable
97 jarStore.put(ze.getName(),b);
99 // System.out.println(ze.getName());
103 catch (Exception e) { throw new Exception();}
105 // catch (NullPointerException e)
107 // System.out.println("JembossJarUtil Error: jarStore");
109 // catch (FileNotFoundException e)
111 // e.printStackTrace();
113 // catch (IOException e)
115 // e.printStackTrace();
122 * Return the hashtable
123 * @return jarStore the hashtable containing the contents
127 public Hashtable getHash()
134 * Return an element of the hashtable
135 * @param el key of an element in the hashtable
136 * @return the hashtable containing the contents
140 public Object getElement(String el)
142 return jarStore.get(el);
147 * Write contents of an element in the hashtable
148 * @param el key of an element in the hashtable
149 * @param f path of file to write to
150 * @return true if written file
153 public boolean writeByteFile(String el, String f)
157 FileOutputStream out = new FileOutputStream(f);
158 out.write((byte []) jarStore.get(el));
161 catch(FileNotFoundException fnfe) {return false;}
162 catch(IOException ioe) {return false;}