List of usage examples for java.util.jar JarFile getJarEntry
public JarEntry getJarEntry(String name)
From source file:JarRead.java
public static void main(String args[]) throws IOException { if (args.length != 2) { System.out.println("Please provide a JAR filename and file to read"); System.exit(-1);//from w ww . jav a2s . c o m } JarFile jarFile = new JarFile(args[0]); JarEntry entry = jarFile.getJarEntry(args[1]); InputStream input = jarFile.getInputStream(entry); process(input); jarFile.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JarFile jarfile = new JarFile(new File("filename.jar"), true, JarFile.OPEN_READ); JarEntry entry = jarfile.getJarEntry("fileName"); }
From source file:com.mirth.connect.server.launcher.MirthLauncher.java
public static void main(String[] args) { try {/*w w w . java2 s . c om*/ try { uninstallPendingExtensions(); installPendingExtensions(); } catch (Exception e) { logger.error("Error uninstalling or installing pending extensions.", e); } Properties mirthProperties = new Properties(); String includeCustomLib = null; try { mirthProperties.load(new FileInputStream(new File(MIRTH_PROPERTIES_FILE))); includeCustomLib = mirthProperties.getProperty(PROPERTY_INCLUDE_CUSTOM_LIB); createAppdataDir(mirthProperties); } catch (Exception e) { logger.error("Error creating the appdata directory.", e); } ManifestFile mirthServerJar = new ManifestFile("server-lib/mirth-server.jar"); ManifestFile mirthClientCoreJar = new ManifestFile("server-lib/mirth-client-core.jar"); ManifestDirectory serverLibDir = new ManifestDirectory("server-lib"); serverLibDir.setExcludes(new String[] { "mirth-client-core.jar" }); List<ManifestEntry> manifestList = new ArrayList<ManifestEntry>(); manifestList.add(mirthServerJar); manifestList.add(mirthClientCoreJar); manifestList.add(serverLibDir); // We want to include custom-lib if the property isn't found, or if it equals "true" if (includeCustomLib == null || Boolean.valueOf(includeCustomLib)) { manifestList.add(new ManifestDirectory("custom-lib")); } ManifestEntry[] manifest = manifestList.toArray(new ManifestEntry[manifestList.size()]); // Get the current server version JarFile mirthClientCoreJarFile = new JarFile(mirthClientCoreJar.getName()); Properties versionProperties = new Properties(); versionProperties.load(mirthClientCoreJarFile .getInputStream(mirthClientCoreJarFile.getJarEntry("version.properties"))); String currentVersion = versionProperties.getProperty("mirth.version"); List<URL> classpathUrls = new ArrayList<URL>(); addManifestToClasspath(manifest, classpathUrls); addExtensionsToClasspath(classpathUrls, currentVersion); URLClassLoader classLoader = new URLClassLoader(classpathUrls.toArray(new URL[classpathUrls.size()])); Class<?> mirthClass = classLoader.loadClass("com.mirth.connect.server.Mirth"); Thread mirthThread = (Thread) mirthClass.newInstance(); mirthThread.setContextClassLoader(classLoader); mirthThread.start(); } catch (Exception e) { e.printStackTrace(); } }
From source file:net.minecraftforge.fml.common.patcher.GenDiffSet.java
public static void main(String[] args) throws IOException { String sourceJar = args[0]; //Clean Vanilla jar minecraft.jar or minecraft_server.jar String targetDir = args[1]; //Directory containing obfed output classes, typically mcp/reobf/minecraft String deobfData = args[2]; //Path to FML's deobfusication_data.lzma String outputDir = args[3]; //Path to place generated .binpatch String killTarget = args[4]; //"true" if we should destroy the target file if it generated a successful .binpatch LogManager.getLogger("GENDIFF").log(Level.INFO, String.format("Creating patches at %s for %s from %s", outputDir, sourceJar, targetDir)); Delta delta = new Delta(); FMLDeobfuscatingRemapper remapper = FMLDeobfuscatingRemapper.INSTANCE; remapper.setupLoadOnly(deobfData, false); JarFile sourceZip = new JarFile(sourceJar); boolean kill = killTarget.equalsIgnoreCase("true"); File f = new File(outputDir); f.mkdirs();//from w ww . j a va 2 s .c o m for (String name : remapper.getObfedClasses()) { // Logger.getLogger("GENDIFF").info(String.format("Evaluating path for data :%s",name)); String fileName = name; String jarName = name; if (RESERVED_NAMES.contains(name.toUpperCase(Locale.ENGLISH))) { fileName = "_" + name; } File targetFile = new File(targetDir, fileName.replace('/', File.separatorChar) + ".class"); jarName = jarName + ".class"; if (targetFile.exists()) { String sourceClassName = name.replace('/', '.'); String targetClassName = remapper.map(name).replace('/', '.'); JarEntry entry = sourceZip.getJarEntry(jarName); byte[] vanillaBytes = toByteArray(sourceZip, entry); byte[] patchedBytes = Files.toByteArray(targetFile); byte[] diff = delta.compute(vanillaBytes, patchedBytes); ByteArrayDataOutput diffOut = ByteStreams.newDataOutput(diff.length + 50); // Original name diffOut.writeUTF(name); // Source name diffOut.writeUTF(sourceClassName); // Target name diffOut.writeUTF(targetClassName); // exists at original diffOut.writeBoolean(entry != null); if (entry != null) { diffOut.writeInt(Hashing.adler32().hashBytes(vanillaBytes).asInt()); } // length of patch diffOut.writeInt(diff.length); // patch diffOut.write(diff); File target = new File(outputDir, targetClassName + ".binpatch"); target.getParentFile().mkdirs(); Files.write(diffOut.toByteArray(), target); Logger.getLogger("GENDIFF").info(String.format("Wrote patch for %s (%s) at %s", name, targetClassName, target.getAbsolutePath())); if (kill) { targetFile.delete(); Logger.getLogger("GENDIFF").info(String.format(" Deleted target: %s", targetFile.toString())); } } } sourceZip.close(); }
From source file:com.jaspersoft.jasperserver.api.engine.jasperreports.util.JarsClassLoader.java
protected static JarFileEntry getJarEntry(JarFile jar, String name) { if (name.startsWith("/")) { name = name.substring(1);//w w w . ja v a 2s .c om } JarFileEntry jarEntry = null; JarEntry entry = jar.getJarEntry(name); if (entry != null) { jarEntry = new JarFileEntry(jar, entry); } return jarEntry; }
From source file:com.baomidou.framework.common.JarHelper.java
public static List<String> readLines(JarFile jarFile, String fileName) throws IOException { if (jarFile == null || StringUtils.isEmpty(fileName)) { return null; }/*from w ww . ja v a 2 s. c o m*/ List<String> lines = new ArrayList<String>(); JarEntry entry = jarFile.getJarEntry(fileName); InputStream inputStream = jarFile.getInputStream(entry); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String line; while ((line = bufferedReader.readLine()) != null) { lines.add(line); } bufferedReader.close(); inputStreamReader.close(); return lines; }
From source file:io.joynr.util.JoynrUtil.java
public static void copyDirectoryFromJar(String jarName, String srcDir, File tmpDir) throws IOException { JarFile jf = null; JarInputStream jarInputStream = null; try {//from ww w. jav a 2 s . c o m jf = new JarFile(jarName); JarEntry je = jf.getJarEntry(srcDir); if (je.isDirectory()) { FileInputStream fis = new FileInputStream(jarName); BufferedInputStream bis = new BufferedInputStream(fis); jarInputStream = new JarInputStream(bis); JarEntry ze = null; while ((ze = jarInputStream.getNextJarEntry()) != null) { if (ze.isDirectory()) { continue; } if (ze.getName().contains(je.getName())) { InputStream is = jf.getInputStream(ze); String name = ze.getName().substring(ze.getName().lastIndexOf("/") + 1); File tmpFile = new File(tmpDir + "/" + name); //File.createTempFile(file.getName(), "tmp"); tmpFile.deleteOnExit(); OutputStream outputStreamRuntime = new FileOutputStream(tmpFile); copyStream(is, outputStreamRuntime); } } } } finally { if (jf != null) { jf.close(); } if (jarInputStream != null) { jarInputStream.close(); } } }
From source file:de.xirp.plugin.PluginLoader.java
/** * Looks for plugins in the plugins directory. Plugins which needs * are not full filled are not accepted. * //from www.j a v a 2 s. c o m * @param manager * instance of the plugin manager which is used for * notifying the application about the loading * progress. */ protected static void searchPlugins(PluginManager manager) { logClass.info(I18n.getString("PluginLoader.log.searchPlugins")); //$NON-NLS-1$ // Get all Files in the Plugin Directory with // Filetype jar File pluginDir = new File(Constants.PLUGIN_DIR); File[] fileList = pluginDir.listFiles(new FilenameFilter() { public boolean accept(@SuppressWarnings("unused") File dir, String filename) { return filename.endsWith(".jar"); //$NON-NLS-1$ } }); if (fileList != null) { double perFile = 1.0 / fileList.length; double cnt = 0; try { // Iterate over all jars and try to find // the plugin.properties file // The file is loaded and Information // extracted to PluginInfo // Plugin is added to List of Plugins for (File f : fileList) { String path = f.getAbsolutePath(); if (!plugins.containsKey(path)) { manager.throwLoaderProgressEvent( I18n.getString("PluginLoader.progress.searchingInFile", f.getName()), cnt); //$NON-NLS-1$ JarFile jar = new JarFile(path); JarEntry entry = jar.getJarEntry("plugin.properties"); //$NON-NLS-1$ if (entry != null) { InputStream input = jar.getInputStream(entry); Properties props = new Properties(); props.load(input); String mainClass = props.getProperty("plugin.mainclass"); //$NON-NLS-1$ PluginInfo info = new PluginInfo(path, mainClass, props); String packageName = ClassUtils.getPackageName(mainClass) + "." //$NON-NLS-1$ + AbstractPlugin.DEFAULT_BUNDLE_NAME; String bundleBaseName = packageName.replaceAll("\\.", "/"); //$NON-NLS-1$ //$NON-NLS-2$ for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) { JarEntry ent = entries.nextElement(); String name = ent.getName(); if (name.indexOf(bundleBaseName) != -1) { String locale = name .substring(name.indexOf(AbstractPlugin.DEFAULT_BUNDLE_NAME)); locale = locale.replaceAll(AbstractPlugin.DEFAULT_BUNDLE_NAME + "_", //$NON-NLS-1$ ""); //$NON-NLS-1$ locale = locale.substring(0, locale.indexOf(".properties")); //$NON-NLS-1$ Locale loc = new Locale(locale); info.addAvailableLocale(loc); } } PluginManager.extractAll(info); if (isPlugin(info)) { plugins.put(mainClass, info); } } } cnt += perFile; } } catch (IOException e) { logClass.error( I18n.getString("PluginLoader.log.errorSearchingforPlugins") + Constants.LINE_SEPARATOR, e); //$NON-NLS-1$ } } checkAllNeeds(); logClass.info(I18n.getString("PluginLoader.log.searchPluginsCompleted") + Constants.LINE_SEPARATOR); //$NON-NLS-1$ }
From source file:com.cisco.dbds.utils.configfilehandler.ConfigFileHandler.java
/** * Load jar cong file./* w w w . j a v a 2 s . c o m*/ * * @param Utilclass the utilclass */ public static void loadJarCongFile(Class Utilclass) { try { String path = Utilclass.getResource("").getPath(); path = path.substring(6, path.length() - 1); path = path.split("!")[0]; System.out.println(path); JarFile jarFile = new JarFile(path); final Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { final JarEntry entry = entries.nextElement(); if (entry.getName().contains(".properties")) { System.out.println("Jar File Property File: " + entry.getName()); JarEntry fileEntry = jarFile.getJarEntry(entry.getName()); InputStream input = jarFile.getInputStream(fileEntry); setSystemvariable(input); InputStreamReader isr = new InputStreamReader(input); BufferedReader reader = new BufferedReader(isr); String line; while ((line = reader.readLine()) != null) { System.out.println("Jar file" + line); } reader.close(); } } jarFile.close(); } catch (Exception e) { System.out.println("Jar file reading Error"); } }
From source file:com.opengamma.examples.simulated.tool.ExampleDatabasePopulator.java
private static String unpackJar(URL resource) { String file = resource.getPath(); if (file.contains(".jar!/")) { s_logger.info("Unpacking zip file located within a jar file: {}", resource); String jarFileName = StringUtils.substringBefore(file, "!/"); if (jarFileName.startsWith("file:/")) { jarFileName = jarFileName.substring(5); if (SystemUtils.IS_OS_WINDOWS) { jarFileName = StringUtils.stripStart(jarFileName, "/"); }//from w w w . j a v a2s. c o m } else if (jarFileName.startsWith("file:/")) { jarFileName = jarFileName.substring(6); } jarFileName = StringUtils.replace(jarFileName, "%20", " "); String innerFileName = StringUtils.substringAfter(file, "!/"); innerFileName = StringUtils.replace(innerFileName, "%20", " "); s_logger.info("Unpacking zip file found jar file: {}", jarFileName); s_logger.info("Unpacking zip file found zip file: {}", innerFileName); try { JarFile jar = new JarFile(jarFileName); JarEntry jarEntry = jar.getJarEntry(innerFileName); try (InputStream in = jar.getInputStream(jarEntry)) { File tempFile = File.createTempFile("simulated-examples-database-populator-", ".zip"); tempFile.deleteOnExit(); try (OutputStream out = new FileOutputStream(tempFile)) { IOUtils.copy(in, out); } file = tempFile.getCanonicalPath(); } } catch (IOException ex) { throw new OpenGammaRuntimeException("Unable to open file within jar file: " + resource, ex); } s_logger.debug("Unpacking zip file extracted to: {}", file); } return file; }