private static Set<String> getZipEntries(String zipFilePath) throws IOException { return new ZipFile(zipFilePath).stream().map(ZipEntry::getName) .filter(x -> !x.endsWith("views.log")).collect(Collectors.toSet()); } }
checksum = zip.stream().mapToLong(ZipEntry::getCrc).reduce((l1, l2) -> l1 ^ l2).getAsLong();
public static Stream<? extends ZipEntry> allFileEntries(ZipFile zipFile) { return zipFile.stream().filter(not(ZipEntry::isDirectory)); }
/** Extracts paths of all files having the given path prefix. */ public static ImmutableList<String> filesUnderPath(ZipFile zipFile, ZipPath pathPrefix) { return zipFile.stream() .map(ZipEntry::getName) .filter(entryName -> ZipPath.create(entryName).startsWith(pathPrefix)) .collect(toImmutableList()); }
private BundleModule toBundleModule(ZipFile moduleZipFile) { BundleModule bundleModule; try { bundleModule = BundleModule.builder() // Assigning a temporary name because the real one will be extracted from the // manifest, but this requires the BundleModule to be built. .setName(BundleModuleName.create("TEMPORARY_MODULE_NAME")) .setBundleConfig(EMPTY_CONFIG_WITH_CURRENT_VERSION) .addEntries( moduleZipFile.stream() .filter(not(ZipEntry::isDirectory)) .map(zipEntry -> ModuleZipEntry.fromModuleZipEntry(zipEntry, moduleZipFile)) .collect(toImmutableList())) .build(); } catch (IOException e) { throw new UncheckedIOException( String.format("Error reading module zip file '%s'.", moduleZipFile.getName()), e); } BundleModuleName actualModuleName = BundleModuleName.create( bundleModule .getAndroidManifest() .getSplitId() .orElse(BundleModuleName.BASE_MODULE_NAME)); return bundleModule.toBuilder().setName(actualModuleName).build(); } }
private static Collection<ScannedDependency> scanZipFile(final File zipFile) throws IOException { final Collection<String> entryNames; try (ZipFile zip = new ZipFile(zipFile)) { entryNames = zip.stream().filter(entry -> { final String entryName = entry.getName(); return entryName.startsWith(META_INF_YANG_STRING_JAR) && !entry.isDirectory() && entryName.endsWith(RFC6020_YANG_FILE_EXTENSION); }).map(ZipEntry::getName).collect(ImmutableList.toImmutableList()); } return entryNames.isEmpty() ? ImmutableList.of() : ImmutableList.of(new Zip(zipFile, entryNames)); }
/** * Traversing jar file and loading all '.class' files by provided classloader. * * @param pathToJar where jar file resides * @param classLoader class loader used for loading classes * @return list of loaded classes */ public static List<Class<?>> loadFromJar(final File pathToJar, final ClassLoader classLoader) throws MojoFailureException { try (ZipFile zipFile = new ZipFile(pathToJar)) { Stream<String> stream = zipFile.stream() .filter(zipEntry -> !zipEntry.isDirectory()) .map(zipEntry -> zipEntry.getName()); return processStream(stream, classLoader, pathToJar); } catch (IOException ioe) { throw new MojoFailureException("Can't read from jar file '" + pathToJar + "'", ioe); } }
/** * Given the path to a jar file, get all the .class files * @param jarPath * @param pkg */ static List<String> getClasses(String jarPath, String pkg) throws IOException { try(ZipFile zf = new ZipFile(jarPath)) { return zf.stream() .filter(e -> !e.isDirectory() && e.getName().endsWith(".class") && !e.getName().contains("$")) .map(e -> { String className = e.getName().replace('/', '.'); String test = className.substring(0, className.length() - ".class".length()); return test; }) .filter(e -> e.contains(pkg)) .collect(Collectors.toList()); } }
/** * Helper function helps read the entries in a zipfile and returns a list of only the javascript * files (i.e files ending in .js). * * <p>Closure supports loading source files as {@code foo.zip!/path/in/zip.js}. */ static List<Path> getJsEntryPathsFromZip(String source) { try (ZipFile zipFile = new ZipFile(source)) { return zipFile .stream() .filter(e -> !e.isDirectory()) .filter(e -> e.getName().endsWith(".js")) .map(e -> source + "!/" + e.getName()) .map(Paths::get) .collect(Collectors.toList()); } catch (IOException e) { throw new RuntimeException("failed to read zip file " + source, e); } }
private boolean isDirectory(final String path) throws IOException { if (productDir.isCompressed()) { if (path.contains(".")) { int sepIndex = path.lastIndexOf('/'); int dotIndex = path.lastIndexOf('.'); return dotIndex < sepIndex; } else { final ZipFile productZip = new ZipFile(baseDir, ZipFile.OPEN_READ); final Optional result = productZip.stream() .filter(ze -> ze.isDirectory()).filter(ze -> ze.getName().equals(path)).findFirst(); return result.isPresent(); } } else { return productDir.getFile(path).isDirectory(); } }
/** * List all Java classes found in the JAR files. * */ private static Set<String> listAllJavaClasses(Set<Path> hookJars, Predicate<String> classNameFilter) throws IOException { Set<String> result = new TreeSet<>(); for (Path hookJar : hookJars) { // For convenient testing, hookJar may be a classes/ directory instead of a JAR file. if (hookJar.toFile().isDirectory()) { try (Stream<Path> dirEntries = Files.walk(hookJar)) { addClassNames(dirEntries.map(hookJar::relativize).map(Path::toString), result, classNameFilter); } } else if (hookJar.toFile().isFile()) { try (ZipFile zipFile = new ZipFile(hookJar.toFile())) { addClassNames(zipFile.stream().map(ZipEntry::getName), result, classNameFilter); } } else { throw new IOException(hookJar + ": Failed to read file or directory."); } } return result; }
protected boolean loadFromCatalog(String basePhysicalIRI, ZipFile z) throws IOException { ZipEntry yaml = z.stream().filter(e -> CATALOG_PATTERN.matcher(e.getName()).matches()) .findFirst().orElse(null); if (yaml == null) { return false; } try { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(z.getInputStream(yaml)); NodeList uris = doc.getElementsByTagName("uri"); for (int i = 0; i < uris.getLength(); i++) { // Catalogs do not have a way to indicate root ontologies; all ontologies will be // considered root. // Duplicate entries are unsupported; entries whose name starts with duplicate: will // cause mismatches Element e = (Element) uris.item(i); IRI physicalIRI = IRI.create(basePhysicalIRI + e.getAttribute("uri")); physicalRoots.add(physicalIRI); String name = e.getAttribute("name"); if (name.startsWith("duplicate:")) { name = name.replace("duplicate:", ""); } logicalToPhysicalIRI.put(IRI.create(name), physicalIRI); } return true; } catch (SAXException | ParserConfigurationException e1) { throw new IOException(e1); } }
protected boolean loadFromCatalog(String basePhysicalIRI, ZipFile z) throws IOException { ZipEntry yaml = z.stream().filter(e -> CATALOG_PATTERN.matcher(e.getName()).matches()) .findFirst().orElse(null); if (yaml == null) { return false; } try { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(z.getInputStream(yaml)); NodeList uris = doc.getElementsByTagName("uri"); for (int i = 0; i < uris.getLength(); i++) { // Catalogs do not have a way to indicate root ontologies; all ontologies will be // considered root. // Duplicate entries are unsupported; entries whose name starts with duplicate: will // cause mismatches Element e = (Element) uris.item(i); IRI physicalIRI = IRI.create(basePhysicalIRI + e.getAttribute("uri")); physicalRoots.add(physicalIRI); String name = e.getAttribute("name"); if (name.startsWith("duplicate:")) { name = name.replace("duplicate:", ""); } logicalToPhysicalIRI.put(IRI.create(name), physicalIRI); } return true; } catch (SAXException | ParserConfigurationException e1) { throw new IOException(e1); } }
protected boolean loadFromCatalog(String basePhysicalIRI, ZipFile z) throws IOException { ZipEntry yaml = z.stream().filter(e -> CATALOG_PATTERN.matcher(e.getName()).matches()) .findFirst().orElse(null); if (yaml == null) { return false; } try { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(z.getInputStream(yaml)); NodeList uris = doc.getElementsByTagName("uri"); for (int i = 0; i < uris.getLength(); i++) { // Catalogs do not have a way to indicate root ontologies; all ontologies will be // considered root. // Duplicate entries are unsupported; entries whose name starts with duplicate: will // cause mismatches Element e = (Element) uris.item(i); IRI physicalIRI = IRI.create(basePhysicalIRI + e.getAttribute("uri")); physicalRoots.add(physicalIRI); String name = e.getAttribute("name"); if (name.startsWith("duplicate:")) { name = name.replace("duplicate:", ""); } logicalToPhysicalIRI.put(IRI.create(name), physicalIRI); } return true; } catch (SAXException | ParserConfigurationException e1) { throw new IOException(e1); } }
/** * @param zipFile zip app * @return app name * @exception FileOperationException I/O error */ public static String getAppName(Path zipFile) { ZipFile zip = null; try { zip = new ZipFile(zipFile.toFile()); ZipEntry firstEntry = zip.stream() .findFirst() .orElseThrow(() -> new FileOperationException("Cannot find app directory in zip artifact '" + zipFile + "'.")); if (firstEntry.isDirectory()) { return Paths.get(firstEntry.getName()).getFileName().toString(); } else { throw new FileOperationException( "Cannot find an app directory inside the zip artifact '" + zipFile + "'."); } } catch (IOException e) { throw new FileOperationException( "An error occurred when opening zip artifact '" + zipFile + "'.", e); } finally { IOUtils.closeQuietly(zip); } }
@Override public void merge(Path source) { if (Files.isDirectory(source)) { merge(walkDirectory(source).stream().map(path -> new PathFileEntry(source.resolve(path), path))); } else { try (ZipFile zipFile = new ZipFile(source.toFile())) { Stream<? extends FileEntry> contentStream = zipFile.stream(). filter(e -> !e.isDirectory()). map(e -> new ZipFileEntry(zipFile, e)). filter(fe -> m_pathFilter.test(fe.path())); merge(contentStream); } catch (IOException e) { throw new UncheckedIOException(e); } } }
@Override public void runImport() { if (!this.file.isRealFile()) { throw new UnsupportedOperationException( "Importing a zip file from a stream is not supported"); } try (ZipFile zip = new ZipFile(this.file.toPath().toFile(), StandardCharsets.UTF_8)) { zip.stream() // .filter(entry -> !entry.isDirectory()) // .map(entry -> createInput(zip, entry)) // .forEach(this.delegateImporter::importFile); } catch (final IOException e) { throw new ImporterException("Error reading \"" + this.file + "\"", e); } }
@Override public void runImport() { if (!this.file.isRealFile()) { throw new UnsupportedOperationException( "Importing a zip file from a stream is not supported"); } try (ZipFile zip = new ZipFile(this.file.toPath().toFile(), StandardCharsets.UTF_8)) { zip.stream() // .filter(entry -> !entry.isDirectory()) // .map(entry -> createInput(zip, entry)) // .forEach(this.delegateImporter::importFile); } catch (final IOException e) { throw new ImporterException("Error reading \"" + this.file + "\"", e); } }
List<ReportItem> reportItems = new ArrayList<>(); ZipFile zipFile = new ZipFile(jarFile); zipFile.stream().filter(ArchiveAnalyzerCallable::isClassFile) .forEach(zipEntry -> { try {
zipFile.stream().forEach(f -> { if (f.getName().endsWith(".jar")) { String targetName = jarId + ".jar";