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:javadepchecker.Main.java

public void processJar(JarFile jar) throws IOException {
    for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
        JarEntry entry = e.nextElement();
        String name = entry.getName();
        if (!entry.isDirectory() && name.endsWith(".class")) {
            this.current.add(name);
            InputStream stream = jar.getInputStream(entry);
            ClassParser parser = new ClassParser(stream, name);
            JavaClass jclass = parser.parse();
            this.pool = jclass.getConstantPool();
            new DescendingVisitor(jclass, this).visitConstantPool(this.pool);
        }/*w ww .  ja v a 2s. c om*/
    }
}

From source file:com.smartitengineering.util.simple.reflection.DefaultClassScannerImpl.java

private void scanJar(File jarFile, String parentPath, ClassVisitor classVisitor) {
    final JarFile jar = getJarFile(jarFile);
    try {/*ww w. j  a v  a 2s.  co m*/
        final Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry e = entries.nextElement();
            if (!e.isDirectory() && e.getName().startsWith(parentPath) && e.getName().endsWith(".class")) {
                visitClassFile(jar, e, classVisitor);
            }
        }
    } catch (Exception e) {
    } finally {
        try {
            if (jar != null) {
                jar.close();
            }
        } catch (IOException ex) {
        }
    }
}

From source file:org.sonatype.maven.plugin.emma4it.InstrumentProjectArtifactMojo.java

private void append(JarOutputStream output, File jar, boolean excludeMetainf) throws IOException {
    JarFile ejar = new JarFile(jar);

    Enumeration<JarEntry> entries = ejar.entries();
    while (entries.hasMoreElements()) {
        JarEntry jarEntry = entries.nextElement();
        if (jarEntry.isDirectory() || (excludeMetainf && jarEntry.getName().startsWith("META-INF"))) {
            continue;
        }//from w  w w  .j  a  v  a2s. com

        output.putNextEntry(jarEntry);

        InputStream input = ejar.getInputStream(jarEntry);
        try {
            IOUtil.copy(input, output);
        } finally {
            IOUtil.close(input);
        }

        output.flush();
    }

}

From source file:com.opensymphony.xwork2.util.finder.ResourceFinder.java

private static void readJarDirectoryEntries(URL location, String basePath, Set<String> resources)
        throws IOException {
    JarURLConnection conn = (JarURLConnection) location.openConnection();
    JarFile jarfile = null;/*from  ww w. j  a  v  a 2s .  c  o m*/
    jarfile = conn.getJarFile();

    Enumeration<JarEntry> entries = jarfile.entries();
    while (entries != null && entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        String name = entry.getName();

        if (entry.isDirectory() && StringUtils.startsWith(name, basePath)) {
            resources.add(name);
        }
    }
}

From source file:com.katsu.dwm.web.Loader.java

private void copyContent(File f, JarEntry je) {
    File rootPath = JarUtils.getRootApplicationContentPath();
    File temp = new File(rootPath, je.getName());
    if (je.isDirectory()) {
        logger.debug("Create path: " + temp.mkdirs() + " " + temp.getPath());
    } else {/*from   w ww  . j  av  a 2s .c o m*/
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            JarFile jf = new JarFile(f);
            is = jf.getInputStream(je);
            if (!temp.exists()) {
                temp.createNewFile();
            }
            fos = new FileOutputStream(temp);
            byte[] array = new byte[1024];
            int readed;
            while ((readed = is.read(array)) != -1) {
                fos.write(array, 0, readed);
            }
        } catch (Exception e) {
            logger.error(e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException ex) {
            }
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException ex) {
            }
        }
    }
}

From source file:com.thoughtworks.go.util.NestedJarClassLoader.java

private URL[] enumerateJar(URL urlOfJar) {
    LOGGER.debug("Enumerating jar: {}", urlOfJar);
    List<URL> urls = new ArrayList<>();
    urls.add(urlOfJar);/*from w w  w . j a  va  2  s . c o m*/
    try {
        JarInputStream jarStream = new JarInputStream(urlOfJar.openStream());
        JarEntry entry;
        while ((entry = jarStream.getNextJarEntry()) != null) {
            if (!entry.isDirectory() && entry.getName().endsWith(".jar")) {
                urls.add(expandJarAndReturnURL(jarStream, entry));
            }
        }
    } catch (IOException e) {
        LOGGER.error("Failed to enumerate jar {}", urlOfJar, e);
    }
    return urls.toArray(new URL[0]);
}

From source file:io.fabric8.vertx.maven.plugin.it.PackagingIT.java

@Test
public void testContentInTheJar() throws IOException, VerificationException {
    File testDir = initProject(PACKAGING_META_INF);
    assertThat(testDir).isDirectory();/*w ww.j  a  v a2 s .  c  o  m*/
    initVerifier(testDir);
    prepareProject(testDir, verifier);
    runPackage(verifier);

    assertThat(testDir).isNotNull();

    File out = new File(testDir, "target/vertx-demo-start-0.0.1.BUILD-SNAPSHOT.jar");
    assertThat(out).isFile();
    JarFile jar = new JarFile(out);

    // Commons utils
    // org.apache.commons.io.CopyUtils
    JarEntry entry = jar.getJarEntry("org/apache/commons/io/CopyUtils.class");
    assertThat(entry).isNotNull();
    assertThat(entry.isDirectory()).isFalse();

    // SLF4J-API
    // /org/slf4j/MDC.class
    entry = jar.getJarEntry("org/slf4j/MDC.class");
    assertThat(entry).isNotNull();
    assertThat(entry.isDirectory()).isFalse();

    // tc native
    // /org/apache/tomcat/Apr.class
    entry = jar.getJarEntry("org/apache/tomcat/Apr.class");
    assertThat(entry).isNotNull();
    assertThat(entry.isDirectory()).isFalse();
    // /org/apache/tomcat/apr.properties
    entry = jar.getJarEntry("org/apache/tomcat/apr.properties");
    assertThat(entry).isNotNull();
    assertThat(entry.isDirectory()).isFalse();
    // /META-INF/native/libnetty-tcnative-linux-x86_64.so
    entry = jar.getJarEntry("META-INF/native/libnetty-tcnative-linux-x86_64.so");
    assertThat(entry).isNotNull();
    assertThat(entry.isDirectory()).isFalse();

    // Jackson (transitive of vert.x core)
    // /com/fasterxml/jackson/annotation/JacksonAnnotation.class
    entry = jar.getJarEntry("com/fasterxml/jackson/annotation/JacksonAnnotation.class");
    assertThat(entry).isNotNull();
    assertThat(entry.isDirectory()).isFalse();

    // Not included
    // codegen - transitive optional
    entry = jar.getJarEntry("io/vertx/codegen/Case.class");
    assertThat(entry).isNull();
    // log4j - transitive provided
    entry = jar.getJarEntry("org/apache/log4j/MDC.class");
    assertThat(entry).isNull();
    // junit - test
    entry = jar.getJarEntry("junit/runner/Version.class");
    assertThat(entry).isNull();
    // commons-lang3 - provided
    entry = jar.getJarEntry("org/apache/commons/lang3/RandomUtils.class");
    assertThat(entry).isNull();

}

From source file:com.opensymphony.xwork2.util.finder.ResourceFinder.java

private static void readJarEntries(URL location, String basePath, Map<String, URL> resources)
        throws IOException {
    JarURLConnection conn = (JarURLConnection) location.openConnection();
    JarFile jarfile = null;/*  ww w.  j  a  va2  s . c  o m*/
    jarfile = conn.getJarFile();

    Enumeration<JarEntry> entries = jarfile.entries();
    while (entries != null && entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        String name = entry.getName();

        if (entry.isDirectory() || !name.startsWith(basePath) || name.length() == basePath.length()) {
            continue;
        }

        name = name.substring(basePath.length());

        if (name.contains("/")) {
            continue;
        }

        URL resource = new URL(location, name);
        resources.put(name, resource);
    }
}

From source file:com.googlecode.arit.systest.Application.java

public File getExplodedWAR() {
    if (explodedWAR == null) {
        File explodedDir = new File(tmpDir, "exploded");
        explodedDir.mkdir();/*from w  w w  .j  ava 2  s . c om*/
        String file = url.getFile();
        explodedWAR = new File(explodedDir, file.substring(file.lastIndexOf('/')));
        try {
            InputStream in = url.openStream();
            try {
                JarInputStream jar = new JarInputStream(in);
                JarEntry jarEntry;
                while ((jarEntry = jar.getNextJarEntry()) != null) {
                    File dest = new File(explodedWAR, jarEntry.getName());
                    if (jarEntry.isDirectory()) {
                        dest.mkdir();
                    } else {
                        dest.getParentFile().mkdirs();
                        OutputStream out = new FileOutputStream(dest);
                        try {
                            IOUtils.copy(jar, out);
                        } finally {
                            out.close();
                        }
                    }
                }
            } finally {
                in.close();
            }
        } catch (IOException ex) {
            throw new SystestException("Failed to explode WAR", ex);
        }
    }
    return explodedWAR;
}

From source file:gov.nih.nci.restgen.util.JarHelper.java

/**
 * Given an InputStream on a jar file, unjars the contents into the given
 * directory./*from  w  w w.j av a2 s .c  om*/
 */
public void unjar(InputStream in, File destDir) throws IOException {
    BufferedOutputStream dest = null;
    JarInputStream jis = new JarInputStream(in);
    JarEntry entry;
    while ((entry = jis.getNextJarEntry()) != null) {
        if (entry.isDirectory()) {
            File dir = new File(destDir, entry.getName());
            dir.mkdir();
            if (entry.getTime() != -1)
                dir.setLastModified(entry.getTime());
            continue;
        }
        int count;
        byte data[] = new byte[BUFFER_SIZE];
        File destFile = new File(destDir, entry.getName());
        if (mVerbose) {
            //System.out.println("unjarring " + destFile + " from " + entry.getName());
        }
        FileOutputStream fos = new FileOutputStream(destFile);
        dest = new BufferedOutputStream(fos, BUFFER_SIZE);
        while ((count = jis.read(data, 0, BUFFER_SIZE)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
        if (entry.getTime() != -1)
            destFile.setLastModified(entry.getTime());
    }
    jis.close();
}