List of usage examples for java.util.jar JarEntry getName
public String getName()
From source file:com.technophobia.substeps.report.DefaultExecutionReportBuilder.java
public void copyJarResourcesRecursively(final File destination, final JarURLConnection jarConnection) throws IOException { final JarFile jarFile = jarConnection.getJarFile(); for (final JarEntry entry : Collections.list(jarFile.entries())) { if (entry.getName().startsWith(jarConnection.getEntryName())) { final String fileName = StringUtils.removeStart(entry.getName(), jarConnection.getEntryName()); if (!entry.isDirectory()) { InputStream entryInputStream = null; try { entryInputStream = jarFile.getInputStream(entry); FileUtils.copyInputStreamToFile(entryInputStream, new File(destination, fileName)); } finally { IOUtils.closeQuietly(entryInputStream); }//from w ww . j a v a2s . co m } else { new File(destination, fileName).mkdirs(); } } } }
From source file:org.paxle.gui.impl.StyleManager.java
public void setStyle(String name) { if (name.equals("default")) { ((ServletManager) this.servletManager).unregisterAllResources(); ((ServletManager) this.servletManager).addResources("/css", "/resources/templates/layout/css"); ((ServletManager) this.servletManager).addResources("/js", "/resources/js"); ((ServletManager) this.servletManager).addResources("/images", "/resources/images"); return;/*ww w. j a va 2 s . c o m*/ } try { File styleFile = new File(this.dataPath, name); HttpContext httpContextStyle = new HttpContextStyle(styleFile); JarFile styleJarFile = new JarFile(styleFile); Enumeration<?> jarEntryEnum = styleJarFile.entries(); while (jarEntryEnum.hasMoreElements()) { JarEntry entry = (JarEntry) jarEntryEnum.nextElement(); if (entry.isDirectory()) { String alias = "/" + entry.getName().substring(0, entry.getName().length() - 1); ((ServletManager) this.servletManager).removeResource(alias); ((ServletManager) this.servletManager).addResources(alias, alias, httpContextStyle); } } } catch (IOException e) { logger.error("io: " + e); e.printStackTrace(); } return; }
From source file:ClassSearchUtils.java
/** * Search archive files for required resource. * @param archive Jar or zip to be searched for classes or other resources. *///from w w w .j a v a2 s .c o m private void lookInArchive(File archive) { log.fine("Looking in archive [" + archive.getName() + "] for extension [" + this.extension + "]."); JarFile jarFile = null; try { jarFile = new JarFile(archive); } catch (IOException e) { log.warning("Non fatal error. Unable to read jar item."); return; } Enumeration entries = jarFile.entries(); JarEntry entry; String entryName; while (entries.hasMoreElements()) { entry = (JarEntry) entries.nextElement(); entryName = entry.getName(); if (entryName.toLowerCase().endsWith(this.extension)) { try { if (this.extension.equalsIgnoreCase(".class")) { // convert name into java classloader notation entryName = entryName.substring(0, entryName.length() - 6); entryName = entryName.replace('/', '.'); // filter ignored resources if (!entryName.startsWith(this.prefix)) { continue; } log.fine("Found class: [" + entryName + "]. "); this.list.add(Class.forName(entryName)); } else { this.list.add(this.classloader.getResource(entryName)); log.fine("Found appropriate resource with name [" + entryName + "]. Resource instance:" + this.classloader.getResource(entryName)); } } catch (Throwable e) { // ignore log.warning("Unable to load resource [" + entryName + "] form file [" + archive.getAbsolutePath() + "]."); } } } }
From source file:org.simplericity.jettyconsole.creator.DefaultCreator.java
private void unpackMainClassAndFilter(URL coredep) { try (JarInputStream in = new JarInputStream(coredep.openStream())) { while (true) { JarEntry entry = in.getNextJarEntry(); if (entry == null) { break; }/*from w w w . j a va 2s.co m*/ if (entry.getName().startsWith(MAIN_CLASS) && entry.getName().endsWith(".class")) { File file = new File(workingDirectory, entry.getName()); file.getParentFile().mkdirs(); FileOutputStream out = new FileOutputStream(file); IOUtils.copy(in, out); out.close(); } } } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.stacksync.desktop.Environment.java
public void copyResourcesFromJar(JarURLConnection jarConnection, File destDir) { try {/*from www . ja v a2s . c om*/ JarFile jarFile = jarConnection.getJarFile(); /** * Iterate all entries in the jar file. */ for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) { JarEntry jarEntry = e.nextElement(); String jarEntryName = jarEntry.getName(); String jarConnectionEntryName = jarConnection.getEntryName(); /** * Extract files only if they match the path. */ if (jarEntryName.startsWith(jarConnectionEntryName)) { String filename = jarEntryName.startsWith(jarConnectionEntryName) ? jarEntryName.substring(jarConnectionEntryName.length()) : jarEntryName; File currentFile = new File(destDir, filename); if (jarEntry.isDirectory()) { currentFile.mkdirs(); } else { InputStream is = jarFile.getInputStream(jarEntry); OutputStream out = FileUtils.openOutputStream(currentFile); IOUtils.copy(is, out); is.close(); out.close(); } } } } catch (IOException e) { // TODO add logger e.printStackTrace(); } }
From source file:org.kitodo.serviceloader.KitodoServiceLoader.java
/** * Checks, whether a passed jarFile has frontend files or not. Returns true, * when the jar contains a folder with the name "resources" * * @param jarFile//from w w w . jav a 2 s. c o m * jarFile that will be checked for frontend files * * @return boolean */ private boolean hasFrontendFiles(JarFile jarFile) { Enumeration enums = jarFile.entries(); while (enums.hasMoreElements()) { JarEntry jarEntry = (JarEntry) enums.nextElement(); if (jarEntry.getName().contains(RESOURCES_FOLDER) && jarEntry.isDirectory()) { return true; } } return false; }
From source file:com.netease.hearttouch.hthotfix.patch.PatchRefGeneratorTransformExecutor.java
public void transformJar(File inputFile, File outputFile) throws IOException { final JarFile jar = new JarFile(inputFile); final OutputJar outputJar = new OutputJar(outputFile); for (final Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) { final JarEntry entry = entries.nextElement(); if (entry.isDirectory()) { continue; }/* ww w . j a v a 2 s . com*/ final InputStream is = jar.getInputStream(entry); try { byte[] bytecode = IOUtils.toByteArray(is); if (entry.getName().endsWith(".class")) { if (extension.getGeneratePatch() && extension.getScanRef()) { ClassReader cr = new ClassReader(bytecode); String className = cr.getClassName(); if (extension.getGeneratePatch() && extension.getScanRef()) { if (!refScanInstrument.isPatchClass(className.replace("/", "."))) { boolean bPatchRef = refScanInstrument.hasReference(bytecode, project); if (bPatchRef) { patchJarHelper.writeClassToDirectory(className, bytecode); } } } } outputJar.writeEntry(entry, bytecode); } else { outputJar.writeEntry(entry, bytecode); } } finally { IOUtils.closeQuietly(is); } } outputJar.close(); }
From source file:org.jahia.data.templates.ModulesPackage.java
private ModulesPackage(JarFile jarFile) throws IOException { modules = new LinkedHashMap<String, PackagedModule>(); Attributes manifestAttributes = jarFile.getManifest().getMainAttributes(); version = new Version(manifestAttributes.getValue(Constants.ATTR_NAME_JAHIA_PACKAGE_VERSION)); name = manifestAttributes.getValue(Constants.ATTR_NAME_JAHIA_PACKAGE_NAME); description = manifestAttributes.getValue(Constants.ATTR_NAME_JAHIA_PACKAGE_DESCRIPTION); // read jars/* ww w . jav a 2 s . c om*/ Enumeration<JarEntry> jars = jarFile.entries(); while (jars.hasMoreElements()) { JarEntry jar = jars.nextElement(); JarFile moduleJarFile = null; OutputStream output = null; if (StringUtils.endsWith(jar.getName(), ".jar")) { try { InputStream input = jarFile.getInputStream(jar); File moduleFile = File.createTempFile(jar.getName(), ""); output = new FileOutputStream(moduleFile); int read = 0; byte[] bytes = new byte[1024]; while ((read = input.read(bytes)) != -1) { output.write(bytes, 0, read); } moduleJarFile = new JarFile(moduleFile); Attributes moduleManifestAttributes = moduleJarFile.getManifest().getMainAttributes(); String bundleName = moduleManifestAttributes.getValue(Constants.ATTR_NAME_BUNDLE_SYMBOLIC_NAME); String jahiaGroupId = moduleManifestAttributes.getValue(Constants.ATTR_NAME_GROUP_ID); if (bundleName == null || jahiaGroupId == null) { throw new IOException( "Jar file " + jar.getName() + " in package does not seems to be a DX bundle."); } modules.put(bundleName, new PackagedModule(bundleName, moduleManifestAttributes, moduleFile)); } finally { IOUtils.closeQuietly(output); if (moduleJarFile != null) { moduleJarFile.close(); } } } } // we finally sort modules based on dependencies try { sortByDependencies(modules); } catch (CycleDetectedException e) { throw new JahiaRuntimeException("A cyclic dependency detected in the modules of the supplied package", e); } }
From source file:com.netease.hearttouch.hthotfix.patch.PatchGeneratorTransformExecutor.java
public void transformJar(File inputFile, File outputFile) throws IOException { final JarFile jar = new JarFile(inputFile); final OutputJar outputJar = new OutputJar(outputFile); for (final Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) { final JarEntry entry = entries.nextElement(); if (entry.isDirectory()) { continue; }/*from w ww. ja v a 2 s .c o m*/ final InputStream is = jar.getInputStream(entry); try { byte[] bytecode = IOUtils.toByteArray(is); if (entry.getName().endsWith(".class")) { if (extension.getGeneratePatch()) { ClassReader cr = new ClassReader(bytecode); String className = cr.getClassName(); if ((hashFileParser != null) && (hashFileParser.isChanged(className, bytecode))) { patchJarHelper.writeClassToDirectory(className, hackInjector.inject(className, bytecode)); refScanInstrument.addPatchClassName(className, hackInjector.getLastSuperName()); } } outputJar.writeEntry(entry, bytecode); } else { outputJar.writeEntry(entry, bytecode); } } finally { IOUtils.closeQuietly(is); } } outputJar.close(); }
From source file:org.openeos.tools.maven.plugins.GenerateEntitiesMojo.java
private List<File> findLiquibaseFiles(Artifact artifact) throws IOException { if (artifact == null) { return Collections.emptyList(); }// w w w .jav a2 s.co m List<File> result = new ArrayList<File>(); if (artifact.getType().equals("jar")) { File file = artifact.getFile(); FileSystem fs = FileSystems.newFileSystem(Paths.get(file.getAbsolutePath()), this.getClass().getClassLoader()); PathMatcher matcher = fs.getPathMatcher("glob:" + searchpath); JarFile jarFile = new JarFile(file); Enumeration<JarEntry> entries = jarFile.entries(); TreeSet<JarEntry> setEntries = new TreeSet<JarEntry>(new Comparator<JarEntry>() { @Override public int compare(JarEntry o1, JarEntry o2) { return o1.getName().compareTo(o2.getName()); } }); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (matcher.matches(fs.getPath(entry.getName()))) { setEntries.add(entry); } } for (JarEntry entry : setEntries) { File resultFile = File.createTempFile("generate-entities-maven", ".xml"); FileOutputStream out = new FileOutputStream(resultFile); InputStream in = jarFile.getInputStream(entry); IOUtils.copy(in, out); in.close(); out.close(); result.add(resultFile); } jarFile.close(); } return result; }