List of usage examples for java.util.jar JarFile getInputStream
public synchronized InputStream getInputStream(ZipEntry ze) throws IOException
From source file:com.heliosdecompiler.helios.handler.addons.JarLauncher.java
@Override public void run(File file) { JarFile jarFile = null; InputStream inputStream = null; try {/*from w w w.ja va 2s. c o m*/ System.out.println("Loading addon: " + file.getAbsolutePath()); jarFile = new JarFile(file); ZipEntry entry = jarFile.getEntry("addon.json"); if (entry != null) { inputStream = jarFile.getInputStream(entry); JsonObject jsonObject = Json.parse(new InputStreamReader(inputStream, StandardCharsets.UTF_8)) .asObject(); String main = jsonObject.get("main").asString(); URL[] url = new URL[] { file.toURI().toURL() }; ClassLoader classLoader = AccessController .doPrivileged((PrivilegedAction<ClassLoader>) () -> new URLClassLoader(url, Helios.class.getClassLoader())); Class<?> clazz = Class.forName(main, true, classLoader); if (Addon.class.isAssignableFrom(clazz)) { Addon addon = Addon.class.cast(clazz.newInstance()); registerAddon(addon.getName(), addon); } else { throw new IllegalArgumentException("Addon main does not extend Addon"); } } else { throw new IllegalArgumentException("No addon.json found"); } } catch (Exception e) { ExceptionHandler.handle(e); } finally { IOUtils.closeQuietly(jarFile); IOUtils.closeQuietly(inputStream); } }
From source file:org.jboss.tools.jst.web.kb.taglib.TagLibraryManager.java
private static File convertUriToFile(String uri) throws IOException { File file = tempFiles.get(uri); if (file != null && file.exists()) { return file; }/* w w w. j av a 2 s . com*/ URL url = new URL(uri); String filePath = url.getFile(); file = new File(filePath); if (!file.exists()) { URLConnection c = url.openConnection(); if (c instanceof JarURLConnection) { JarURLConnection connection = (JarURLConnection) c; JarFile jar = connection.getJarFile(); JarEntry entry = connection.getJarEntry(); File entryFile = new File(entry.getName()); String name = entryFile.getName(); String prefix = name; String sufix = null; int i = name.lastIndexOf('.'); if (i > 0 && i < name.length()) { prefix = name.substring(0, i); sufix = name.substring(i); } while (prefix.length() < 3) { prefix += "_"; } WebKbPlugin plugin = WebKbPlugin.getDefault(); if (plugin != null) { //The plug-in instance can be null at shutdown, when the plug-in is stopped. IPath path = plugin.getStateLocation(); File tmp = new File(path.toFile(), "tmp"); //$NON-NLS-1$ tmp.mkdirs(); file = File.createTempFile(prefix, sufix, tmp); file.deleteOnExit(); InputStream in = null; try { in = jar.getInputStream(entry); FileOutputStream out = new FileOutputStream(file); IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); } } } } if (file.exists()) { tempFiles.put(uri, file); } else { file = null; } return file; }
From source file:com.jrummyapps.busybox.signing.ZipSigner.java
/** Copy all the files in a manifest from input to output. */ private static void copyFiles(Manifest manifest, JarFile in, JarArchiveOutputStream out, long timestamp) throws IOException { final byte[] buffer = new byte[4096]; int num;/*from ww w . j a v a 2s . c om*/ final Map<String, Attributes> entries = manifest.getEntries(); final List<String> names = new ArrayList<>(entries.keySet()); Collections.sort(names); for (final String name : names) { final JarEntry inEntry = in.getJarEntry(name); if (inEntry.getMethod() == JarArchiveEntry.STORED) { // Preserve the STORED method of the input entry. out.putArchiveEntry(new JarArchiveEntry(inEntry)); } else { // Create a new entry so that the compressed len is recomputed. final JarArchiveEntry je = new JarArchiveEntry(name); je.setTime(timestamp); out.putArchiveEntry(je); } final InputStream data = in.getInputStream(inEntry); while ((num = data.read(buffer)) > 0) { out.write(buffer, 0, num); } out.flush(); out.closeArchiveEntry(); } }
From source file:net.sf.freecol.FreeCol.java
/** * Get a stream for the default splash file. * * Note: Not bothering to check for nulls as this is called in try * block that ignores all exceptions.//from w w w.j a v a2s . c o m * * @param juc The <code>JarURLConnection</code> to extract from. * @return A suitable <code>InputStream</code>, or null on error. */ private static InputStream getDefaultSplashStream(JarURLConnection juc) throws IOException { JarFile jf = juc.getJarFile(); ZipEntry ze = jf.getEntry(SPLASH_DEFAULT); return jf.getInputStream(ze); }
From source file:eu.trentorise.opendata.josman.Josmans.java
/** * * Extracts the files starting with dirPath from {@code file} to * {@code destDir}/*from w w w .ja v a 2s . c o m*/ * * @param dirPath the prefix used for filtering. If empty the whole jar * content is extracted. */ public static void copyDirFromJar(File jarFile, File destDir, String dirPath) { checkNotNull(jarFile); checkNotNull(destDir); checkNotNull(dirPath); String normalizedDirPath; if (dirPath.startsWith("/")) { normalizedDirPath = dirPath.substring(1); } else { normalizedDirPath = dirPath; } try { JarFile jar = new JarFile(jarFile); java.util.Enumeration enumEntries = jar.entries(); while (enumEntries.hasMoreElements()) { JarEntry jarEntry = (JarEntry) enumEntries.nextElement(); if (jarEntry.getName().startsWith(normalizedDirPath)) { File f = new File( destDir + File.separator + jarEntry.getName().substring(normalizedDirPath.length())); if (jarEntry.isDirectory()) { // if its a directory, create it f.mkdirs(); continue; } else { f.getParentFile().mkdirs(); } InputStream is = jar.getInputStream(jarEntry); // get the input stream FileOutputStream fos = new FileOutputStream(f); IOUtils.copy(is, fos); fos.close(); is.close(); } } } catch (Exception ex) { throw new RuntimeException("Error while extracting jar file! Jar source: " + jarFile.getAbsolutePath() + " destDir = " + destDir.getAbsolutePath(), ex); } }
From source file:org.spout.api.plugin.CommonPlugin.java
@Override public InputStream getResource(String path) { Validate.notNull(path);/*w w w . j a v a 2 s . c om*/ JarFile jar; try { jar = new JarFile(getFile()); } catch (IOException e) { return null; } JarEntry entry = jar.getJarEntry(path); try { return entry == null ? null : jar.getInputStream(entry); } catch (IOException e) { return null; } }
From source file:org.ebayopensource.turmeric.eclipse.typelibrary.ui.TypeLibraryUtil.java
private static String getTypeLibName(JarEntry entry, JarFile jarFile) throws SAXException, IOException, ParserConfigurationException { String typeLibName = "Lib Not Found"; InputStream stream = null;/*from w w w . j a va2 s .co m*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db; try { db = dbf.newDocumentBuilder(); stream = jarFile.getInputStream(entry); Document doc = db.parse(stream); doc.getDocumentElement().normalize(); typeLibName = doc.getDocumentElement().getAttribute(SOATypeLibraryConstants.ATTR_TYPE_INFO_LIBNAME); } catch (ParserConfigurationException e) { throw e; } catch (IOException e) { throw e; } catch (SAXException e) { throw e; } finally { if (stream != null) IOUtils.closeQuietly(stream); } return typeLibName; }
From source file:fr.husta.test.assembly.JarWithDependenciesTest.java
@Test public void checkMetaInfContent() throws IOException { // check META-INF/services/java.sql.Driver exists JarFile jar = new JarFile("target/issue-mvn-assembly-plugin-730-jar-with-dependencies.jar"); JarEntry entry = jar.getJarEntry("META-INF/services/java.sql.Driver"); if (entry == null) { fail("the file 'META-INF/services/java.sql.Driver' should exist in jar-with-dependencies"); }// w w w . j a v a2 s . co m // Content should be "org.postgresql.Driver" InputStream is = jar.getInputStream(entry); String content = IOUtils.toString(is, "UTF-8"); System.out.println("JDBC Driver found : " + content.substring(0, content.indexOf("\n"))); assertEquals("org.postgresql.Driver", content.substring(0, content.indexOf("\n"))); // if test fails and content == "sun.jdbc.odbc.JdbcOdbcDriver", // it means it comes from jre/lib/resources.jar!/META-INF/services/java.sql.Driver (which is unwanted) }
From source file:de.smartics.maven.plugin.jboss.modules.util.classpath.AbstractProjectClassLoader.java
/** * Opens a reader to the class file within the archive. * * @param className the name of the class to load. * @param fileName the name of the class file to load. * @param dirName the name of the directory the archive file is located. * @return the reader to the source file for the given class in the archive. * @throws ClassNotFoundException if the class cannot be found. */// ww w .jav a 2 s . c om protected Class<?> loadClassFromLibrary(final String className, final String fileName, final File dirName) throws ClassNotFoundException { ensurePackageProvided(className); try { final JarFile jarFile = new JarFile(dirName); // NOPMD final JarEntry entry = jarFile.getJarEntry(fileName); if (entry != null) { InputStream in = null; try { in = new BufferedInputStream(jarFile.getInputStream(entry)); final byte[] data = IOUtils.toByteArray(in); final Class<?> clazz = defineClass(className, data, 0, data.length); return clazz; } finally { IOUtils.closeQuietly(in); } } } catch (final IOException e) { final String message = "Cannot load class '" + className + "' from file '" + dirName + "'."; LOG.fine(message); throw new ClassNotFoundException(message, e); } final String message = "Cannot load class '" + className + "' from file '" + dirName + "'."; LOG.fine(message); throw new ClassNotFoundException(message); }
From source file:com.egreen.tesla.server.api.component.Component.java
/** * * Init configurations from Component/*ww w . ja v a2s .c o m*/ * * @param jf * @throws IOException * @throws ConfigurationException */ private void setConfiguraton(JarFile jf) throws IOException, ConfigurationException { JarEntry entry = jf.getJarEntry(TESLAR_WIDGET_MAINIFIESTXML); InputStream inputStream = jf.getInputStream(entry); configuration = new XMLConfiguration(); configuration.load(inputStream); componentID = configuration.getString(Settings.COMPONENT_ID.getType()); LOGGER.info("Loadding : " + componentID); component_base = context.getRealPath("/") + "/components/" + componentID; }