List of usage examples for java.util.jar JarEntry isDirectory
public boolean isDirectory()
From source file:com.infosupport.ellison.core.archive.ApplicationArchive.java
/** * Extracts a single {@link JarEntry} into the {@code destination} directory. * <p/>/* w w w . j a va 2 s .c om*/ * If {@code jarEntry} represents a directory, then a new directory is created in {@code destination}. If it is a * file, its {@link InputStream} is opened, and its contents are then written in {@code destination}. * * @param destination * the destination directory to unpack {@code jarEntry} into. This is the "root" directory where the entire * archive is unpacked into, as each {@code JarEntry}'s name points to whatever directory it should in fact be * in. See {@link File#File(java.io.File, String)} to see how this is achieved. * @param jar * the archive from which the {@code jarEntry} originates * @param jarEntry * the {@code JarEntry} to unpack * * @throws IOException * if any IO error occurred while trying to unpack a file */ protected void unpackJarEntry(File destination, JarFile jar, JarEntry jarEntry) throws IOException { byte[] ioBuffer = new byte[Constants.UNPACK_BUFFER_SIZE]; if (jarEntry.isDirectory()) { File newDir = new File(destination, jarEntry.getName()); boolean dirCreated = newDir.mkdir(); if (dirCreated) { newDir.deleteOnExit(); } } else { File outFile = new File(destination, jarEntry.getName()); outFile.deleteOnExit(); try (InputStream jarEntryInputStream = jar.getInputStream(jarEntry); OutputStream jarEntryOutputStream = new FileOutputStream(outFile)) { for (int readBytes = jarEntryInputStream .read(ioBuffer); readBytes != -1; readBytes = jarEntryInputStream.read(ioBuffer)) { jarEntryOutputStream.write(ioBuffer, 0, readBytes); } } } }
From source file:com.netflix.nicobar.core.archive.JarScriptArchive.java
protected JarScriptArchive(ScriptModuleSpec moduleSpec, Path jarPath, String moduleSpecEntry, long createTime) throws IOException { this.createTime = createTime; this.moduleSpec = Objects.requireNonNull(moduleSpec, "moduleSpec"); Objects.requireNonNull(jarPath, "jarFile"); if (!jarPath.isAbsolute()) throw new IllegalArgumentException("jarPath must be absolute."); // initialize the index JarFile jarFile = new JarFile(jarPath.toFile()); Set<String> indexBuilder; try {//from w ww . j a v a 2s . com Enumeration<JarEntry> jarEntries = jarFile.entries(); indexBuilder = new HashSet<String>(); while (jarEntries.hasMoreElements()) { JarEntry jarEntry = jarEntries.nextElement(); // Skip adding moduleSpec to archive entries if (jarEntry.getName().equals(moduleSpecEntry)) { continue; } if (!jarEntry.isDirectory()) { indexBuilder.add(jarEntry.getName()); } } } finally { jarFile.close(); } entryNames = Collections.unmodifiableSet(indexBuilder); rootUrl = jarPath.toUri().toURL(); }
From source file:hotbeans.support.JarFileHotBeanModuleLoader.java
/** * Extracts all the files in the module jar file, including nested jar files. The reason for extracting the complete * contents of the jar file (and not just the nested jar files) is to make sure the module jar file isn't locked, and * thus may be deleted./*from w ww.j a va 2 s.co m*/ */ private void extractLibs() throws IOException { if (logger.isDebugEnabled()) logger.debug("Extracting module jar file '" + moduleJarFile + "'."); JarFile jarFile = new JarFile(this.moduleJarFile); Enumeration entries = jarFile.entries(); JarEntry entry; String entryName; File extractedFile = null; FileOutputStream extractedFileOutputStream; while (entries.hasMoreElements()) { entry = (JarEntry) entries.nextElement(); if ((entry != null) && (!entry.isDirectory())) { entryName = entry.getName(); if (entryName != null) { // if( logger.isDebugEnabled() ) logger.debug("Extracting '" + entryName + "'."); // Copy nested jar file to temp dir extractedFile = new File(this.tempDir, entryName); extractedFile.getParentFile().mkdirs(); extractedFileOutputStream = new FileOutputStream(extractedFile); FileCopyUtils.copy(jarFile.getInputStream(entry), extractedFileOutputStream); extractedFileOutputStream = null; if ((entryName.startsWith(LIB_PATH)) && (entryName.toLowerCase().endsWith(".jar"))) { // Register nested jar file in "class path" super.addURL(extractedFile.toURI().toURL()); } } } } jarFile.close(); jarFile = null; super.addURL(tempDir.toURI().toURL()); // Add temp dir as class path (note that this must be added after all the // files have been extracted) if (logger.isDebugEnabled()) logger.debug("Done extracting module jar file '" + moduleJarFile + "'."); }
From source file:gov.nih.nci.restgen.util.JarHelper.java
public void unjar(File jarFile, String destdir) throws IOException { java.util.jar.JarFile jarfile = new java.util.jar.JarFile(jarFile); java.util.Enumeration<java.util.jar.JarEntry> enu = jarfile.entries(); while (enu.hasMoreElements()) { java.util.jar.JarEntry je = enu.nextElement(); java.io.File fl = new java.io.File(destdir + File.separator + je.getName()); if (!fl.exists()) { fl.getParentFile().mkdirs(); fl = new java.io.File(destdir + File.separator + je.getName()); }// ww w. j ava 2s . co m if (je.isDirectory()) { continue; } java.io.InputStream is = jarfile.getInputStream(je); java.io.FileOutputStream fo = new java.io.FileOutputStream(fl); while (is.available() > 0) { fo.write(is.read()); } fo.close(); is.close(); } }
From source file:goja.initialize.ctxbox.ClassSearcher.java
/** * jarclass//from w ww. jav a 2s .c om */ private List<String> findjarFiles(String baseDirName) { List<String> classFiles = Lists.newArrayList(); File baseDir = new File(baseDirName); if (!baseDir.exists() || !baseDir.isDirectory()) { LOG.error("file search error:" + baseDirName + " is not a dir?"); } else { File[] files = baseDir.listFiles(); if (files == null) { return Collections.EMPTY_LIST; } for (File file : files) { if (file.isDirectory()) { classFiles.addAll(findjarFiles(file.getAbsolutePath())); } else { if (includeAllJarsInLib || includeJars.contains(file.getName())) { JarFile localJarFile = null; try { localJarFile = new JarFile(new File(baseDirName + File.separator + file.getName())); Enumeration<JarEntry> entries = localJarFile.entries(); while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); String entryName = jarEntry.getName(); if (scanPackages.isEmpty()) { if (!jarEntry.isDirectory() && entryName.endsWith(".class")) { String className = StringUtils.replace(entryName, StringPool.SLASH, ".") .substring(0, entryName.length() - 6); classFiles.add(className); } } else { for (String scanPackage : scanPackages) { scanPackage = scanPackage.replaceAll("\\.", "\\" + File.separator); if (!jarEntry.isDirectory() && entryName.endsWith(".class") && entryName.startsWith(scanPackage)) { String className = StringUtils.replace(entryName, File.separator, ".") .substring(0, entryName.length() - 6); classFiles.add(className); } } } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (localJarFile != null) { localJarFile.close(); } } catch (IOException e) { LOG.error("close jar file has error!", e); } } } } } } return classFiles; }
From source file:org.drools.guvnor.server.RepositoryModuleService.java
private JarInputStream typesForModel(List<String> res, AssetItem asset) throws IOException { if (!asset.isBinary()) { return null; }/*w ww . ja va2s . c om*/ if (asset.getBinaryContentAttachment() == null) { return null; } JarInputStream jis; jis = new JarInputStream(asset.getBinaryContentAttachment()); JarEntry entry = null; while ((entry = jis.getNextJarEntry()) != null) { if (!entry.isDirectory()) { if (entry.getName().endsWith(".class") && !entry.getName().endsWith("package-info.class")) { res.add(ModelContentHandler.convertPathToName(entry.getName())); } } } return jis; }
From source file:org.twdata.pkgscanner.InternalScanner.java
/** * Finds matching classes within a jar files that contains a folder structure * matching the package structure. If the File is not a JarFile or does not exist a warning * will be logged, but no error will be raised. * * @param test a Test used to filter the classes that are discovered * @param file the jar file to be examined for classes * @return List of packages to export.//from ww w. j a v a 2 s . c o m */ List<ExportPackage> loadImplementationsInJar(Test test, File file) { final List<ExportPackage> localExports = new ArrayList<ExportPackage>(); Set<String> packages = this.jarContentCache.get(file.getPath()); if (packages == null) { packages = new HashSet<String>(); try { final JarFile jarFile = new JarFile(file); for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) { final JarEntry entry = e.nextElement(); final String name = entry.getName(); if (!entry.isDirectory()) { String pkg = name; final int pos = pkg.lastIndexOf('/'); if (pos > -1) { pkg = pkg.substring(0, pos); } pkg = pkg.replace('/', '.'); final boolean newlyAdded = packages.add(pkg); if (newlyAdded && this.log.isDebugEnabled()) { // Use newlyAdded as we don't want to log duplicates this.log.debug(String.format("Found package '%s' in jar file [%s]", pkg, file)); } } } } catch (final IOException ioe) { this.log.error("Could not search jar file '" + file + "' for classes matching criteria: " + test + " due to an IOException" + ioe); return Collections.emptyList(); } finally { // set the cache, even if the scan produced an error this.jarContentCache.put(file.getPath(), packages); } } final Set<String> scanned = new HashSet<String>(); for (final String pkg : packages) { if (!scanned.contains(pkg)) { if (test.matchesPackage(pkg)) { localExports.add(new ExportPackage(pkg, determinePackageVersion(file, pkg), file)); } scanned.add(pkg); } } return localExports; }
From source file:org.apache.sling.maven.bundlesupport.AbstractBundleDeployMojo.java
/** * Change the version in jar//from w ww . j av a2 s . com * * @param newVersion * @param file * @return * @throws MojoExecutionException */ protected File changeVersion(File file, String oldVersion, String newVersion) throws MojoExecutionException { String fileName = file.getName(); int pos = fileName.indexOf(oldVersion); fileName = fileName.substring(0, pos) + newVersion + fileName.substring(pos + oldVersion.length()); JarInputStream jis = null; JarOutputStream jos; OutputStream out = null; JarFile sourceJar = null; try { // now create a temporary file and update the version sourceJar = new JarFile(file); final Manifest manifest = sourceJar.getManifest(); manifest.getMainAttributes().putValue("Bundle-Version", newVersion); jis = new JarInputStream(new FileInputStream(file)); final File destJar = new File(file.getParentFile(), fileName); out = new FileOutputStream(destJar); jos = new JarOutputStream(out, manifest); jos.setMethod(JarOutputStream.DEFLATED); jos.setLevel(Deflater.BEST_COMPRESSION); JarEntry entryIn = jis.getNextJarEntry(); while (entryIn != null) { JarEntry entryOut = new JarEntry(entryIn.getName()); entryOut.setTime(entryIn.getTime()); entryOut.setComment(entryIn.getComment()); jos.putNextEntry(entryOut); if (!entryIn.isDirectory()) { IOUtils.copy(jis, jos); } jos.closeEntry(); jis.closeEntry(); entryIn = jis.getNextJarEntry(); } // close the JAR file now to force writing jos.close(); return destJar; } catch (IOException ioe) { throw new MojoExecutionException("Unable to update version in jar file.", ioe); } finally { if (sourceJar != null) { try { sourceJar.close(); } catch (IOException ex) { // close } } IOUtils.closeQuietly(jis); IOUtils.closeQuietly(out); } }
From source file:org.drools.guvnor.server.contenthandler.soa.JarFileContentHandler.java
private String getClassesFromJar(AssetItem assetItem) throws IOException { Map<String, String> nonCollidingImports = new HashMap<String, String>(); String assetPackageName = assetItem.getModuleName(); //Setup class-loader to check for class visibility JarInputStream cljis = new JarInputStream(assetItem.getBinaryContentAttachment()); List<JarInputStream> jarInputStreams = new ArrayList<JarInputStream>(); jarInputStreams.add(cljis);/* ww w .j a v a2 s.com*/ ClassLoaderBuilder clb = new ClassLoaderBuilder(jarInputStreams); ClassLoader cl = clb.buildClassLoader(); //Reset stream to read classes JarInputStream jis = new JarInputStream(assetItem.getBinaryContentAttachment()); JarEntry entry = null; //Get Class names from JAR, only the first occurrence of a given Class leaf name will be inserted. Thus //"org.apache.commons.lang.NumberUtils" will be imported but "org.apache.commons.lang.math.NumberUtils" //will not, assuming it follows later in the JAR structure. while ((entry = jis.getNextJarEntry()) != null) { if (!entry.isDirectory()) { if (entry.getName().endsWith(".class") && entry.getName().indexOf('$') == -1 && !entry.getName().endsWith("package-info.class")) { String fullyQualifiedName = convertPathToName(entry.getName()); if (isClassVisible(cl, fullyQualifiedName, assetPackageName)) { String leafName = getLeafName(fullyQualifiedName); if (!nonCollidingImports.containsKey(leafName)) { nonCollidingImports.put(leafName, fullyQualifiedName); } } } } } //Build list of classes StringBuffer classes = new StringBuffer(); for (String value : nonCollidingImports.values()) { classes.append(value + "\n"); } return classes.toString(); }
From source file:com.jayway.maven.plugins.android.phase04processclasses.DexMojo.java
private void unjar(JarFile jarFile, File outputDirectory) throws IOException { for (Enumeration en = jarFile.entries(); en.hasMoreElements();) { JarEntry entry = (JarEntry) en.nextElement(); File entryFile = new File(outputDirectory, entry.getName()); if (!entryFile.getParentFile().exists() && !entry.getName().startsWith("META-INF")) { entryFile.getParentFile().mkdirs(); }/* w w w . j a v a 2 s .com*/ if (!entry.isDirectory() && entry.getName().endsWith(".class")) { final InputStream in = jarFile.getInputStream(entry); try { final OutputStream out = new FileOutputStream(entryFile); try { IOUtil.copy(in, out); } finally { IOUtils.closeQuietly(out); } } finally { IOUtils.closeQuietly(in); } } } }