/** * load from the specified jar filled with help files in the [language] directory in the jar * * @param file the jar file */ private void loadFromJar(File file) { if (file.getName().toLowerCase().endsWith(".jar") && file.isFile()) { try { int counter = 0; JarInputStream jis; JarEntry je; counter = 0; jis = new JarInputStream(new BufferedInputStream(new FileInputStream(file))); je = jis.getNextJarEntry(); while (je != null) { String mnemo = trimEntryName(je); if (je.getName().toLowerCase().matches(helproot + "/" + language + "/.*.htm") && !exists(mnemo)) { addToCache(jis, mnemo); counter++; } je = jis.getNextJarEntry(); } jis.close(); System.out.println( "+ " + String.valueOf(counter) + "\thelp text(s) from:\t" + file.getCanonicalPath()); } catch (IOException ignored) { } } }
private Hashtable<String, String> getJarManifestAttributes(String path) { Hashtable<String, String> h = new Hashtable<String, String>(); JarInputStream jis = null; try { cp.appendln(Color.black, "Looking for " + path); InputStream is = getClass().getResourceAsStream(path); if (is == null) { if (!path.endsWith("/MIRC.jar")) { cp.appendln(Color.red, "...could not find it."); } else { cp.appendln( Color.black, "...could not find it. [OK, this is a " + programName + " installation]"); } return null; } jis = new JarInputStream(is); Manifest manifest = jis.getManifest(); h = getManifestAttributes(manifest); } catch (Exception ex) { ex.printStackTrace(); } if (jis != null) { try { jis.close(); } catch (Exception ignore) { } } return h; }
public static List getClasseNamesInPackage(String jarName, String packageName) { ArrayList classes = new ArrayList(); packageName = packageName.replaceAll("\\.", "/"); if (debug) System.out.println("Jar " + jarName + " looking for " + packageName); try { JarInputStream jarFile = new JarInputStream(new FileInputStream(jarName)); JarEntry jarEntry; while (true) { jarEntry = jarFile.getNextJarEntry(); if (jarEntry == null) { break; } if ((jarEntry.getName().startsWith(packageName)) && (jarEntry.getName().endsWith(".class"))) { if (debug) System.out.println("Found " + jarEntry.getName().replaceAll("/", "\\.")); classes.add(jarEntry.getName().replaceAll("/", "\\.")); } } } catch (Exception e) { e.printStackTrace(); } return classes; }
public static boolean extractJarDir(String jarfile, String outfile) throws Exception { long filesize = new File(jarfile).length(); JarInputStream jar = new JarInputStream(new FileInputStream(jarfile)); // while (true) { // JarEntry jarentry = (JarEntry)jar.getNextJarEntry(); // if (jarentry==null) break; // String maindir = jarentry.getName(); // // get rid of "./" and "/" prefix // if (maindir.startsWith(".")) // maindir = maindir.substring(1); // if (maindir.startsWith(File.separator)) // maindir = maindir.substring(1); // // Find manifest // System.out.println(maindir); // if (maindir.equals("META-INF/MANIFEST.MF")) { // java.io.InputStream ins = jar; java.io.FileOutputStream outs = new java.io.FileOutputStream(outfile); Manifest man = jar.getManifest(); if (man == null) return false; man.write(outs); byte[] buf = ("MIDlet-Jar-Size: " + filesize).getBytes(); outs.write(buf, 0, buf.length); outs.close(); // // copy buffered (is a lot faster than one byte at a time) // byte[] buf = new byte[8192]; // int total_bytes=0; // while (true) { // //outs.write(ins.read()); // int len = ins.read(buf); // if (len < 0) break; // outs.write(buf,0,len); // total_bytes += len; // } // buf = ("MIDlet-Jar-Size: "+total_bytes).getBytes(); // outs.write(buf,0,buf.length); // outs.close(); // //ins.close(); // } // } // return false; // remove empty lines from the generated jad which may have been // introduced by manifest write. // some systems have trouble with empty lines List<String> lines = new ArrayList<String>(); BufferedReader in = new BufferedReader(new FileReader(outfile)); while (true) { String line = in.readLine(); if (line == null) break; lines.add(line); } in.close(); PrintWriter outp = new PrintWriter(outfile); for (String s : lines) { if (s.trim().equals("")) continue; outp.println(s); } outp.close(); return true; }
private static Set<String> findStandardMBeansFromJar(URL codeBase) throws Exception { InputStream is = codeBase.openStream(); JarInputStream jis = new JarInputStream(is); Set<String> names = new TreeSet<String>(); JarEntry entry; while ((entry = jis.getNextJarEntry()) != null) { String name = entry.getName(); if (!name.endsWith(".class")) continue; name = name.substring(0, name.length() - 6); name = name.replace('/', '.'); names.add(name); } return names; }
public static void unzipInteralZip( ClassLoader classLoader, String resourcePath, File libDir, boolean debug) { if (debug) System.out.println("Extracting " + resourcePath); libDir.mkdir(); URL resource = classLoader.getResource(resourcePath); if (resource == null) { System.err.println("Could not find the " + resourcePath + " on classpath!"); System.exit(1); } class PrintDot extends TimerTask { public void run() { System.out.print("."); } } Timer timer = new Timer(); PrintDot task = new PrintDot(); timer.schedule(task, 0, 2000); try { BufferedInputStream bis = new BufferedInputStream(resource.openStream()); JarInputStream jis = new JarInputStream(bis); JarEntry je = null; while ((je = jis.getNextJarEntry()) != null) { java.io.File f = new java.io.File(libDir.toString() + java.io.File.separator + je.getName()); if (je.isDirectory()) { f.mkdir(); continue; } File parentDir = new File(f.getParent()); if (!parentDir.exists()) { parentDir.mkdir(); } FileOutputStream fileOutStream = new FileOutputStream(f); writeStreamTo(jis, fileOutStream, 8 * KB); if (f.getPath().endsWith("pack.gz")) { unpack(f); fileOutStream.close(); f.delete(); } } } catch (Exception exc) { task.cancel(); exc.printStackTrace(); } task.cancel(); }
/** * Enables to get the name of an object in the spoken language thanks to the jar file * * @param jarName the file that contains the object classes * @return the object name in the spoken language */ private String getLangName(File jarName) { String name = null; try { URL url = jarName.toURI().toURL(); JarInputStream jarFile = new JarInputStream(url.openStream()); JarEntry jarEntry = jarFile.getNextJarEntry(); while (jarEntry != null) { if (!jarEntry.isDirectory() && jarEntry.getName().contains(Configuration.instance().getLanguage())) { int lang_index = jarEntry.getName().lastIndexOf(Configuration.instance().getLanguage()); name = jarEntry.getName().substring(lang_index + 3, jarEntry.getName().length() - 6); } jarEntry = jarFile.getNextJarEntry(); } } catch (Exception e) { LOG.error("Error getLangName " + jarName + " " + e); } return name; }
/** * load from specified JarInputStream * * @param fis Jar Input Stream * @param mnemo the name for the text file */ private void addToCache(JarInputStream fis, String mnemo) { String text = ""; byte[] buf = new byte[4096]; int i = 0; try { while ((i = fis.read(buf)) != -1) { text += new String(buf, 0, i); } } catch (IOException ignored) { } helpcache.put(mnemo, text); }
/** * scans inside a jar for possible language directories * * @param file the jar file * @param paths the list to add to * @param helproot the root directory of all help directories */ private void searchJarPath(File file, List<String> paths) { if (file.getName().toLowerCase().endsWith(".jar") && file.isFile()) { try { JarInputStream jis; JarEntry je; jis = new JarInputStream(new BufferedInputStream(new FileInputStream(file))); je = jis.getNextJarEntry(); while (je != null) { if (je.getName().startsWith(helproot)) { String[] name = je.getName().split("/"); if (name.length > 1) { addToList(paths, name[1]); } } je = jis.getNextJarEntry(); } jis.close(); } catch (IOException ignored) { } } }