List of usage examples for java.net JarURLConnection getJarEntry
public JarEntry getJarEntry() throws IOException
From source file:Main.java
public static void main(String[] argv) throws Exception { URL url = new URL("jar:file:/c://my.jar!/"); JarURLConnection conn = (JarURLConnection) url.openConnection(); JarEntry jarEntry = conn.getJarEntry(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { URL url = new URL("jar:file:/c://my.jar!/"); JarURLConnection conn = (JarURLConnection) url.openConnection(); JarEntry jarEntry = conn.getJarEntry(); System.out.println(jarEntry.getName()); }
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))); }// w w w . jav 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:de.uni.bremen.monty.moco.util.MontyJar.java
public MontyJar(URL url) throws IOException { JarURLConnection urlConnection = (JarURLConnection) url.openConnection(); jarFile = urlConnection.getJarFile(); jarEntry = urlConnection.getJarEntry(); }
From source file:org.apache.cxf.management.web.browser.bootstrapping.BootstrapStorage.java
@GET @Path("{resource:.*}") public Response getResource(@Context final MessageContext mc, @PathParam("resource") final String resource) { if (isLastModifiedRequest(mc)) { return Response.notModified().build(); }// www. j av a 2 s.c om try { URL url; URL jar = getClass().getProtectionDomain().getCodeSource().getLocation(); url = new URL(String.format("jar:%s!/static-content/logbrowser/%s", jar, resource)); JarURLConnection connection = (JarURLConnection) url.openConnection(); if (connection.getContentLength() == -1 || connection.getJarEntry() == null) { return Response.status(Status.NOT_FOUND).build(); } else if (connection.getJarEntry().isDirectory()) { return Response.status(Status.FORBIDDEN).build(); } else { // correct MediaType mime = getMimeType(mc, resource); StaticFile staticFile = new StaticFile(url, acceptsGzip(mc), mime); Response.ResponseBuilder builder = Response.ok(staticFile); builder.variant(new Variant(mime, (Locale) null, staticFile.isGzipEnabled() ? "gzip" : null)); return builder.build(); } } catch (MalformedURLException e) { return Response.status(Status.BAD_REQUEST).build(); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Error occur while retrieve static file", e); return Response.serverError().build(); } }
From source file:org.apache.myfaces.config.impl.FacesConfigEntityResolver.java
public InputSource resolveEntity(String publicId, String systemId) throws IOException { InputStream stream;/*from w w w .ja v a 2s. c o m*/ if (systemId.equals(FACES_CONFIG_1_0_DTD_SYSTEM_ID)) { stream = ClassUtils.getResourceAsStream(FACES_CONFIG_1_0_DTD_RESOURCE); } else if (systemId.equals(FACES_CONFIG_1_1_DTD_SYSTEM_ID)) { stream = ClassUtils.getResourceAsStream(FACES_CONFIG_1_1_DTD_RESOURCE); } else if (systemId.startsWith("jar:")) { URL url = new URL(systemId); JarURLConnection conn = (JarURLConnection) url.openConnection(); JarEntry jarEntry = conn.getJarEntry(); if (jarEntry == null) { log.fatal("JAR entry '" + systemId + "' not found."); } //_jarFile.getInputStream(jarEntry); stream = conn.getJarFile().getInputStream(jarEntry); } else { if (_externalContext == null) { stream = ClassUtils.getResourceAsStream(systemId); } else { if (systemId.startsWith("file:")) { systemId = systemId.substring(7); // remove file:// } stream = _externalContext.getResourceAsStream(systemId); } } if (stream == null) { return null; } InputSource is = new InputSource(stream); is.setPublicId(publicId); is.setSystemId(systemId); is.setEncoding("ISO-8859-1"); return is; }
From source file:org.b3log.latke.ioc.ClassPathResolver.java
/** * scan the jar to get the URLS of the Classes. * * @param rootDirResource which is "Jar" * @param subPattern subPattern// w ww . j a v a2 s.c om * @return the URLs of all the matched classes */ private static Collection<? extends URL> doFindPathMatchingJarResources(final URL rootDirResource, final String subPattern) { final Set<URL> result = new LinkedHashSet<URL>(); JarFile jarFile = null; String jarFileUrl; String rootEntryPath = null; URLConnection con; boolean newJarFile = false; try { con = rootDirResource.openConnection(); if (con instanceof JarURLConnection) { final JarURLConnection jarCon = (JarURLConnection) con; jarCon.setUseCaches(false); jarFile = jarCon.getJarFile(); jarFileUrl = jarCon.getJarFileURL().toExternalForm(); final JarEntry jarEntry = jarCon.getJarEntry(); rootEntryPath = jarEntry != null ? jarEntry.getName() : ""; } else { // No JarURLConnection -> need to resort to URL file parsing. // We'll assume URLs of the format "jar:path!/entry", with the // protocol // being arbitrary as long as following the entry format. // We'll also handle paths with and without leading "file:" // prefix. final String urlFile = rootDirResource.getFile(); final int separatorIndex = urlFile.indexOf(JAR_URL_SEPARATOR); if (separatorIndex != -1) { jarFileUrl = urlFile.substring(0, separatorIndex); rootEntryPath = urlFile.substring(separatorIndex + JAR_URL_SEPARATOR.length()); jarFile = getJarFile(jarFileUrl); } else { jarFile = new JarFile(urlFile); jarFileUrl = urlFile; rootEntryPath = ""; } newJarFile = true; } } catch (final IOException e) { LOGGER.log(Level.ERROR, "reslove jar File error", e); return result; } try { if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) { // Root entry path must end with slash to allow for proper // matching. // The Sun JRE does not return a slash here, but BEA JRockit // does. rootEntryPath = rootEntryPath + "/"; } for (final Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) { final JarEntry entry = (JarEntry) entries.nextElement(); final String entryPath = entry.getName(); String relativePath = null; if (entryPath.startsWith(rootEntryPath)) { relativePath = entryPath.substring(rootEntryPath.length()); if (AntPathMatcher.match(subPattern, relativePath)) { if (relativePath.startsWith("/")) { relativePath = relativePath.substring(1); } result.add(new URL(rootDirResource, relativePath)); } } } return result; } catch (final IOException e) { LOGGER.log(Level.ERROR, "parse the JarFile error", e); } finally { // Close jar file, but only if freshly obtained - // not from JarURLConnection, which might cache the file reference. if (newJarFile) { try { jarFile.close(); } catch (final IOException e) { LOGGER.log(Level.WARN, " occur error when closing jarFile", e); } } } return result; }
From source file:org.codehaus.groovy.grails.io.support.PathMatchingResourcePatternResolver.java
/** * Find all resources in jar files that match the given location pattern * via the Ant-style PathMatcher./* w ww . j a v a 2s . c o m*/ * @param rootDirResource the root directory as Resource * @param subPattern the sub pattern to match (below the root directory) * @return the Set of matching Resource instances * @throws IOException in case of I/O errors * @see java.net.JarURLConnection */ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource, String subPattern) throws IOException { URLConnection con = rootDirResource.getURL().openConnection(); JarFile jarFile; String jarFileUrl; String rootEntryPath; boolean newJarFile = false; if (con instanceof JarURLConnection) { // Should usually be the case for traditional JAR files. JarURLConnection jarCon = (JarURLConnection) con; GrailsResourceUtils.useCachesIfNecessary(jarCon); jarFile = jarCon.getJarFile(); jarFileUrl = jarCon.getJarFileURL().toExternalForm(); JarEntry jarEntry = jarCon.getJarEntry(); rootEntryPath = (jarEntry != null ? jarEntry.getName() : ""); } else { // No JarURLConnection -> need to resort to URL file parsing. // We'll assume URLs of the format "jar:path!/entry", with the protocol // being arbitrary as long as following the entry format. // We'll also handle paths with and without leading "file:" prefix. String urlFile = rootDirResource.getURL().getFile(); int separatorIndex = urlFile.indexOf(GrailsResourceUtils.JAR_URL_SEPARATOR); if (separatorIndex != -1) { jarFileUrl = urlFile.substring(0, separatorIndex); rootEntryPath = urlFile.substring(separatorIndex + GrailsResourceUtils.JAR_URL_SEPARATOR.length()); jarFile = getJarFile(jarFileUrl); } else { jarFile = new JarFile(urlFile); jarFileUrl = urlFile; rootEntryPath = ""; } newJarFile = true; } try { if (logger.isDebugEnabled()) { logger.debug("Looking for matching resources in jar file [" + jarFileUrl + "]"); } if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) { // Root entry path must end with slash to allow for proper matching. // The Sun JRE does not return a slash here, but BEA JRockit does. rootEntryPath = rootEntryPath + "/"; } Set<Resource> result = new LinkedHashSet<Resource>(8); for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) { JarEntry entry = entries.nextElement(); String entryPath = entry.getName(); if (entryPath.startsWith(rootEntryPath)) { String relativePath = entryPath.substring(rootEntryPath.length()); if (getPathMatcher().match(subPattern, relativePath)) { result.add(rootDirResource.createRelative(relativePath)); } } } return result; } finally { // Close jar file, but only if freshly obtained - // not from JarURLConnection, which might cache the file reference. if (newJarFile) { jarFile.close(); } } }
From source file:org.grails.io.support.PathMatchingResourcePatternResolver.java
/** * Find all resources in jar files that match the given location pattern * via the Ant-style PathMatcher./*from w ww. j a v a 2 s .c o m*/ * @param rootDirResource the root directory as Resource * @param subPattern the sub pattern to match (below the root directory) * @return the Set of matching Resource instances * @throws IOException in case of I/O errors * @see java.net.JarURLConnection */ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource, String subPattern) throws IOException { URLConnection con = rootDirResource.getURL().openConnection(); JarFile jarFile; String jarFileUrl; String rootEntryPath; boolean newJarFile = false; if (con instanceof JarURLConnection) { // Should usually be the case for traditional JAR files. JarURLConnection jarCon = (JarURLConnection) con; GrailsResourceUtils.useCachesIfNecessary(jarCon); jarFile = jarCon.getJarFile(); jarFileUrl = jarCon.getJarFileURL().toExternalForm(); JarEntry jarEntry = jarCon.getJarEntry(); rootEntryPath = (jarEntry != null ? jarEntry.getName() : ""); } else { // No JarURLConnection -> need to resort to URL file parsing. // We'll assume URLs of the format "jar:path!/entry", with the protocol // being arbitrary as long as following the entry format. // We'll also handle paths with and without leading "file:" prefix. String urlFile = rootDirResource.getURL().getFile(); int separatorIndex = urlFile.indexOf(GrailsResourceUtils.JAR_URL_SEPARATOR); if (separatorIndex != -1) { jarFileUrl = urlFile.substring(0, separatorIndex); rootEntryPath = urlFile.substring(separatorIndex + GrailsResourceUtils.JAR_URL_SEPARATOR.length()); jarFile = getJarFile(jarFileUrl); } else { jarFile = new JarFile(urlFile); jarFileUrl = urlFile; rootEntryPath = ""; } newJarFile = true; } try { if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) { // Root entry path must end with slash to allow for proper matching. // The Sun JRE does not return a slash here, but BEA JRockit does. rootEntryPath = rootEntryPath + "/"; } Set<Resource> result = new LinkedHashSet<Resource>(8); for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) { JarEntry entry = entries.nextElement(); String entryPath = entry.getName(); if (entryPath.startsWith(rootEntryPath)) { String relativePath = entryPath.substring(rootEntryPath.length()); if (getPathMatcher().match(subPattern, relativePath)) { result.add(rootDirResource.createRelative(relativePath)); } } } return result; } finally { // Close jar file, but only if freshly obtained - // not from JarURLConnection, which might cache the file reference. if (newJarFile) { jarFile.close(); } } }
From source file:org.jboss.tools.jst.web.kb.taglib.TagLibraryManager.java
private static File convertUriToFile(String uri) throws IOException { File file = tempFiles.get(uri); if (file != null && file.exists()) { return file; }/*from ww w . j a va 2s .co m*/ URL url = new URL(uri); String filePath = url.getFile(); file = new File(filePath); if (!file.exists()) { URLConnection c = url.openConnection(); if (c instanceof JarURLConnection) { JarURLConnection connection = (JarURLConnection) c; JarFile jar = connection.getJarFile(); JarEntry entry = connection.getJarEntry(); File entryFile = new File(entry.getName()); String name = entryFile.getName(); String prefix = name; String sufix = null; int i = name.lastIndexOf('.'); if (i > 0 && i < name.length()) { prefix = name.substring(0, i); sufix = name.substring(i); } while (prefix.length() < 3) { prefix += "_"; } WebKbPlugin plugin = WebKbPlugin.getDefault(); if (plugin != null) { //The plug-in instance can be null at shutdown, when the plug-in is stopped. IPath path = plugin.getStateLocation(); File tmp = new File(path.toFile(), "tmp"); //$NON-NLS-1$ tmp.mkdirs(); file = File.createTempFile(prefix, sufix, tmp); file.deleteOnExit(); InputStream in = null; try { in = jar.getInputStream(entry); FileOutputStream out = new FileOutputStream(file); IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); } } } } if (file.exists()) { tempFiles.put(uri, file); } else { file = null; } return file; }