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.colombbus.tangara.FileUtils.java

public static int extractJar(File jar, File directory) {
    JarEntry entry = null;
    File currentFile = null;/*from   www.  j  av a  2s.co  m*/
    BufferedInputStream input = null;
    JarFile jarFile = null;
    int filesCount = 0;
    try {
        jarFile = new JarFile(jar);
        Enumeration<JarEntry> entries = jarFile.entries();

        while (entries.hasMoreElements()) {
            entry = entries.nextElement();
            currentFile = new File(directory, entry.getName());
            if (entry.isDirectory()) {
                currentFile.mkdir();
            } else {
                currentFile.createNewFile();
                input = new BufferedInputStream(jarFile.getInputStream(entry));
                copyFile(input, currentFile);
                input.close();
                filesCount++;
            }
        }
    } catch (IOException e) {
        LOG.error("Error extracting JAR file " + jar.getAbsolutePath(), e);
    } finally {
        try {
            if (input != null) {
                input.close();
            }
            if (jarFile != null) {
                jarFile.close();
            }
        } catch (IOException e) {
        }
    }
    return filesCount;
}

From source file:org.lpe.common.util.system.LpeSystemUtils.java

/**
 * Extracts a file/folder identified by the URL that resides in the
 * classpath, into the destiation folder.
 * //from w  w w . ja v  a  2s .co  m
 * @param url
 *            URL of the JAR file
 * @param dirOfInterest
 *            the name of the directory of interest
 * @param dest
 *            destination folder
 * 
 * @throws IOException
 *             ...
 * @throws URISyntaxException
 *             ...
 */
public static void extractJARtoTemp(URL url, String dirOfInterest, String dest)
        throws IOException, URISyntaxException {
    if (!url.getProtocol().equals("jar")) {
        throw new IllegalArgumentException("Cannot locate the JAR file.");
    }

    // create a temp lib directory
    File tempJarDirFile = new File(dest);
    if (!tempJarDirFile.exists()) {
        boolean ok = tempJarDirFile.mkdir();
        if (!ok) {
            logger.warn("Could not create directory {}", tempJarDirFile.getAbsolutePath());
        }

    } else {
        FileUtils.cleanDirectory(tempJarDirFile);
    }

    String urlStr = url.getFile();
    // if (urlStr.startsWith("jar:file:") || urlStr.startsWith("jar:") ||
    // urlStr.startsWith("file:")) {
    // urlStr = urlStr.replaceFirst("jar:", "");
    // urlStr = urlStr.replaceFirst("file:", "");
    // }
    if (urlStr.contains("!")) {
        final int endIndex = urlStr.indexOf("!");
        urlStr = urlStr.substring(0, endIndex);
    }

    URI uri = new URI(urlStr);

    final File jarFile = new File(uri);

    logger.debug("Unpacking jar file {}...", jarFile.getAbsolutePath());

    java.util.jar.JarFile jar = null;
    InputStream is = null;
    OutputStream fos = null;
    try {
        jar = new JarFile(jarFile);
        java.util.Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            java.util.jar.JarEntry file = (JarEntry) entries.nextElement();

            String destFileName = dest + File.separator + file.getName();
            if (destFileName.indexOf(dirOfInterest + File.separator) < 0
                    && destFileName.indexOf(dirOfInterest + "/") < 0) {
                continue;
            }

            logger.debug("unpacking {}...", file.getName());

            java.io.File f = new java.io.File(destFileName);
            if (file.isDirectory()) { // if its a directory, create it
                boolean ok = f.mkdir();
                if (!ok) {
                    logger.warn("Could not create directory {}", f.getAbsolutePath());
                }
                continue;
            }
            is = new BufferedInputStream(jar.getInputStream(file));
            fos = new BufferedOutputStream(new FileOutputStream(f));

            LpeStreamUtils.pipe(is, fos);
        }

        logger.debug("Unpacking jar file done.");
    } catch (IOException e) {
        throw e;
    } finally {
        if (jar != null) {
            jar.close();
        }
        if (fos != null) {
            fos.close();
        }
        if (is != null) {
            is.close();
        }
    }
}

From source file:org.mule.util.FileUtils.java

/**
 * Extract recources contain if jar (have to in classpath)
 *
 * @param connection          JarURLConnection to jar library
 * @param outputDir           Directory for unpack recources
 * @param keepParentDirectory true -  full structure of directories is kept; false - file - removed all directories, directory - started from resource point
 * @throws IOException if any error/*from  w  ww. j  a  v a  2s .c o m*/
 */
private static void extractJarResources(JarURLConnection connection, File outputDir,
        boolean keepParentDirectory) throws IOException {
    JarFile jarFile = connection.getJarFile();
    JarEntry jarResource = connection.getJarEntry();
    Enumeration entries = jarFile.entries();
    InputStream inputStream = null;
    OutputStream outputStream = null;
    int jarResourceNameLenght = jarResource.getName().length();
    for (; entries.hasMoreElements();) {
        JarEntry entry = (JarEntry) entries.nextElement();
        if (entry.getName().startsWith(jarResource.getName())) {

            String path = outputDir.getPath() + File.separator + entry.getName();

            //remove directory struct for file and first dir for directory
            if (!keepParentDirectory) {
                if (entry.isDirectory()) {
                    if (entry.getName().equals(jarResource.getName())) {
                        continue;
                    }
                    path = outputDir.getPath() + File.separator
                            + entry.getName().substring(jarResourceNameLenght, entry.getName().length());
                } else {
                    if (entry.getName().length() > jarResourceNameLenght) {
                        path = outputDir.getPath() + File.separator
                                + entry.getName().substring(jarResourceNameLenght, entry.getName().length());
                    } else {
                        path = outputDir.getPath() + File.separator + entry.getName()
                                .substring(entry.getName().lastIndexOf("/"), entry.getName().length());
                    }
                }
            }

            File file = FileUtils.newFile(path);
            if (!file.getParentFile().exists()) {
                if (!file.getParentFile().mkdirs()) {
                    throw new IOException("Could not create directory: " + file.getParentFile());
                }
            }
            if (entry.isDirectory()) {
                if (!file.exists() && !file.mkdirs()) {
                    throw new IOException("Could not create directory: " + file);
                }

            } else {
                try {
                    inputStream = jarFile.getInputStream(entry);
                    outputStream = new BufferedOutputStream(new FileOutputStream(file));
                    IOUtils.copy(inputStream, outputStream);
                } finally {
                    IOUtils.closeQuietly(inputStream);
                    IOUtils.closeQuietly(outputStream);
                }
            }

        }
    }
}

From source file:org.javaan.bytecode.JarFileLoader.java

private void processEntry(String path, String fileName, JarFile jar, List<Type> classes, JarEntry entry)
        throws IOException {
    if (!entry.isDirectory()) {
        String name = entry.getName();
        boolean isClass = name.endsWith(".class");
        boolean isLibrary = name.endsWith(".jar") || name.endsWith(".war") || name.endsWith(".ear");
        if (isClass) {
            ClassParser parser = new ClassParser(fileName, entry.getName());
            JavaClass javaClass = parser.parse();
            String filePath = path + File.pathSeparator + javaClass.getFileName();
            Type type = Type.create(javaClass, filePath);
            classes.add(type);//from w  w w  .j  ava  2s.  com
        } else if (isLibrary) {
            InputStream input = jar.getInputStream(entry);
            try {
                processJar(path + File.pathSeparator + entry.getName(), input, classes);
            } finally {
                input.close();
            }
        }
    }
}

From source file:org.massyframework.modules.utils.SunJdkModuleExporter.java

/**
 * ?SunJdk??//  w w  w .j av  a2s.  co  m
 * 
 * @param fileName
 * @return
 * @throws IOException
 */
protected List<String> getSunJdkPackageNames(String rtJar) throws IOException {
    Set<String> packageNames = new HashSet<String>();
    JarFile file = null;
    try {
        file = new JarFile(rtJar);
        Enumeration<JarEntry> em = file.entries();
        while (em.hasMoreElements()) {
            JarEntry entry = em.nextElement();
            if (!entry.isDirectory()) {
                String name = entry.getName();
                if (name.endsWith(".class")) {
                    int index = StringUtils.lastIndexOf(name, "/");
                    packageNames.add(StringUtils.substring(name, 0, index));
                }
            }
        }
    } finally {
        IOUtils.closeStream(file);
    }
    List<String> result = new ArrayList<String>(packageNames);
    Collections.sort(result);
    return result;
}

From source file:com.netease.hearttouch.hthotfix.HashTransformExecutor.java

public void transformJar(File inputFile, File outputFile) throws IOException {
    logger.info("HASHTRANSFORMJAR\t" + outputFile.getAbsolutePath());
    final JarFile jar = new JarFile(inputFile);
    final OutputJar outputJar = new OutputJar(outputFile);
    for (final Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) {
        final JarEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            //                new File(outFile, entry.getName()).mkdirs();
            continue;
        }//from  w w  w  .  j a v a 2  s.  co m
        final InputStream is = jar.getInputStream(entry);

        try {
            byte[] bytecode = IOUtils.toByteArray(is);
            if (entry.getName().endsWith(".class")) {
                if (!extension.getGeneratePatch()) {
                    hashFileGenerator.addClass(bytecode);
                    ClassReader cr = new ClassReader(bytecode);

                    outputJar.writeEntry(entry, hackInjector.inject(cr.getClassName(), bytecode));
                } else {
                    outputJar.writeEntry(entry, bytecode);
                }
            } else {
                outputJar.writeEntry(entry, bytecode);
            }
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    outputJar.close();
}

From source file:com.netease.hearttouch.hthotfix.patch.PatchRefGeneratorTransformExecutor.java

public void transformJar(File inputFile, File outputFile) throws IOException {
    final JarFile jar = new JarFile(inputFile);
    final OutputJar outputJar = new OutputJar(outputFile);
    for (final Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) {
        final JarEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            continue;
        }//from w ww  .  ja  v  a2 s  .c  o m
        final InputStream is = jar.getInputStream(entry);
        try {

            byte[] bytecode = IOUtils.toByteArray(is);
            if (entry.getName().endsWith(".class")) {
                if (extension.getGeneratePatch() && extension.getScanRef()) {
                    ClassReader cr = new ClassReader(bytecode);
                    String className = cr.getClassName();
                    if (extension.getGeneratePatch() && extension.getScanRef()) {
                        if (!refScanInstrument.isPatchClass(className.replace("/", "."))) {
                            boolean bPatchRef = refScanInstrument.hasReference(bytecode, project);
                            if (bPatchRef) {
                                patchJarHelper.writeClassToDirectory(className, bytecode);
                            }
                        }
                    }
                }
                outputJar.writeEntry(entry, bytecode);
            } else {
                outputJar.writeEntry(entry, bytecode);
            }
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    outputJar.close();
}

From source file:com.netease.hearttouch.hthotfix.patch.PatchGeneratorTransformExecutor.java

public void transformJar(File inputFile, File outputFile) throws IOException {
    final JarFile jar = new JarFile(inputFile);
    final OutputJar outputJar = new OutputJar(outputFile);
    for (final Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) {
        final JarEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            continue;
        }/*from   w  w  w.  j  a v a2s  .  com*/
        final InputStream is = jar.getInputStream(entry);
        try {

            byte[] bytecode = IOUtils.toByteArray(is);
            if (entry.getName().endsWith(".class")) {
                if (extension.getGeneratePatch()) {
                    ClassReader cr = new ClassReader(bytecode);
                    String className = cr.getClassName();
                    if ((hashFileParser != null) && (hashFileParser.isChanged(className, bytecode))) {
                        patchJarHelper.writeClassToDirectory(className,
                                hackInjector.inject(className, bytecode));
                        refScanInstrument.addPatchClassName(className, hackInjector.getLastSuperName());
                    }
                }
                outputJar.writeEntry(entry, bytecode);
            } else {
                outputJar.writeEntry(entry, bytecode);
            }
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    outputJar.close();
}

From source file:br.com.uol.runas.classloader.JarClassLoader.java

private void addUrlsFromJar(URL url) throws IOException, MalformedURLException {
    final JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
    jarFile = jarConnection.getJarFile();
    final Enumeration<JarEntry> entries = jarFile.entries();
    final URL jarUrl = new File(jarFile.getName()).toURI().toURL();
    final String base = jarUrl.toString();

    addURL(jarUrl);//from   w  w w.  j  av a 2  s  .  c om

    while (entries.hasMoreElements()) {
        final JarEntry entry = entries.nextElement();

        if (entry.isDirectory() || endsWithAny(entry.getName(), ".jar", ".war", ".ear")) {
            addURL(new URL(base + entry.getName()));
        }
    }
}

From source file:hudson.remoting.RegExpBenchmark.java

private List<String> getAllRTClasses() throws Exception {
    List<String> classes = new ArrayList<String>();
    // Object.class.getProtectionDomain().getCodeSource() returns null :(
    String javaHome = System.getProperty("java.home");
    JarFile jf = new JarFile(javaHome + "/lib/rt.jar");
    for (JarEntry je : Collections.list(jf.entries())) {
        if (!je.isDirectory() && je.getName().endsWith(".class")) {
            String name = je.getName().replace('/', '.');
            // remove the .class
            name = name.substring(0, name.length() - 6);
            classes.add(name);// w  ww  . j a v a 2 s  . co m
        }
    }
    jf.close();
    // add in a couple from xalan and commons just for testing...
    classes.add(new String("org.apache.commons.collections.functors.EvilClass"));
    classes.add(new String("org.codehaus.groovy.runtime.IWIllHackYou"));
    classes.add(new String("org.apache.xalan.YouAreOwned"));
    return classes;
}