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:MainClass.java

public static void main(String[] args) throws IOException {

    JarFile jf = new JarFile(args[0]);
    Enumeration e = jf.entries();
    while (e.hasMoreElements()) {
        JarEntry je = (JarEntry) e.nextElement();
        String name = je.getName();

        long crc = je.getCrc();
        System.out.println("Its CRC is " + crc);
        String comment = je.getComment();
        if (comment != null && !comment.equals("")) {
            System.out.println(comment);
        }//w w w. j a v a 2s .c  o  m
        if (je.isDirectory()) {
            System.out.println(name + " is a directory");
        }

    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    JarFile jf = new JarFile("a.jar");
    Enumeration e = jf.entries();
    while (e.hasMoreElements()) {
        JarEntry je = (JarEntry) e.nextElement();
        System.out.println(je.getName());
        long uncompressedSize = je.getSize();
        long compressedSize = je.getCompressedSize();
        long crc = je.getCrc();
        int method = je.getMethod();
        String comment = je.getComment();
        System.out.println(new Date(je.getTime()));
        System.out.println("from  " + uncompressedSize + " bytes to " + compressedSize);
        if (method == ZipEntry.STORED) {
            System.out.println("ZipEntry.STORED");
        } else if (method == ZipEntry.DEFLATED) {
            System.out.println(ZipEntry.DEFLATED);
        }/*from www.j a  v a  2  s .  c o  m*/
        System.out.println("Its CRC is " + crc);
        System.out.println(comment);
        System.out.println(je.isDirectory());

        Attributes a = je.getAttributes();
        if (a != null) {
            Object[] nameValuePairs = a.entrySet().toArray();
            for (int j = 0; j < nameValuePairs.length; j++) {
                System.out.println(nameValuePairs[j]);
            }
        }
        System.out.println();
    }
}

From source file:com.zb.app.common.file.FileUtils.java

public static void unJar(File jarFile, File toDir) throws IOException {
    JarFile jar = new JarFile(jarFile);
    try {/* w w w.j av  a2s  .c om*/
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = (JarEntry) entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = jar.getInputStream(entry);
                try {
                    File file = new File(toDir, entry.getName());
                    if (!file.getParentFile().mkdirs()) {
                        if (!file.getParentFile().isDirectory()) {
                            throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                        }
                    }
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        jar.close();
    }
}

From source file:JarUtils.java

/**
 * Add jar contents to the deployment archive under the given prefix
 *//* ww  w .ja  va 2s. co  m*/
public static String[] addJar(JarOutputStream outputStream, String prefix, File jar) throws IOException {

    ArrayList tmp = new ArrayList();
    FileInputStream fis = new FileInputStream(jar);
    JarInputStream jis = new JarInputStream(fis);
    JarEntry entry = jis.getNextJarEntry();
    while (entry != null) {
        if (entry.isDirectory() == false) {
            String entryName = prefix + entry.getName();
            tmp.add(entryName);
            addJarEntry(outputStream, entryName, jis);
        }
        entry = jis.getNextJarEntry();
    }
    jis.close();
    String[] names = new String[tmp.size()];
    tmp.toArray(names);
    return names;
}

From source file:org.clickframes.util.JarResourceUtil.java

private static void copyFile(JarFile jarFile, File temporaryFolder, JarEntry je, final String fileInJar)
        throws IOException, FileNotFoundException {
    if (!je.isDirectory()) {
        File tempFile = new File(temporaryFolder, fileInJar);
        tempFile.getParentFile().mkdirs();
        tempFile.deleteOnExit();/*from w w w  .  j  av a 2s  .  com*/
        IOUtils.copy(jarFile.getInputStream(je), new FileOutputStream(tempFile));
    }
}

From source file:org.raspinloop.fmi.VMRunnerUtils.java

private static boolean containsClass(JarFile jarFile, String agentRunnerClassName) {

    Enumeration<JarEntry> e = jarFile.entries();
    while (e.hasMoreElements()) {
        JarEntry je = e.nextElement();
        if (je.isDirectory() || !je.getName().endsWith(".class")) {
            continue;
        }//from w ww .j  a  v  a 2  s .c o m
        // -6 because of .class
        String className = je.getName().substring(0, je.getName().length() - 6);
        className = className.replace('/', '.');
        if (className.equals(agentRunnerClassName))
            return true;
    }
    return false;
}

From source file:com.apifest.oauth20.LifecycleEventHandlers.java

@SuppressWarnings("unchecked")
public static void loadLifecycleHandlers(URLClassLoader classLoader, String customJar) {
    try {// w ww  .  j a  v  a2  s. c o m
        if (classLoader != null) {
            JarFile jarFile = new JarFile(customJar);
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
                    continue;
                }
                // remove .class
                String className = entry.getName().substring(0, entry.getName().length() - 6);
                className = className.replace('/', '.');
                try {
                    // REVISIT: check for better solution
                    if (className.startsWith("org.jboss.netty") || className.startsWith("org.apache.log4j")
                            || className.startsWith("org.apache.commons")) {
                        continue;
                    }
                    Class<?> clazz = classLoader.loadClass(className);
                    if (clazz.isAnnotationPresent(OnRequest.class)
                            && LifecycleHandler.class.isAssignableFrom(clazz)) {
                        requestEventHandlers.add((Class<LifecycleHandler>) clazz);
                        log.debug("preIssueTokenHandler added {}", className);
                    }
                    if (clazz.isAnnotationPresent(OnResponse.class)
                            && LifecycleHandler.class.isAssignableFrom(clazz)) {
                        responseEventHandlers.add((Class<LifecycleHandler>) clazz);
                        log.debug("postIssueTokenHandler added {}", className);
                    }
                    if (clazz.isAnnotationPresent(OnException.class)
                            && ExceptionEventHandler.class.isAssignableFrom(clazz)) {
                        exceptionHandlers.add((Class<ExceptionEventHandler>) clazz);
                        log.debug("exceptionHandlers added {}", className);
                    }
                } catch (ClassNotFoundException e1) {
                    // continue
                }
            }
        }
    } catch (MalformedURLException e) {
        log.error("cannot load lifecycle handlers", e);
    } catch (IOException e) {
        log.error("cannot load lifecycle handlers", e);
    } catch (IllegalArgumentException e) {
        log.error(e.getMessage());
    }
}

From source file:org.apache.hadoop.hbase.util.RunTrigger.java

public static void unJar(File jarFile, File toDir) throws IOException {
    JarFile jar = new JarFile(jarFile);
    try {//from  w  w  w  .j  a va2  s  .co  m
        Enumeration entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = (JarEntry) entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = jar.getInputStream(entry);
                try {
                    File file = new File(toDir, entry.getName());
                    if (!file.getParentFile().mkdirs()) {
                        if (!file.getParentFile().isDirectory()) {
                            throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                        }
                    }
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        jar.close();
    }
}

From source file:org.jiemamy.utils.ResourceTraversal.java

/**
 * ???//from w  w  w  . ja  v a  2  s.  c o m
 * 
 * @param jarFile JarFile
 * @param handler ?
 * @throws IOException ????
 * @throws TraversalHandlerException ??????? 
 * @throws IllegalArgumentException ?{@code null}???
 */
public static void forEach(JarFile jarFile, ResourceHandler handler)
        throws IOException, TraversalHandlerException {
    Validate.notNull(jarFile);
    Validate.notNull(handler);
    Enumeration<JarEntry> enumeration = jarFile.entries();
    while (enumeration.hasMoreElements()) {
        JarEntry entry = enumeration.nextElement();
        if (!entry.isDirectory()) {
            String entryName = entry.getName().replace('\\', '/');
            InputStream is = jarFile.getInputStream(entry);
            try {
                handler.processResource(entryName, is);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    }
}

From source file:org.apache.torque.generator.configuration.JarConfigurationProvider.java

/**
 * Extracts the outlet configuration files from a jar file.
 * @param jarFile the jar file to process, not null.
 * @param outletConfigurationDirectory the name of the directory
 *        which contains the outlet configuration files. Cannot be
 *        a composite path like parent/child.
 * @return a set with the names of all outlet configuration files
 *         contained in the jar file./*  w ww  . j  ava2 s  .  c  o  m*/
 * @throws NullPointerException if jarFile
 *         or outletConfigurationDirectory is null
 */
static Collection<String> getOutletConfigurationNames(JarFile jarFile, String outletConfigurationDirectory) {
    if (log.isDebugEnabled()) {
        log.debug("Analyzing jar file " + jarFile.getName() + " seeking Directory "
                + outletConfigurationDirectory);
    }

    List<String> result = new ArrayList<String>();

    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        JarEntry jarEntry = entries.nextElement();
        if (jarEntry.isDirectory()) {
            continue;
        }
        String rawName = jarEntry.getName();
        if (!rawName.startsWith(outletConfigurationDirectory)) {
            continue;
        }
        String name = rawName.substring(rawName.lastIndexOf('/') + 1);

        int expectedRawNameLength = outletConfigurationDirectory.length() + name.length() + 1;
        if (rawName.length() != expectedRawNameLength) {
            // file is in a subdirectory of outletConfigurationSubdir,
            // we only consider files directly in
            // outletConfigurationSubdir
            continue;
        }
        result.add(name);
    }
    if (log.isDebugEnabled()) {
        log.debug("Found the following outlet configuration files " + result);
    }
    return result;
}