Example usage for java.util.jar JarEntry isDirectory

List of usage examples for java.util.jar JarEntry isDirectory

Introduction

In this page you can find the example usage for java.util.jar JarEntry isDirectory.

Prototype

public boolean isDirectory() 

Source Link

Document

Returns true if this is a directory entry.

Usage

From source file:org.phenotips.variantstore.shared.ResourceManager.java

/**
 * Copy resources recursively from a folder specified by source in a jar file specified by jarPath
 * to a destination folder on the filesystem dest.
 *
 * @param jarPath the jar file/*from   w w  w  .  j  a  v a  2  s.  c om*/
 * @param source  the folder on the filesystem
 * @param dest    the destination
 * @throws IOException
 */
private static void copyResourcesFromJar(Path jarPath, Path source, Path dest) throws IOException {
    JarFile jar = new JarFile(jarPath.toFile());

    for (JarEntry entry : Collections.list(jar.entries())) {

        if (entry.getName().startsWith(source.toString())) {
            if (entry.isDirectory()) {
                Files.createDirectory(dest.resolve(entry.getName()));
            } else {
                Files.copy(jar.getInputStream(entry), dest.resolve(entry.getName()));
            }
        }

    }
}

From source file:org.reprap.configuration.store.ConfigurationInitializer.java

private static void copyJarTree(final URL source, final File target) throws IOException {
    final JarURLConnection jarConnection = (JarURLConnection) source.openConnection();
    final String prefix = jarConnection.getEntryName();
    final JarFile jarFile = jarConnection.getJarFile();
    for (final JarEntry jarEntry : Collections.list(jarFile.entries())) {
        final String entryName = jarEntry.getName();
        if (entryName.startsWith(prefix)) {
            if (!jarEntry.isDirectory()) {
                final String fileName = StringUtils.removeStart(entryName, prefix);
                final InputStream fileStream = jarFile.getInputStream(jarEntry);
                try {
                    FileUtils.copyInputStreamToFile(fileStream, new File(target, fileName));
                } finally {
                    fileStream.close();//from   w ww . jav  a2s  .c  o  m
                }
            }
        }
    }
}

From source file:request.processing.ServletLoader.java

public static void loadServlet(String servletDir) {
    try {//from ww  w. j av a  2s  .co  m
        JarFile jarFile = new JarFile(servletDir);
        Enumeration<JarEntry> e = jarFile.entries();

        URL[] urls = { new URL("jar:file:" + servletDir + "!/") };
        URLClassLoader cl = URLClassLoader.newInstance(urls);
        while (e.hasMoreElements()) {
            try {
                JarEntry je = (JarEntry) e.nextElement();
                if (je.isDirectory() || !je.getName().endsWith(".class")) {
                    continue;
                }
                String className = je.getName().substring(0, je.getName().length() - 6);
                className = className.replace('/', '.');
                Class c = cl.loadClass(className);
                if (Servlet.class.isAssignableFrom(c)) {
                    Constructor<Servlet> constructor = c.getConstructor();
                    Servlet i = constructor.newInstance();
                    namesToServlets.put(c.getSimpleName(), i);
                } else {
                    continue;
                }

            } catch (ClassNotFoundException cnfe) {
                System.err.println("Class not found");
                cnfe.printStackTrace();
            } catch (NoSuchMethodException e1) {
                System.err.println("No such method");
                e1.printStackTrace();
            } catch (SecurityException e1) {
                System.err.println("Security Exception");
                e1.printStackTrace();
            } catch (InstantiationException e1) {
                System.err.println("Instantiation Exception");
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                System.err.println("IllegalAccessException");
                e1.printStackTrace();
            } catch (IllegalArgumentException e1) {
                System.err.println("IllegalArgumentException");
                e1.printStackTrace();
            } catch (InvocationTargetException e1) {
                System.err.println("InvocationTargetException");
                e1.printStackTrace();
            }

        }
        jarFile.close();
    } catch (IOException e) {
        System.err.println("Not a jarFile, no biggie. Moving on.");
    }
}

From source file:ezbake.deployer.utilities.VersionHelper.java

private static String getVersionFromPomProperties(File artifact) throws IOException {
    List<JarEntry> pomPropertiesFiles = new ArrayList<>();
    String versionNumber = null;/* w w w . j a va2 s  .c  om*/
    try (JarFile jar = new JarFile(artifact)) {
        JarEntry entry;
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            entry = entries.nextElement();
            if (!entry.isDirectory() && entry.getName().endsWith("pom.properties")) {
                pomPropertiesFiles.add(entry);
            }
        }

        if (pomPropertiesFiles.size() == 1) {
            Properties pomProperties = new Properties();
            pomProperties.load(jar.getInputStream(pomPropertiesFiles.get(0)));
            versionNumber = pomProperties.getProperty("version", null);
        } else {
            logger.debug("Found {} pom.properties files. Cannot use that for version",
                    pomPropertiesFiles.size());
        }
    }
    return versionNumber;
}

From source file:eu.stratosphere.yarn.Utils.java

public static void copyJarContents(String prefix, String pathToJar) throws IOException {
    LOG.info("Copying jar (location: " + pathToJar + ") to prefix " + prefix);

    JarFile jar = null;//from  w  w  w . j  a v  a  2 s .  c  o m
    jar = new JarFile(pathToJar);
    Enumeration<JarEntry> enumr = jar.entries();
    byte[] bytes = new byte[1024];
    while (enumr.hasMoreElements()) {
        JarEntry entry = enumr.nextElement();
        if (entry.getName().startsWith(prefix)) {
            if (entry.isDirectory()) {
                File cr = new File(entry.getName());
                cr.mkdirs();
                continue;
            }
            InputStream inStream = jar.getInputStream(entry);
            File outFile = new File(entry.getName());
            if (outFile.exists()) {
                throw new RuntimeException("File unexpectedly exists");
            }
            FileOutputStream outputStream = new FileOutputStream(outFile);
            int read = 0;
            while ((read = inStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
            inStream.close();
            outputStream.close();
        }
    }
    jar.close();
}

From source file:io.cloudslang.maven.compiler.CloudSlangMavenCompiler.java

private static Map<String, byte[]> getSourceFilesForDependencies(String dependency) throws IOException {
    Path path = Paths.get(dependency);
    if (!Files.exists(path) || !path.toString().toLowerCase().endsWith(".jar")) {
        return Collections.emptyMap();
    }/*from w w w  .  j  av  a2 s .c  o  m*/

    Map<String, byte[]> sources = new HashMap<>();

    try (JarFile jar = new JarFile(dependency)) {
        Enumeration enumEntries = jar.entries();
        while (enumEntries.hasMoreElements()) {
            JarEntry file = (JarEntry) enumEntries.nextElement();
            if ((file == null) || (file.isDirectory()) || (!file.getName().endsWith(".sl.yaml")
                    && !file.getName().endsWith(".sl") && !file.getName().endsWith(".sl.yml"))) {
                continue;
            }

            byte[] bytes;
            try (InputStream is = jar.getInputStream(file)) {
                bytes = IOUtils.toByteArray(is);
                sources.put(file.getName(), bytes);
            }
        }
    }

    return sources;
}

From source file:net.sf.keystore_explorer.crypto.jcepolicy.JcePolicyUtil.java

/**
 * Get a JCE policy's details.// ww  w  .  j  a v a2  s.c om
 *
 * @param jcePolicy
 *            JCE policy
 * @return Policy details
 * @throws CryptoException
 *             If there was a problem getting the policy details
 */
public static String getPolicyDetails(JcePolicy jcePolicy) throws CryptoException {
    try {
        StringWriter sw = new StringWriter();

        File file = getJarFile(jcePolicy);

        // if there is no policy file at all, return empty string
        if (!file.exists()) {
            return "";
        }

        JarFile jar = new JarFile(file);

        Enumeration<JarEntry> jarEntries = jar.entries();

        while (jarEntries.hasMoreElements()) {
            JarEntry jarEntry = jarEntries.nextElement();

            String entryName = jarEntry.getName();

            if (!jarEntry.isDirectory() && entryName.endsWith(".policy")) {
                sw.write(entryName + ":\n\n");

                InputStreamReader isr = null;
                try {
                    isr = new InputStreamReader(jar.getInputStream(jarEntry));
                    CopyUtil.copy(isr, sw);
                } finally {
                    IOUtils.closeQuietly(isr);
                }

                sw.write('\n');
            }
        }

        return sw.toString();
    } catch (IOException ex) {
        throw new CryptoException(
                MessageFormat.format(res.getString("NoGetPolicyDetails.exception.message"), jcePolicy), ex);
    }
}

From source file:org.kse.crypto.jcepolicy.JcePolicyUtil.java

/**
 * Get a JCE policy's details.//from ww  w . ja  v  a  2s.  c  o m
 *
 * @param jcePolicy
 *            JCE policy
 * @return Policy details
 * @throws CryptoException
 *             If there was a problem getting the policy details
 */
public static String getPolicyDetails(JcePolicy jcePolicy) throws CryptoException {
    JarFile jarFile = null;
    try {
        StringWriter sw = new StringWriter();

        File file = getJarFile(jcePolicy);

        // if there is no policy file at all, return empty string
        if (!file.exists()) {
            return "";
        }

        jarFile = new JarFile(file);

        Enumeration<JarEntry> jarEntries = jarFile.entries();

        while (jarEntries.hasMoreElements()) {
            JarEntry jarEntry = jarEntries.nextElement();

            String entryName = jarEntry.getName();

            if (!jarEntry.isDirectory() && entryName.endsWith(".policy")) {
                sw.write(entryName + ":\n\n");

                InputStreamReader isr = null;
                try {
                    isr = new InputStreamReader(jarFile.getInputStream(jarEntry));
                    CopyUtil.copy(isr, sw);
                } finally {
                    IOUtils.closeQuietly(isr);
                }

                sw.write('\n');
            }
        }

        return sw.toString();
    } catch (IOException ex) {
        throw new CryptoException(
                MessageFormat.format(res.getString("NoGetPolicyDetails.exception.message"), jcePolicy), ex);
    } finally {
        IOUtils.closeQuietly(jarFile);
    }
}

From source file:io.joynr.util.JoynrUtil.java

public static void copyDirectoryFromJar(String jarName, String srcDir, File tmpDir) throws IOException {

    JarFile jf = null;/*from  ww w.ja va2  s  .  c om*/
    JarInputStream jarInputStream = null;

    try {
        jf = new JarFile(jarName);
        JarEntry je = jf.getJarEntry(srcDir);
        if (je.isDirectory()) {
            FileInputStream fis = new FileInputStream(jarName);
            BufferedInputStream bis = new BufferedInputStream(fis);
            jarInputStream = new JarInputStream(bis);
            JarEntry ze = null;
            while ((ze = jarInputStream.getNextJarEntry()) != null) {
                if (ze.isDirectory()) {
                    continue;
                }
                if (ze.getName().contains(je.getName())) {
                    InputStream is = jf.getInputStream(ze);
                    String name = ze.getName().substring(ze.getName().lastIndexOf("/") + 1);
                    File tmpFile = new File(tmpDir + "/" + name); //File.createTempFile(file.getName(), "tmp");
                    tmpFile.deleteOnExit();
                    OutputStream outputStreamRuntime = new FileOutputStream(tmpFile);
                    copyStream(is, outputStreamRuntime);
                }
            }
        }
    } finally {
        if (jf != null) {
            jf.close();
        }
        if (jarInputStream != null) {
            jarInputStream.close();
        }
    }
}

From source file:JarUtil.java

/**
 * Extracts the given jar-file to the specified directory. The target
 * directory will be cleaned before the jar-file will be extracted.
 * /*w w  w . j av  a 2s .  co  m*/
 * @param jarFile
 *            The jar file which should be unpacked
 * @param targetDir
 *            The directory to which the jar-content should be extracted.
 * @throws FileNotFoundException
 *             when the jarFile does not exist
 * @throws IOException
 *             when a file could not be written or the jar-file could not
 *             read.
 */
public static void unjar(File jarFile, File targetDir) throws FileNotFoundException, IOException {
    // clear target directory:
    if (targetDir.exists()) {
        targetDir.delete();
    }
    // create new target directory:
    targetDir.mkdirs();
    // read jar-file:
    String targetPath = targetDir.getAbsolutePath() + File.separatorChar;
    byte[] buffer = new byte[1024 * 1024];
    JarFile input = new JarFile(jarFile, false, ZipFile.OPEN_READ);
    Enumeration<JarEntry> enumeration = input.entries();
    for (; enumeration.hasMoreElements();) {
        JarEntry entry = enumeration.nextElement();
        if (!entry.isDirectory()) {
            // do not copy anything from the package cache:
            if (entry.getName().indexOf("package cache") == -1) {
                String path = targetPath + entry.getName();
                File file = new File(path);
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                FileOutputStream out = new FileOutputStream(file);
                InputStream in = input.getInputStream(entry);
                int read;
                while ((read = in.read(buffer)) != -1) {
                    out.write(buffer, 0, read);
                }
                in.close();
                out.close();
            }
        }
    }

}