private static Map<String, SQLCase> loadSQLCasesFromJar(final String path, final File file) throws IOException, JAXBException { Map<String, SQLCase> result = new TreeMap<>(); try (JarFile jar = new JarFile(file)) { Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { String name = entries.nextElement().getName(); if (name.startsWith(path + "/") && name.endsWith(".xml")) { fillSQLMap(result, SQLCasesLoader.class.getClassLoader().getResourceAsStream(name)); } } } return result; }
private void loadJarContent(JarURLConnection url, String packageName, Set<InputStream> streams) throws IOException { // Using a JarURLConnection will load the JAR from the cache when using Webstart 1.6 // In Webstart 1.5, the URL will point to the cached JAR on the local filesystem JarFile jarFile = url.getJarFile(); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.getName().startsWith(packageName)) { streams.add(jarFile.getInputStream(entry)); } } }
protected String identifyPluginShortName(File t) { try { JarFile j = new JarFile(t); try { String name = j.getManifest().getMainAttributes().getValue("Short-Name"); if (name!=null) return name; } finally { j.close(); } } catch (IOException e) { LOGGER.log(WARNING, "Failed to identify the short name from "+t,e); } return FilenameUtils.getBaseName(t.getName()); // fall back to the base name of what's uploaded }
private static String getManifestKey(JarFile jarFile, String key) { try { Manifest manifest = jarFile.getManifest(); if (manifest != null) { Attributes attributes = manifest.getMainAttributes(); return attributes.getValue(key); } } catch (IOException e) { LOG.error("Exception while trying to read key {} from manifest of {}", key, jarFile.getName(), e); } return null; }
public InputStream openClassfile(String classname) throws NotFoundException { try { String jarname = classname.replace('.', '/') + ".class"; JarEntry je = jarfile.getJarEntry(jarname); if (je != null) return jarfile.getInputStream(je); else return null; // not found } catch (IOException e) {} throw new NotFoundException("broken jar file?: " + jarfile.getName()); }
jf = new JarFile(fileName); for (Enumeration<JarEntry> e = jf.entries(); e.hasMoreElements(); ) { JarEntry je = e.nextElement(); String outFileName = outputPath + je.getName(); File f = new File(outFileName); if (je.isDirectory()) { if (!f.exists()) { f.mkdirs(); pf.mkdirs(); InputStream in = jf.getInputStream(je); OutputStream out = new BufferedOutputStream( new FileOutputStream(f)); byte[] buffer = new byte[2048]; int nBytes; while ((nBytes = in.read(buffer)) > 0) { out.write(buffer, 0, nBytes); in.close(); if (jf != null) { try { jf.close(); } catch (IOException e) { e.printStackTrace();
/** * Find all .tld files in the given jar. * * @param uri the uri to jar file * @return the collection of tlds as url references * @throws IOException if unable to scan the jar file */ public Collection<URL> getTlds (URI uri) throws IOException { HashSet<URL> tlds = new HashSet<URL>(); String jarUri = uriJarPrefix(uri, "!/"); URL url = new URL(jarUri); JarURLConnection jarConn = (JarURLConnection) url.openConnection(); jarConn.setUseCaches(Resource.getDefaultUseCaches()); JarFile jarFile = jarConn.getJarFile(); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry e = entries.nextElement(); String name = e.getName(); if (name.startsWith("META-INF") && name.endsWith(".tld")) { tlds.add(new URL(jarUri + name)); } } if (!Resource.getDefaultUseCaches()) jarFile.close(); return tlds; }
java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile); java.util.Enumeration enumEntries = jar.entries(); while (enumEntries.hasMoreElements()) { java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement(); java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName()); if (file.isDirectory()) { // if its a directory, create it f.mkdir(); continue; } java.io.InputStream is = jar.getInputStream(file); // get the input stream java.io.FileOutputStream fos = new java.io.FileOutputStream(f); while (is.available() > 0) { // write contents of 'is' to 'fos' fos.write(is.read()); } fos.close(); is.close(); }
public static void analyzeJar(String jarFile) { try { Enumeration<JarEntry> e = new JarFile(jarFile).entries(); while (e.hasMoreElements()) { ZipEntry en = (ZipEntry) e.nextElement(); String n = en.getName(); if (!n.endsWith(".class")) continue; n = n.substring(0, n.length() - 6).replace('/','.'); analyzeClass(n); } } catch (Exception e) { e.printStackTrace(); } } }
public static void copyJarResourcesRecursively(File destination, JarURLConnection jarConnection) { JarFile jarFile; try { jarFile = jarConnection.getJarFile(); } catch (Exception e) { _.die("Failed to get jar file)"); Enumeration<JarEntry> em = jarFile.entries(); while (em.hasMoreElements()) { JarEntry entry = em.nextElement(); if (entry.getName().startsWith(jarConnection.getEntryName())) { String fileName = StringUtils.removeStart(entry.getName(), jarConnection.getEntryName()); if (!fileName.equals("/")) { // exclude the directory InputStream entryInputStream = null; try { entryInputStream = jarFile.getInputStream(entry); FileUtils.copyInputStreamToFile(entryInputStream, new File(destination, fileName)); } catch (Exception e) { if (entryInputStream != null) { try { entryInputStream.close(); } catch (Exception e) {
JarFile jar = null; try { jarURL = new URL("file:/" + file.getCanonicalPath()); jarURL = new URL("jar:" + jarURL.toExternalForm() + "!/"); JarURLConnection conn = (JarURLConnection) jarURL.openConnection(); jar = conn.getJarFile(); } catch (Exception e) { Enumeration<JarEntry> e = jar.entries(); while (e.hasMoreElements()) { JarEntry entry = e.nextElement(); if (entry.isDirectory()) { if (entry.getName().toUpperCase().equals("META-INF/")) { continue; map.put(new URL(jarURL.toExternalForm() + entry.getName()), packageNameFor(entry)); } catch (MalformedURLException murl) {
File directory = new File(url.getFile()); JarURLConnection conn = (JarURLConnection) url.openConnection(); JarFile jarFile = conn.getJarFile(); Enumeration<JarEntry> e = jarFile.entries(); while (e.hasMoreElements()) { JarEntry entry = e.nextElement(); String entryname = entry.getName(); if (!entry.isDirectory() && entryname.endsWith(".class")) { String classname = entryname.substring(0, entryname.length() - 6); if (classname.startsWith("/")) {
Manifest input = jar.getManifest(); Manifest output = new Manifest(); Attributes main = output.getMainAttributes(); if (input != null) { main.putAll(input.getMainAttributes()); main.putValue("Manifest-Version", "1.0"); main.putValue("Created-By", "1.6.0_21 (d2j-" + AbstractJarSign.class.getPackage().getImplementationVersion() + ")"); for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) { JarEntry entry = e.nextElement(); byName.put(entry.getName(), entry); String name = entry.getName(); if (!entry.isDirectory() && !name.equals(JarFile.MANIFEST_NAME) && !stripPattern.matcher(name).matches()) { InputStream data = jar.getInputStream(entry); while ((num = data.read(buffer)) > 0) { md.update(buffer, 0, num);
JarFile jarFile = new JarFile(pathToJar); Enumeration<JarEntry> e = jarFile.entries(); URL[] urls = { new URL("jar:file:" + pathToJar+"!/") }; URLClassLoader cl = URLClassLoader.newInstance(urls); while (e.hasMoreElements()) { JarEntry je = e.nextElement(); if(je.isDirectory() || !je.getName().endsWith(".class")){ continue; } // -6 because of .class String className = je.getName().substring(0,je.getName().length()-6); className = className.replace('/', '.'); Class c = cl.loadClass(className); }
throws IOException { URLConnection con = rootDirURL.openConnection(); JarFile jarFile; String jarFileUrl; jarFile = jarCon.getJarFile(); jarFileUrl = jarCon.getJarFileURL().toExternalForm(); JarEntry jarEntry = jarCon.getJarEntry(); rootEntryPath = (jarEntry != null ? jarEntry.getName() : ""); closeJarFile = !jarCon.getUseCaches(); String urlFile = rootDirURL.getFile(); try { int separatorIndex = urlFile.indexOf(ResourceUtils.WAR_URL_SEPARATOR); jarFile = new JarFile(urlFile); jarFileUrl = urlFile; rootEntryPath = ""; for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) { JarEntry entry = entries.nextElement(); String entryPath = entry.getName(); if (entryPath.startsWith(rootEntryPath)) { String relativePath = entryPath.substring(rootEntryPath.length()); jarFile.close();
List<FileMeta> getResourceListing(URL dirURL, String path) throws IOException { List<FileMeta> listFiles = new ArrayList<>(); if (null == CACHE_JAR_FILE) { //strip out only the JAR file String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); CACHE_JAR_FILE = new JarFile(URLDecoder.decode(jarPath, "UTF-8")); } Enumeration<JarEntry> entries = CACHE_JAR_FILE.entries(); while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); String name = jarEntry.getName(); if (!name.startsWith(path) || name.equals(path + "/")) { continue; } FileMeta fileMeta = new FileMeta(); fileMeta.name = name.substring(path.length() + 1); fileMeta.isDirectory = jarEntry.isDirectory(); if (!fileMeta.isDirectory) { fileMeta.length = jarEntry.getSize(); } listFiles.add(fileMeta); } return listFiles; }
|| part.toLowerCase().endsWith(".zip")) { try (JarFile jf = new JarFile(p.toFile())) { Enumeration<? extends ZipEntry> zipEnums = jf.entries(); while (zipEnums.hasMoreElements()) { ZipEntry entry = zipEnums.nextElement(); if (!entry.isDirectory() && entry.getName().equals(propFileName)) { try (InputStreamReader reader = new InputStreamReader(jf.getInputStream(entry))) { Properties info = new Properties(); info.load(reader);
private static List<JavaFileObject> browseJar(URL packageFolderURL) { List<JavaFileObject> result = new ArrayList<>(); try { String jarUri = packageFolderURL.toExternalForm().split("!")[0]; JarURLConnection jarConn = (JarURLConnection) packageFolderURL.openConnection(); String rootEntryName = jarConn.getEntryName(); int rootEnd = rootEntryName.length() + 1; Enumeration<JarEntry> entryEnum = jarConn.getJarFile().entries(); while (entryEnum.hasMoreElements()) { JarEntry jarEntry = entryEnum.nextElement(); String name = jarEntry.getName(); if (name.startsWith(rootEntryName) && name.indexOf('/', rootEnd) == -1 && name.endsWith(CLASS_FILE)) { String binaryName = name.replaceAll("/", ".").replaceAll(CLASS_FILE + "$", ""); result.add(new CustomJavaFileObject(URI.create(jarUri + "!/" + name), JavaFileObject.Kind.CLASS, binaryName)); } } } catch (Exception e) { throw new RuntimeException(packageFolderURL + " is not a JAR file", e); } return result; } }
final String path = "sample/folder"; final File jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath()); if(jarFile.isFile()) { // Run with JAR file final JarFile jar = new JarFile(jarFile); final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar while(entries.hasMoreElements()) { final String name = entries.nextElement().getName(); if (name.startsWith(path + "/")) { //filter according to the path System.out.println(name); } } jar.close(); } else { // Run with IDE final URL url = Launcher.class.getResource("/" + path); if (url != null) { try { final File apps = new File(url.toURI()); for (File app : apps.listFiles()) { System.out.println(app); } } catch (URISyntaxException ex) { // never happens } } }
private byte[] loadWithSecurity(String path) throws IOException { URL scriptUrl = URLClassLoader.getSystemResource(path); if (scriptUrl.getProtocol().equals("jar")) { String jarPath = scriptUrl.getPath().substring(5, scriptUrl.getPath().indexOf("!")); JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8")); Enumeration<JarEntry> ens = jar.entries(); while (ens.hasMoreElements()) { JarEntry en = ens.nextElement(); if (!en.isDirectory()) { if (en.toString().equals(path)) { byte[] data = readAll(jar.getInputStream(en), en.getSize()); CodeSigner[] signers = en.getCodeSigners(); canLoadPack = signers != null && signers.length != 0; return data; } } } } return null; }