public static List<File> extractFilesInLibDirAndReturnFiles(File aJarFile, Predicate<JarEntry> extractFilter, File outputTmpDir) { List<File> newClassPath = new ArrayList<>(); try (JarFile jarFile = new JarFile(aJarFile)) { List<File> extractedJars = jarFile.stream() .filter(extractFilter) .map(jarEntry -> { String jarFileBaseName = FilenameUtils.getName(jarEntry.getName()); File targetFile = new File(outputTmpDir, jarFileBaseName); return extractJarEntry(jarFile, jarEntry, targetFile); }) .collect(Collectors.toList()); // add deps in dir specified by `libDirManifestKey` newClassPath.addAll(extractedJars); } catch (IOException e) { throw new RuntimeException(e); } return newClassPath; }
private void run() { Handler.init(); try (JarFile jarFile = new JarFile(currentJarFile())) { String mainClassName = mainClassName(jarFile); List<URL> jarsInJar = jarFile.stream() .filter(FIND_JAR_FILES_UNDER_LIB_FOLDER) .map(jarEntry -> Handler.toOneJarUrl(jarEntry.getName())) .collect(Collectors.toList()); List<URL> urls = new ArrayList<>(); urls.addAll(additionalJarsInLibsDirectoryInCWD()); urls.addAll(jarsInJar); ClassLoader jcl = new URLClassLoader(urls.toArray(new URL[0]), getClass().getClassLoader()); Thread.currentThread().setContextClassLoader(jcl); Class<?> mainClass = jcl.loadClass(mainClassName); mainClass.getMethod("main", String[].class).invoke(null, new Object[]{args}); } catch (IOException | ReflectiveOperationException e) { e.printStackTrace(); System.exit(1); } }
JarURLConnection jarCon = (JarURLConnection) con; JarFile jarFile = jarCon.getJarFile(); jarFile.stream() .filter(entry -> { String name = entry.getName();
+ " tsDefOutputDirectory=" + tsDefOutputDirectory + " jsOutputDir=" + jsOutputDirectory); jarFile.stream() .filter(entry -> entry.getName().endsWith(".d.ts") && (entry.getName().startsWith("src/") || entry.getName().startsWith("META-INF/resources/"))) //
public static CandyDescriptor fromCandyJar(JarFile jarFile, String jsOutputDirPath) throws IOException { final Optional<JarEntry> pomXmlEntry = jarFile.stream().filter((JarEntry t) -> t.getName().endsWith("pom.xml")).findFirst(); final Optional<JarEntry> pomPropertiesEntry = jarFile.stream().filter((JarEntry t) -> t.getName().endsWith("pom.properties")).findFirst(); final ZipEntry metadataEntry = jarFile.getEntry("META-INF/candy-metadata.json"); final long lastUpdateTimestamp = jarFile.getEntry("META-INF/MANIFEST.MF").getTime(); if (jsDirEntry != null) { jarFile.stream().map(JarEntry::getName) .filter((String t) -> t.startsWith(jsDirPath) && t.endsWith(".js")) .forEach(jsFilesPaths::add);
jarFile.stream() .map(VersionedJarEntry::new) .filter(e->(includeDirectories||!e.isDirectory()) && e.isApplicable())
JarFile source = getZipFileFromFile(f);// have to adapt the return type of that method source.stream().forEach(ExceptionWrapper.wrapConsumer( e -> addEntryContent(out, source, e, entryNames)));
private static List<String> listClasses(JarFile jar) { return jar.stream() .filter(entry -> !entry.isDirectory() && entry.getName().endsWith(".class")) .map(path -> path.getName().replace("/", ".").substring(0, path.getName().length() - ".class".length())) .collect(Collectors.toList()); } }
private static List<String> forJarFile(JarFile jarFile) { return jarFile.stream().filter(entry -> !entry.isDirectory()).map(ZipEntry::getName).collect(toList()); }
private boolean isClassInsideJarFile(String classname, File jarFile) { String requestedJarEntryName = classname.replace(".", "/") + ".class"; try{ JarFile jarFileToSearchIn = new JarFile(jarFile, false, JarFile.OPEN_READ); return jarFileToSearchIn.stream().parallel().filter(jarEntry -> { return jarEntry.getName().equals(requestedJarEntryName); }).findAny().isPresent(); } catch(IOException ex){ // NO-OP } return false; } }
public static void filterInJar(JarFile jarFile, String fileNamePattern, Consumer<JarEntry> consumer) { Pattern pattern = Pattern.compile(fileNamePattern, "*"); jarFile.stream() .filter(entry -> !entry.isDirectory()) .filter(entry -> pattern.match(Paths.get(entry.getName()).getFileName().toString()) != null) .forEach(consumer); }
private static Collection<String> classEntriesInJar(Path jarPath, Set<String> packagePathsToScan) { Predicate<String> acceptedPackage; if (packagePathsToScan.isEmpty()) { acceptedPackage = ign -> true; } else { acceptedPackage = name -> packagePathsToScan.contains(packagePath(name)); } try (JarFile jarFile = new JarFile(jarPath.toFile())) { return jarFile.stream().map(JarEntry::getName).filter(name -> name.endsWith(CLASS_FILE_TYPE_SUFFIX)).filter(acceptedPackage) .collect(Collectors.toList()); } catch (IOException e) { throw new RuntimeException(e); } }
private void addParentFirstPatternsFromJar(File jar) throws IOException { JarFile jarFile = new JarFile(jar); jarFile.stream() .filter(jarEntry -> !jarEntry.isDirectory() && jarEntry.getName().endsWith(".class")) .map(jarEntry -> compileJarEntryPattern(trimDotClass(jarEntry.getName()))) .forEach(pattern -> parentFirst.add(pattern)); }
public static void filterInJar(JarFile jarFile, String fileNamePattern, Consumer<JarEntry> consumer) { Pattern pattern = Pattern.compile(fileNamePattern, "*"); jarFile.stream() .filter(entry -> !entry.isDirectory()) .filter(entry -> pattern.match(Paths.get(entry.getName()).getFileName().toString()) != null) .forEach(consumer); }
private List<JarEntry> getJarEntries() throws IOException { // Get the JAR file of the resource URL url = LwjglResourceFilePath.class.getClassLoader().getResource(getPath()); if (url == null) throw new SilenceException("Error, resource doesn't exist."); String jarUrl = url.toString(); String jarPath = URLDecoder.decode(jarUrl.substring(jarUrl.indexOf('/'), jarUrl.indexOf('!')), "UTF-8"); // Now get the JarEntry for this path JarFile jarFile = new JarFile(new File(jarPath)); List<JarEntry> entries = jarFile.stream().filter(e -> e.getName().startsWith(path)) .collect(Collectors.toList()); jarFile.close(); if (entries != null) return entries; throw new IOException("Cannot find the JAR entry for " + getPath() + " in JAR file " + jarPath); }
private void extractMainJar(JarFileInfo jarFileInfo) throws URISyntaxException { final String LIB_DIRECTORY = "lib/"; createTempDirectory(); jarFileInfo.getJarFile() .stream() .parallel() .filter(je -> !je.getName().toLowerCase().startsWith(LIB_DIRECTORY)) .forEach(je -> { try { JarEntryInfo jarEntryInfo = new JarEntryInfo(jarFileInfo, je); File tempFile = createFile(je, jarFileInfo.getJarFile().getInputStream(je)); debug(String.format("Loading inner JAR %s from temp file %s", jarEntryInfo.getJarEntry(), getFilenameForLog(tempFile))); files.add(new FileInfo(tempFile, je.getName())); } catch (IOException e) { throw new RuntimeException(String.format("Cannot load jar entries from jar %s", je.getName().toLowerCase()), e); } catch (EeClassLoaderException e) { throw new RuntimeException("ERROR on loading inner JAR: " + e.getMessageAll()); } }); }
private void extractMainJar(JarFileInfo jarFileInfo) throws URISyntaxException { final String LIB_DIRECTORY = "lib/"; createTempDirectory(); jarFileInfo.getJarFile() .stream() .parallel() .filter(je -> !je.getName().toLowerCase().startsWith(LIB_DIRECTORY)) .forEach(je -> { try { JarEntryInfo jarEntryInfo = new JarEntryInfo(jarFileInfo, je); File tempFile = createFile(je, jarFileInfo.getJarFile().getInputStream(je)); debug(String.format("Loading inner JAR %s from temp file %s", jarEntryInfo.getJarEntry(), getFilenameForLog(tempFile))); files.add(new FileInfo(tempFile, je.getName())); } catch (IOException e) { throw new RuntimeException(String.format("Cannot load jar entries from jar %s", je.getName().toLowerCase()), e); } catch (EeClassLoaderException e) { throw new RuntimeException("ERROR on loading inner JAR: " + e.getMessageAll()); } }); }
private void testNumFiles(int numFiles, boolean isFile) throws IOException { File input = isFile ? File.createTempFile("abcd", "txt", TEMP_FOLDER.newFolder()) : TEMP_FOLDER.newFolder(); Set<String> files = new HashSet<>(); if (!isFile) { for (int i = 0; i < numFiles; i++) { files.add(File.createTempFile("abcd", ".txt", input).getName()); } } else { files.add(input.getName()); } File destArchive = new File(TEMP_FOLDER.newFolder(), "myBundle.jar"); BundleJarUtil.createJar(input, destArchive); try (JarFile jarFile = new JarFile(destArchive)) { Assert.assertTrue(jarFile.stream().map(JarEntry::getName).allMatch(files::contains)); } } }