List of usage examples for java.util.jar JarEntry isDirectory
public boolean isDirectory()
From source file:net.sourceforge.lept4j.util.LoadLibs.java
/** * Copies resources from the jar file of the current thread and extract it * to the destination directory.// w w w. j av a 2s. c om * * @param jarConnection * @param destDir */ static void copyJarResourceToDirectory(JarURLConnection jarConnection, File destDir) { try { JarFile jarFile = jarConnection.getJarFile(); String jarConnectionEntryName = jarConnection.getEntryName() + "/"; /** * Iterate all entries in the jar file. */ for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) { JarEntry jarEntry = e.nextElement(); String jarEntryName = jarEntry.getName(); /** * Extract files only if they match the path. */ if (jarEntryName.startsWith(jarConnectionEntryName)) { String filename = jarEntryName.substring(jarConnectionEntryName.length()); File currentFile = new File(destDir, filename); if (jarEntry.isDirectory()) { currentFile.mkdirs(); } else { currentFile.deleteOnExit(); InputStream is = jarFile.getInputStream(jarEntry); OutputStream out = FileUtils.openOutputStream(currentFile); IOUtils.copy(is, out); is.close(); out.close(); } } } } catch (IOException e) { logger.log(Level.WARNING, e.getMessage(), e); } }
From source file:com.catalyst.sonar.score.batch.util.FileInstaller.java
/** * Copies a directory from a {@link JarURLConnection} to a destination * Directory outside the jar.// ww w . j a v a2s. c o m * * @param destDir * @param jarConnection * @return true if copy is successful, false otherwise. * @throws IOException */ public static boolean copyJarResourcesRecursively(final File destDir, final JarURLConnection jarConnection) throws IOException { logger.debug("copyJarResourcesRecursively()"); boolean success = true; final JarFile jarFile = jarConnection.getJarFile(); for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) { final JarEntry entry = e.nextElement(); if (entry.getName().startsWith(jarConnection.getEntryName())) { final String filename = StringUtils.removeStart(entry.getName(), // jarConnection.getEntryName()); final File f = new File(destDir, filename); if (!entry.isDirectory()) { final InputStream entryInputStream = jarFile.getInputStream(entry); if (!FileInstaller.copyStream(entryInputStream, f)) { success = false; logger.debug("returning " + success); return success; } entryInputStream.close(); } else { if (!FileInstaller.ensureDirectoryExists(f)) { logger.debug("throwing an IOException"); throw new IOException("Could not create directory: " + f.getAbsolutePath()); } } } } logger.debug("returning " + success); return success; }
From source file:net.sourceforge.tess4j.util.LoadLibs.java
/** * Copies resources from the jar file of the current thread and extract it * to the destination path./*w w w.j a v a2 s . c om*/ * * @param jarConnection * @param destPath destination file or directory */ static void copyJarResourceToPath(JarURLConnection jarConnection, File destPath) { try { JarFile jarFile = jarConnection.getJarFile(); String jarConnectionEntryName = jarConnection.getEntryName(); /** * Iterate all entries in the jar file. */ for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) { JarEntry jarEntry = e.nextElement(); String jarEntryName = jarEntry.getName(); /** * Extract files only if they match the path. */ if (jarEntryName.startsWith(jarConnectionEntryName)) { String filename = jarEntryName.substring(jarConnectionEntryName.length()); File currentFile = new File(destPath, filename); if (jarEntry.isDirectory()) { currentFile.mkdirs(); } else { currentFile.deleteOnExit(); InputStream is = jarFile.getInputStream(jarEntry); OutputStream out = FileUtils.openOutputStream(currentFile); IOUtils.copy(is, out); is.close(); out.close(); } } } } catch (IOException e) { logger.log(Level.SEVERE, e.getMessage(), e); } }
From source file:org.orbisgis.framework.BundleTools.java
private static void parseJar(File jarFilePath, Set<String> packages) throws IOException { try (JarFile jar = new JarFile(jarFilePath)) { Enumeration<? extends JarEntry> entryEnum = jar.entries(); while (entryEnum.hasMoreElements()) { JarEntry entry = entryEnum.nextElement(); if (!entry.isDirectory()) { final String path = entry.getName(); if (path.endsWith(".class")) { // Extract folder String parentPath = (new File(path)).getParent(); if (parentPath != null) { packages.add(parentPath.replace(File.separator, ".")); }/* www .j ava2s . c o m*/ } } } } }
From source file:com.github.sakserv.minicluster.impl.KnoxLocalCluster.java
public static boolean copyJarResourcesRecursively(final File destDir, final JarURLConnection jarConnection) throws IOException { final JarFile jarFile = jarConnection.getJarFile(); for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) { final JarEntry entry = e.nextElement(); if (entry.getName().startsWith(jarConnection.getEntryName())) { final String filename = StringUtils.removeStart(entry.getName(), // jarConnection.getEntryName()); final File f = new File(destDir, filename); if (!entry.isDirectory()) { final InputStream entryInputStream = jarFile.getInputStream(entry); if (!copyStream(entryInputStream, f)) { return false; }//from w w w .j a v a 2 s .c o m entryInputStream.close(); } else { if (!ensureDirectoryExists(f)) { throw new IOException("Could not create directory: " + f.getAbsolutePath()); } } } } return true; }
From source file:org.infoglue.deliver.portal.deploy.Deploy.java
private static int expandArchive(File warFile, File destDir) throws IOException { int numEntries = 0; if (!destDir.exists()) { destDir.mkdirs();/*from www . ja v a 2s. c o m*/ } JarFile jarFile = new JarFile(warFile); Enumeration files = jarFile.entries(); while (files.hasMoreElements()) { JarEntry entry = (JarEntry) files.nextElement(); String fileName = entry.getName(); File file = new File(destDir, fileName); File dirF = new File(file.getParent()); dirF.mkdirs(); if (entry.isDirectory()) { file.mkdirs(); } else { InputStream fis = jarFile.getInputStream(entry); FileOutputStream fos = new FileOutputStream(file); copyStream(fis, fos); fos.close(); } numEntries++; } return numEntries; }
From source file:com.enderville.enderinstaller.util.InstallScript.java
/** * Unpacks all the contents of the old minecraft.jar to the temp directory. * * @param tmpdir The temp directory to unpack to. * @param mcjar The location of the old minecraft.jar * @throws IOException// w ww . j a va 2s . c o m */ public static void unpackMCJar(File tmpdir, File mcjar) throws IOException { byte[] dat = new byte[4 * 1024]; JarFile jar = new JarFile(mcjar); Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String name = entry.getName(); //This gets rid of META-INF if it exists. if (name.startsWith("META-INF")) { continue; } InputStream in = jar.getInputStream(entry); File dest = new File(FilenameUtils.concat(tmpdir.getPath(), name)); if (entry.isDirectory()) { //I don't think this actually happens LOGGER.warn("Found a directory while iterating over jar."); dest.mkdirs(); } else if (!dest.getParentFile().exists()) { if (!dest.getParentFile().mkdirs()) { throw new IOException("Couldn't create directory for " + name); } } FileOutputStream out = new FileOutputStream(dest); int len = -1; while ((len = in.read(dat)) > 0) { out.write(dat, 0, len); } out.flush(); out.close(); in.close(); } }
From source file:com.flagleader.builder.BuilderConfig.java
public static void jarExtracting(String paramString1, String paramString2) { int i1 = 2048; BufferedOutputStream localBufferedOutputStream = null; BufferedInputStream localBufferedInputStream = null; JarEntry localJarEntry = null; JarFile localJarFile = null;//from w ww . j av a2 s. c om Enumeration localEnumeration = null; try { localJarFile = new JarFile(paramString2); localEnumeration = localJarFile.entries(); while (localEnumeration.hasMoreElements()) { localJarEntry = (JarEntry) localEnumeration.nextElement(); if (localJarEntry.isDirectory()) { new File(paramString1 + localJarEntry.getName()).mkdirs(); } else { localBufferedInputStream = new BufferedInputStream(localJarFile.getInputStream(localJarEntry)); byte[] arrayOfByte = new byte[i1]; FileOutputStream localFileOutputStream = new FileOutputStream( paramString1 + localJarEntry.getName()); localBufferedOutputStream = new BufferedOutputStream(localFileOutputStream, i1); int i2; while ((i2 = localBufferedInputStream.read(arrayOfByte, 0, i1)) != -1) localBufferedOutputStream.write(arrayOfByte, 0, i2); localBufferedOutputStream.flush(); localBufferedOutputStream.close(); localBufferedInputStream.close(); } } } catch (Exception localException1) { localException1.printStackTrace(); try { if (localBufferedOutputStream != null) { localBufferedOutputStream.flush(); localBufferedOutputStream.close(); } if (localBufferedInputStream != null) localBufferedInputStream.close(); } catch (Exception localException2) { localException2.printStackTrace(); } } finally { try { if (localBufferedOutputStream != null) { localBufferedOutputStream.flush(); localBufferedOutputStream.close(); } if (localBufferedInputStream != null) localBufferedInputStream.close(); } catch (Exception localException3) { localException3.printStackTrace(); } } }
From source file:com.arcusys.liferay.vaadinplugin.util.ControlPanelPortletUtil.java
/** * Extracts the jarEntry from the jarFile to the target directory. * * @param jarFile//www . jav a2 s .co m * @param jarEntry * @param targetDir * @return true if extraction was successful, false otherwise */ public static boolean extractJarEntry(JarFile jarFile, JarEntry jarEntry, String targetDir) { boolean extractSuccessful = false; File file = new File(targetDir); if (!file.exists()) { file.mkdir(); } if (jarEntry != null) { InputStream inputStream = null; try { inputStream = jarFile.getInputStream(jarEntry); file = new File(targetDir + jarEntry.getName()); if (jarEntry.isDirectory()) { file.mkdir(); } else { int size; byte[] buffer = new byte[2048]; FileOutputStream fileOutputStream = new FileOutputStream(file); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream, buffer.length); try { while ((size = inputStream.read(buffer, 0, buffer.length)) != -1) { bufferedOutputStream.write(buffer, 0, size); } bufferedOutputStream.flush(); } finally { bufferedOutputStream.close(); } } extractSuccessful = true; } catch (Exception e) { log.warn(e); } finally { close(inputStream); } } return extractSuccessful; }
From source file:com.jrummyapps.busybox.signing.ZipSigner.java
/** Add the SHA1 of every file to the manifest, creating it if necessary. */ private static Manifest addDigestsToManifest(final JarFile jar) throws IOException, GeneralSecurityException { final Manifest input = jar.getManifest(); final Manifest output = new Manifest(); final Attributes main = output.getMainAttributes(); main.putValue("Manifest-Version", MANIFEST_VERSION); main.putValue("Created-By", CREATED_BY); // We sort the input entries by name, and add them to the output manifest in sorted order. // We expect that the output map will be deterministic. final TreeMap<String, JarEntry> byName = new TreeMap<>(); for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) { JarEntry entry = e.nextElement(); byName.put(entry.getName(), entry); }//from w w w . j a va2s . c o m final MessageDigest md = MessageDigest.getInstance("SHA1"); final byte[] buffer = new byte[4096]; int num; for (JarEntry entry : byName.values()) { final String name = entry.getName(); if (!entry.isDirectory() && !name.equals(JarFile.MANIFEST_NAME) && !name.equals(CERT_SF_NAME) && !name.equals(CERT_RSA_NAME)) { InputStream data = jar.getInputStream(entry); while ((num = data.read(buffer)) > 0) { md.update(buffer, 0, num); } Attributes attr = null; if (input != null) { attr = input.getAttributes(name); } attr = attr != null ? new Attributes(attr) : new Attributes(); attr.putValue("SHA1-Digest", base64encode(md.digest())); output.getEntries().put(name, attr); } } return output; }