List of usage examples for java.net JarURLConnection getJarEntry
public JarEntry getJarEntry() throws IOException
From source file:org.mule.util.FileUtils.java
/** * Extract recources contain if jar (have to in classpath) * * @param connection JarURLConnection to jar library * @param outputDir Directory for unpack recources * @param keepParentDirectory true - full structure of directories is kept; false - file - removed all directories, directory - started from resource point * @throws IOException if any error//from w ww . j av a 2 s .co m */ private static void extractJarResources(JarURLConnection connection, File outputDir, boolean keepParentDirectory) throws IOException { JarFile jarFile = connection.getJarFile(); JarEntry jarResource = connection.getJarEntry(); Enumeration entries = jarFile.entries(); InputStream inputStream = null; OutputStream outputStream = null; int jarResourceNameLenght = jarResource.getName().length(); for (; entries.hasMoreElements();) { JarEntry entry = (JarEntry) entries.nextElement(); if (entry.getName().startsWith(jarResource.getName())) { String path = outputDir.getPath() + File.separator + entry.getName(); //remove directory struct for file and first dir for directory if (!keepParentDirectory) { if (entry.isDirectory()) { if (entry.getName().equals(jarResource.getName())) { continue; } path = outputDir.getPath() + File.separator + entry.getName().substring(jarResourceNameLenght, entry.getName().length()); } else { if (entry.getName().length() > jarResourceNameLenght) { path = outputDir.getPath() + File.separator + entry.getName().substring(jarResourceNameLenght, entry.getName().length()); } else { path = outputDir.getPath() + File.separator + entry.getName() .substring(entry.getName().lastIndexOf("/"), entry.getName().length()); } } } File file = FileUtils.newFile(path); if (!file.getParentFile().exists()) { if (!file.getParentFile().mkdirs()) { throw new IOException("Could not create directory: " + file.getParentFile()); } } if (entry.isDirectory()) { if (!file.exists() && !file.mkdirs()) { throw new IOException("Could not create directory: " + file); } } else { try { inputStream = jarFile.getInputStream(entry); outputStream = new BufferedOutputStream(new FileOutputStream(file)); IOUtils.copy(inputStream, outputStream); } finally { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); } } } } }
From source file:org.springframework.core.io.support.PathMatchingResourcePatternResolver.java
/** * Find all resources in jar files that match the given location pattern * via the Ant-style PathMatcher.//from ww w . jav a 2s .c o m * @param rootDirResource the root directory as Resource * @param rootDirURL the pre-resolved root directory URL * @param subPattern the sub pattern to match (below the root directory) * @return a mutable Set of matching Resource instances * @throws IOException in case of I/O errors * @since 4.3 * @see java.net.JarURLConnection * @see org.springframework.util.PathMatcher */ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource, URL rootDirURL, String subPattern) throws IOException { URLConnection con = rootDirURL.openConnection(); JarFile jarFile; String jarFileUrl; String rootEntryPath; boolean closeJarFile; if (con instanceof JarURLConnection) { // Should usually be the case for traditional JAR files. JarURLConnection jarCon = (JarURLConnection) con; ResourceUtils.useCachesIfNecessary(jarCon); jarFile = jarCon.getJarFile(); jarFileUrl = jarCon.getJarFileURL().toExternalForm(); JarEntry jarEntry = jarCon.getJarEntry(); rootEntryPath = (jarEntry != null ? jarEntry.getName() : ""); closeJarFile = !jarCon.getUseCaches(); } 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 = rootDirURL.getFile(); try { int separatorIndex = urlFile.indexOf(ResourceUtils.WAR_URL_SEPARATOR); if (separatorIndex == -1) { separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR); } if (separatorIndex != -1) { jarFileUrl = urlFile.substring(0, separatorIndex); rootEntryPath = urlFile.substring(separatorIndex + 2); // both separators are 2 chars jarFile = getJarFile(jarFileUrl); } else { jarFile = new JarFile(urlFile); jarFileUrl = urlFile; rootEntryPath = ""; } closeJarFile = true; } catch (ZipException ex) { if (logger.isDebugEnabled()) { logger.debug("Skipping invalid jar classpath entry [" + urlFile + "]"); } return Collections.emptySet(); } } 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<>(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 { if (closeJarFile) { jarFile.close(); } } }
From source file:org.wymiwyg.commons.util.dirbrowser.ZipPathNode.java
/** * @param connection/* ww w . j ava 2 s .c o m*/ */ public ZipPathNode(JarURLConnection connection) throws IOException { entryName = connection.getJarEntry().getName(); file = connection.getJarFile(); }
From source file:org.zkoss.spring.core.io.support.PathMatchingResourcePatternResolver.java
/** * Find all resources in jar files that match the given location pattern * via the Ant-style PathMatcher./*from w w w . j a v a2s . c om*/ * @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 * @see org.springframework.util.PathMatcher */ 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; jarCon.setUseCaches(false); 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(ResourceUtils.JAR_URL_SEPARATOR); if (separatorIndex != -1) { jarFileUrl = urlFile.substring(0, separatorIndex); rootEntryPath = urlFile.substring(separatorIndex + ResourceUtils.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 entries = jarFile.entries(); entries.hasMoreElements();) { JarEntry entry = (JarEntry) 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(); } } }