public static void copyJarResourcesRecursively(File destination, JarURLConnection jarConnection) { JarFile jarFile; try { jarFile = jarConnection.getJarFile(); } catch (Exception e) { _.die("Failed to get jar file)"); return; } 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) { die("Failed to copy resource: " + fileName); } finally { if (entryInputStream != null) { try { entryInputStream.close(); } catch (Exception e) { } } } } } } }
/** * Returns the name of the jar file main class, or null if no "Main-Class" manifest attributes was * defined. */ public String getMainClassName() throws IOException { URL u = new URL("jar", "", url + "!/"); JarURLConnection uc = (JarURLConnection) u.openConnection(); if (uc.getEntryName() == null) System.out.println("No Entry Name"); else System.out.println(uc.getEntryName()); if (uc == null) System.out.println("Connection is null"); Attributes attr = uc.getMainAttributes(); if (attr != null) System.out.println("Attributes has size: " + attr.size()); // output(attr); return attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null; }
public static boolean copyJarResourcesRecursively( final File destDir, final JarURLConnection jarConnection) throws IOException { final JarFile jarFile = jarConnection.getJarFile(); for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements(); ) { final JarEntry entry = e.nextElement(); if (entry.getName().startsWith(jarConnection.getEntryName())) { final String filename = entry.getName(); final File f = new File(destDir, filename); if (!entry.isDirectory()) { final InputStream entryInputStream = jarFile.getInputStream(entry); if (!FileUtils.copyStream(entryInputStream, f)) { return false; } entryInputStream.close(); } else { if (!FileUtils.ensureDirectoryExists(f)) { throw new IOException("Could not create directory: " + f.getAbsolutePath()); } } } } return true; }
/** * Return the directories in a jar URI, relative to the jar entry, if any. . Jar URLS have several * forms, the most common being: <code>jar:file:///foo/bar.jar/!/bif/baz</code>, which means that * the jar file /foo/bar.jar has a directory or file name bif/baz. If such a file is passed to * this method, then any directories in the jar file directory bif/baz will be returned. * * @param jarURL The Jar URL for which we are to look for directories. * @return An list of Strings that name the directories * @exception IOException If opening the connection fails or if getting the jar file from the * connection fails */ public static List<String> jarURLDirectories(URL jarURL) throws IOException { List<String> directories = new LinkedList<String>(); JarURLConnection connection = (JarURLConnection) (jarURL.openConnection()); String jarEntryName = connection.getEntryName(); if (jarEntryName.endsWith("/")) { jarEntryName = jarEntryName.substring(0, jarEntryName.length() - 1); } JarFile jarFile = connection.getJarFile(); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String name = entry.getName(); int jarEntryIndex = name.indexOf(jarEntryName + "/"); int jarEntrySlashIndex = jarEntryIndex + jarEntryName.length() + 1; int nextSlashIndex = name.indexOf("/", jarEntrySlashIndex); int lastSlashIndex = name.indexOf("/", jarEntrySlashIndex); if (jarEntryIndex > -1 && jarEntrySlashIndex > -1 && nextSlashIndex > -1 && nextSlashIndex == lastSlashIndex && nextSlashIndex == name.length() - 1 && entry.isDirectory()) { directories.add(name); } } jarFile.close(); return directories; }
private List<JavaFileObject> processJar(URL packageFolderURL) { List<JavaFileObject> result = new ArrayList<JavaFileObject>(); 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_EXTENSION)) { URI uri = URI.create(jarUri + "!/" + name); String binaryName = name.replaceAll("/", "."); binaryName = binaryName.replaceAll(CLASS_FILE_EXTENSION + "$", ""); result.add(new CustomJavaFileObject(binaryName, uri)); } } } catch (Exception e) { throw new RuntimeException("Wasn't able to open " + packageFolderURL + " as a jar file", e); } return result; }
/** * Extracts the entry name for the grammar from a given JAR or ZIP URL. The name is either given * in the JAR entry part of the URL, or it is taken to be the name of the archive file. * * @param url the URL to be parsed; guaranteed to be a JAR or ZIP * @return the name; non-null * @throws IllegalArgumentException if no name can be found according to the above rules * @throws IOException if the URL cannot be opened */ private String extractEntryName(URL url) throws IOException { String result; JarURLConnection connection = (JarURLConnection) url.openConnection(); result = connection.getEntryName(); if (result == null) { result = FileType.getPureName(new File(connection.getJarFileURL().getPath())); } return result; }