List of usage examples for java.util.jar JarFile getName
public String getName()
From source file:org.jsweet.transpiler.candies.CandiesProcessor.java
private void extractCandy( // CandyDescriptor descriptor, // JarFile jarFile, // File javaOutputDirectory, // File tsDefOutputDirectory, // File jsOutputDirectory, // Predicate<String> isTsDefToBeExtracted) { logger.info("extract candy: " + jarFile.getName() + " javaOutputDirectory=" + javaOutputDirectory + " tsDefOutputDirectory=" + tsDefOutputDirectory + " jsOutputDir=" + jsOutputDirectory); jarFile.stream().filter(entry -> entry.getName().endsWith(".d.ts") && (entry.getName().startsWith("src/") || entry.getName().startsWith("META-INF/resources/"))) // .forEach(entry -> {//from ww w.ja v a2s . c o m File out; if (entry.getName().endsWith(".java")) { // RP: this looks like dead code... out = new File(javaOutputDirectory + "/" + entry.getName().substring(4)); } else if (entry.getName().endsWith(".d.ts")) { if (isTsDefToBeExtracted != null && !isTsDefToBeExtracted.test(entry.getName())) { return; } out = new File(tsDefOutputDirectory + "/" + entry.getName()); } else { out = null; } extractEntry(jarFile, entry, out); }); for (String jsFilePath : descriptor.jsFilesPaths) { JarEntry entry = jarFile.getJarEntry(jsFilePath); String relativeJsPath = jsFilePath.substring(descriptor.jsDirPath.length()); File out = new File(jsOutputDirectory, relativeJsPath); extractEntry(jarFile, entry, out); } }
From source file:org.jasig.portal.plugin.deployer.AbstractExtractingEarDeployer.java
/** * Reads the specified {@link JarEntry} from the {@link JarFile} and writes its contents * to the specified {@link File}./* ww w . j a v a2 s.c om*/ * * @param earEntry The JarEntry for the file to read from the archive. * @param earFile The JarFile to get the {@link InputStream} for the file from. * @param destinationFile The File to write to, all parent directories should exist and no file should already exist at this location. * @throws IOException If the copying of data from the JarEntry to the File fails. */ protected void copyAndClose(JarEntry earEntry, JarFile earFile, File destinationFile) throws MojoFailureException { if (this.getLogger().isInfoEnabled()) { this.getLogger().info("Copying EAR entry '" + earFile.getName() + "!" + earEntry.getName() + "' to '" + destinationFile + "'"); } InputStream jarEntryStream = null; try { jarEntryStream = earFile.getInputStream(earEntry); final OutputStream jarOutStream = new FileOutputStream(destinationFile); try { IOUtils.copy(jarEntryStream, jarOutStream); } finally { IOUtils.closeQuietly(jarOutStream); } } catch (IOException e) { throw new MojoFailureException("Failed to copy EAR entry '" + earEntry.getName() + "' out of '" + earFile.getName() + "' to '" + destinationFile + "'", e); } finally { IOUtils.closeQuietly(jarEntryStream); } }
From source file:UnpackedJarFile.java
public static File toFile(JarFile jarFile, String path) throws IOException { if (jarFile instanceof UnpackedJarFile) { File baseDir = ((UnpackedJarFile) jarFile).getBaseDir(); File file = new File(baseDir, path); if (!file.isFile()) { throw new IOException("No such file: " + file.getAbsolutePath()); }//from w ww .j a v a 2s .c om return file; } else { String urlString = "jar:" + new File(jarFile.getName()).toURL() + "!/" + path; return toTempFile(new URL(urlString)); } }
From source file:org.ebayopensource.turmeric.eclipse.utils.classloader.SOAPluginClassLoader.java
/** * {@inheritDoc}/*from w w w. j av a2 s . co m*/ */ @Override public URL findResource(String resourceName) { //logger.info("resource name in findresource is " + resourceName); try { URL retUrl = null; for (Bundle pluginBundle : pluginBundles) { retUrl = pluginBundle.getResource(resourceName); if (retUrl != null) { if (logger.isLoggable(Level.FINE)) { logger.fine("found resource using bundle " + resourceName); } return retUrl; } } } catch (Exception exception) { } for (URL url : m_jarURLs) { try { File file = FileUtils.toFile(url); JarFile jarFile; jarFile = new JarFile(file); JarEntry jarEntry = jarFile.getJarEntry(resourceName); if (jarEntry != null) { SOAToolFileUrlHandler handler = new SOAToolFileUrlHandler(jarFile, jarEntry); URL retUrl = new URL("jar", "", -1, new File(jarFile.getName()).toURI().toURL() + "!/" + jarEntry.getName(), handler); handler.setExpectedUrl(retUrl); return retUrl; } } catch (IOException e) { e.printStackTrace(); // KEEPME } } return super.findResource(resourceName); }
From source file:net.minecraftforge.fml.relauncher.CoreModManager.java
private static Map<String, File> extractContainedDepJars(JarFile jar, File baseModsDir, File versionedModsDir) throws IOException { Map<String, File> result = Maps.newHashMap(); if (!jar.getManifest().getMainAttributes().containsKey(MODCONTAINSDEPS)) return result; String deps = jar.getManifest().getMainAttributes().getValue(MODCONTAINSDEPS); String[] depList = deps.split(" "); for (String dep : depList) { String depEndName = new File(dep).getName(); // extract last part of name if (skipContainedDeps.contains(dep) || skipContainedDeps.contains(depEndName)) { FMLRelaunchLog.log(Level.ERROR, "Skipping dep at request: %s", dep); continue; }/*from w ww. j a v a 2 s .c om*/ final JarEntry jarEntry = jar.getJarEntry(dep); if (jarEntry == null) { FMLRelaunchLog.log(Level.ERROR, "Found invalid ContainsDeps declaration %s in %s", dep, jar.getName()); continue; } File target = new File(versionedModsDir, depEndName); File modTarget = new File(baseModsDir, depEndName); if (target.exists()) { FMLRelaunchLog.log(Level.DEBUG, "Found existing ContainsDep extracted to %s, skipping extraction", target.getCanonicalPath()); result.put(dep, target); continue; } else if (modTarget.exists()) { FMLRelaunchLog.log(Level.DEBUG, "Found ContainsDep in main mods directory at %s, skipping extraction", modTarget.getCanonicalPath()); result.put(dep, modTarget); continue; } FMLRelaunchLog.log(Level.DEBUG, "Extracting ContainedDep %s from %s to %s", dep, jar.getName(), target.getCanonicalPath()); try { Files.createParentDirs(target); FileOutputStream targetOutputStream = null; InputStream jarInputStream = null; try { targetOutputStream = new FileOutputStream(target); jarInputStream = jar.getInputStream(jarEntry); ByteStreams.copy(jarInputStream, targetOutputStream); } finally { IOUtils.closeQuietly(targetOutputStream); IOUtils.closeQuietly(jarInputStream); } FMLRelaunchLog.log(Level.DEBUG, "Extracted ContainedDep %s from %s to %s", dep, jar.getName(), target.getCanonicalPath()); result.put(dep, target); } catch (IOException e) { FMLRelaunchLog.log(Level.ERROR, e, "An error occurred extracting dependency"); } } return result; }
From source file:edu.stanford.muse.email.JarDocCache.java
/** returns a list of files in the jar */ private synchronized Set<String> readJarFileEntries(JarFile jf) throws IOException { Set<String> result = new LinkedHashSet<String>(); Enumeration<JarEntry> entries = jf.entries(); while (entries.hasMoreElements()) result.add(entries.nextElement().getName()); log.info("Jarfile " + jf.getName() + " has " + result.size() + " entries"); return result; }
From source file:org.jasig.portal.plugin.deployer.AbstractExtractingEarDeployer.java
/** * Reads the specified {@link JarEntry} from the {@link JarFile} assuming that the * entry represents another a JAR file. The files in the {@link JarEntry} will be * extracted using the contextDir as the base directory. * /*from ww w . j a v a2 s . c om*/ * @param earFile The JarEntry for the JAR to read from the archive. * @param earEntry The JarFile to get the {@link InputStream} for the file from. * @param contextDir The directory to extract the JAR to. * @throws IOException If the extracting of data from the JarEntry fails. */ protected void extractWar(JarFile earFile, final JarEntry earEntry, final File contextDir) throws MojoFailureException { if (this.getLogger().isInfoEnabled()) { this.getLogger().info("Extracting EAR entry '" + earFile.getName() + "!" + earEntry.getName() + "' to '" + contextDir + "'"); } if (!contextDir.exists()) { if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("Creating context directory entry '" + contextDir + "'"); } try { FileUtils.forceMkdir(contextDir); } catch (IOException e) { throw new MojoFailureException("Failed to create '" + contextDir + "' to extract '" + earEntry.getName() + "' out of '" + earFile.getName() + "' into", e); } } JarInputStream warInputStream = null; try { warInputStream = new JarInputStream(earFile.getInputStream(earEntry)); // Write out the MANIFEST.MF file to the target directory Manifest manifest = warInputStream.getManifest(); if (manifest != null) { FileOutputStream manifestFileOutputStream = null; try { final File manifestFile = new File(contextDir, MANIFEST_PATH); manifestFile.getParentFile().mkdirs(); manifestFileOutputStream = new FileOutputStream(manifestFile); manifest.write(manifestFileOutputStream); } catch (Exception e) { this.getLogger().error("Failed to copy the MANIFEST.MF file for ear entry '" + earEntry.getName() + "' out of '" + earFile.getName() + "'", e); throw new MojoFailureException("Failed to copy the MANIFEST.MF file for ear entry '" + earEntry.getName() + "' out of '" + earFile.getName() + "'", e); } finally { try { if (manifestFileOutputStream != null) { manifestFileOutputStream.close(); } } catch (Exception e) { this.getLogger().warn("Error closing the OutputStream for MANIFEST.MF in warEntry: " + earEntry.getName()); } } } JarEntry warEntry; while ((warEntry = warInputStream.getNextJarEntry()) != null) { final File warEntryFile = new File(contextDir, warEntry.getName()); if (warEntry.isDirectory()) { if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("Creating WAR directory entry '" + earEntry.getName() + "!" + warEntry.getName() + "' as '" + warEntryFile + "'"); } FileUtils.forceMkdir(warEntryFile); } else { if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("Extracting WAR entry '" + earEntry.getName() + "!" + warEntry.getName() + "' to '" + warEntryFile + "'"); } FileUtils.forceMkdir(warEntryFile.getParentFile()); final FileOutputStream jarEntryFileOutputStream = new FileOutputStream(warEntryFile); try { IOUtils.copy(warInputStream, jarEntryFileOutputStream); } finally { IOUtils.closeQuietly(jarEntryFileOutputStream); } } } } catch (IOException e) { throw new MojoFailureException("Failed to extract EAR entry '" + earEntry.getName() + "' out of '" + earFile.getName() + "' to '" + contextDir + "'", e); } finally { IOUtils.closeQuietly(warInputStream); } }
From source file:org.eclipse.emf.mwe.utils.StandaloneSetup.java
protected void registerBundle(File file) { JarFile jarFile = null; try {//w ww .j a va2s .c om jarFile = new JarFile(file); log.debug("Trying to determine project name from Manifest for " + jarFile.getName()); String name = getBundleNameFromManifest(jarFile); if (name == null) { log.debug("Trying to determine project name from file name for " + jarFile.getName()); name = getBundleNameFromJarName(jarFile.getName()); } if (name != null) { final int indexOf = name.indexOf(';'); if (indexOf > 0) name = name.substring(0, indexOf); String path = "archive:" + file.getCanonicalFile().toURI() + "!/"; URI uri = URI.createURI(path); registerMapping(name, uri); } else { log.debug("Could not determine project name for " + jarFile.getName() + ". No project mapping will be added."); } } catch (ZipException e) { log.warn("Could not open Jar file " + file.getAbsolutePath() + "."); } catch (Exception e) { handleException(file, e); } finally { try { if (jarFile != null) jarFile.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } }
From source file:com.amalto.core.query.SystemStorageTest.java
private static Collection<String> getConfigFiles() throws Exception { URL data = InitDBUtil.class.getResource("data"); //$NON-NLS-1$ List<String> result = new ArrayList<String>(); if ("jar".equals(data.getProtocol())) { //$NON-NLS-1$ JarURLConnection connection = (JarURLConnection) data.openConnection(); JarEntry entry = connection.getJarEntry(); JarFile file = connection.getJarFile(); Enumeration<JarEntry> entries = file.entries(); while (entries.hasMoreElements()) { JarEntry e = entries.nextElement(); if (e.getName().startsWith(entry.getName()) && !e.isDirectory()) { result.add(IOUtils.toString(file.getInputStream(e))); }//from w w w .j a v a 2 s.c om } } else { Collection<File> files = FileUtils.listFiles(new File(data.toURI()), new IOFileFilter() { @Override public boolean accept(File file) { return true; } @Override public boolean accept(File file, String s) { return true; } }, new IOFileFilter() { @Override public boolean accept(File file) { return !".svn".equals(file.getName()); //$NON-NLS-1$ } @Override public boolean accept(File file, String s) { return !".svn".equals(file.getName()); //$NON-NLS-1$ } }); for (File f : files) { result.add(IOUtils.toString(new FileInputStream(f))); } } return result; }
From source file:cn.homecredit.web.listener.MyOsgiHost.java
protected String getVersion(URL url) { if ("jar".equals(url.getProtocol())) { try {// ww w . j a va 2s . c om JarFile jarFile = new JarFile(new File(URLUtil.normalizeToFileProtocol(url).toURI())); Manifest manifest = jarFile.getManifest(); if (manifest != null) { String version = manifest.getMainAttributes().getValue("Bundle-Version"); if (StringUtils.isNotBlank(version)) { return getVersionFromString(version); } } else { //try to get the version from the file name return getVersionFromString(jarFile.getName()); } } catch (Exception e) { if (LOG.isErrorEnabled()) { LOG.error("Unable to extract version from [#0], defaulting to '1.0.0'", url.toExternalForm()); } } } return "1.0.0"; }