Example usage for java.util.jar JarEntry getName

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the name of the entry.

Usage

From source file:com.baomidou.framework.common.JarHelper.java

public static List<String> listFiles(JarFile jarFile, String endsWith) {
    if (jarFile == null || StringUtils.isEmpty(endsWith)) {
        return null;
    }// w  ww  .  j a v a 2  s  .  co  m
    List<String> files = new ArrayList<String>();
    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        String name = entry.getName();
        if (name.endsWith(endsWith)) {
            files.add(name);
        }
    }
    return files;
}

From source file:Main.java

/**
 * Iterates over APK (jar entries) to populate
 * /*  w ww.  j a  va 2 s  .  c o  m*/
 * @param projectFile
 * @return
 * @throws IOException
 * @throws CertificateException
 */
public static List<Certificate> populateCertificate(File projectFile) throws IOException, CertificateException {
    List<Certificate> certList = new ArrayList<Certificate>();
    JarFile jar = new JarFile(projectFile);
    Enumeration<JarEntry> jarEntries = jar.entries();
    while (jarEntries.hasMoreElements()) {
        JarEntry entry = jarEntries.nextElement();
        if (entry.getName().toLowerCase().contains(DSA) || entry.getName().toLowerCase().contains(RSA)) {
            certList.addAll(extractCertificate(jar, entry));
        }
    }
    return certList;
}

From source file:com.canoo.webtest.util.WebtestEmbeddingUtil.java

/**
 * Copies WebTest resources (ie webtest.xml, tools/*, the XSLs, CSSs and pictures, ...) package in WebTest jar file
 * into the provide folder. Indeed different Ant tasks can't work with resources in a jar file.
 * @param targetFolder the folder in which resources should be copied to
 * @throws java.io.IOException if the resources can be accessed or copied
 */// w w  w  .  j  a va  2  s.c om
static void copyWebTestResources(final File targetFolder) throws IOException {
    final String resourcesPrefix = "com/canoo/webtest/resources/";
    final URL webtestXmlUrl = WebtestEmbeddingUtil.class.getClassLoader()
            .getResource(resourcesPrefix + "webtest.xml");

    if (webtestXmlUrl == null) {
        throw new IllegalStateException("Can't find resource " + resourcesPrefix + "webtest.xml");
    } else if (webtestXmlUrl.toString().startsWith("jar:file")) {
        final String urlJarFileName = webtestXmlUrl.toString().replaceFirst("^jar:file:([^\\!]*).*$", "$1");
        final String jarFileName = URLDecoder.decode(urlJarFileName, Charset.defaultCharset().name());
        final JarFile jarFile = new JarFile(jarFileName);
        final String urlPrefix = StringUtils.removeEnd(webtestXmlUrl.toString(), "webtest.xml");

        for (final Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
            final JarEntry jarEntry = entries.nextElement();
            if (jarEntry.getName().startsWith(resourcesPrefix)) {
                final String relativeName = StringUtils.removeStart(jarEntry.getName(), resourcesPrefix);
                final URL url = new URL(urlPrefix + relativeName);
                final File targetFile = new File(targetFolder, relativeName);
                FileUtils.forceMkdir(targetFile.getParentFile());
                FileUtils.copyURLToFile(url, targetFile);
            }
        }
    } else if (webtestXmlUrl.toString().startsWith("file:")) {
        // we're probably developing and/or have a custom version of the resources in classpath
        final File webtestXmlFile = FileUtils.toFile(webtestXmlUrl);
        final File resourceFolder = webtestXmlFile.getParentFile();

        FileUtils.copyDirectory(resourceFolder, targetFolder);
    } else {
        throw new IllegalStateException(
                "Resource " + resourcesPrefix + "webtest.xml is not in a jar file: " + webtestXmlUrl);
    }
}

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

public static File copyTemplatesFromJarToTempDirectory(String jarFileName, String autoscanDirectoryName)
        throws IOException {
    // String jarName = "clickframes-webflow-plugin";
    // String AUTOSCAN_DIR = "/autoscan/";
    final String classpath = System.getProperty("java.class.path");
    String[] classpathArray = classpath.split(File.pathSeparatorChar + "");

    logger.debug("classpath = " + classpath);
    for (String classpathJar : classpathArray) {
        logger.debug("jar = " + classpathJar);
        if (classpathJar.contains(jarFileName)) {
            JarFile jarFile = new JarFile(classpathJar);
            File temporaryFolder = new File(System.getProperty("java.io.tmpdir"), "autoscanTempDir");
            temporaryFolder.mkdirs();//  w w w. jav a2  s . co  m
            temporaryFolder.deleteOnExit();
            // list all entries in jar
            Enumeration<JarEntry> resources = jarFile.entries();
            while (resources.hasMoreElements()) {
                JarEntry je = resources.nextElement();
                final String fileInJar = je.getName();
                // if autoscan target, copy to temp folder
                if (fileInJar.contains(autoscanDirectoryName)) {
                    copyFile(jarFile, temporaryFolder, je, fileInJar);
                }
            }
            return temporaryFolder;
        }
    }
    throw new RuntimeException("We're sorry, we couldn't find your jar");
}

From source file:com.photon.maven.plugins.android.common.JarHelper.java

/** Unjars the specified jar file into the the specified directory
 *
 * @param jarFile//from   ww w.  j  ava 2  s .  c om
 * @param outputDirectory
 * @param unjarListener
 * @throws IOException
 */
public static void unjar(JarFile jarFile, File outputDirectory, UnjarListener unjarListener)
        throws IOException {
    for (Enumeration en = jarFile.entries(); en.hasMoreElements();) {
        JarEntry entry = (JarEntry) en.nextElement();
        File entryFile = new File(outputDirectory, entry.getName());
        if (unjarListener.include(entry)) {
            if (!entryFile.getParentFile().exists() && !entryFile.getParentFile().mkdirs()) {
                throw new IOException("Error creating output directory: " + entryFile.getParentFile());
            }

            // If the entry is an actual file, unzip that too
            if (!entry.isDirectory()) {
                final InputStream in = jarFile.getInputStream(entry);
                try {
                    final OutputStream out = new FileOutputStream(entryFile);
                    try {
                        IOUtil.copy(in, out);
                    } finally {
                        IOUtils.closeQuietly(out);
                    }
                } finally {
                    IOUtils.closeQuietly(in);
                }
            }
        }
    }
}

From source file:com.reactive.hzdfs.dll.JarClassLoader.java

private static TreeSet<String> scanForPackages(String path) throws IOException {
    try (JarFile file = new JarFile(path)) {
        TreeSet<String> packages = new TreeSet<>(new Comparator<String>() {

            @Override/*from   w ww .  j a v  a  2s  .co  m*/
            public int compare(String o1, String o2) {
                if (o2.length() > o1.length() && o2.contains(o1))
                    return -1;
                else if (o2.length() < o1.length() && o1.contains(o2))
                    return 1;
                else
                    return o1.compareTo(o2);
            }
        });
        for (Enumeration<JarEntry> entries = file.entries(); entries.hasMoreElements();) {
            JarEntry entry = entries.nextElement();
            String name = entry.getName();

            if (name.endsWith(".class")) {
                String fqcn = ClassUtils.convertResourcePathToClassName(name);
                fqcn = StringUtils.delete(fqcn, ".class");
                packages.add(ClassUtils.getPackageName(fqcn));
            }
        }

        return packages;
    }
}

From source file:org.schemaspy.util.ResourceWriter.java

/**
 * Copies resources from the jar file of the current thread and extract it
 * to the destination path.//w  w w. jav  a 2s . c o  m
 *
 * @param jarConnection
 * @param destPath destination file or directory
 */
private static void copyJarResourceToPath(JarURLConnection jarConnection, File destPath, FileFilter filter) {
    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()) {
                    FileUtils.forceMkdir(currentFile);
                } else {
                    if (filter == null || filter.accept(currentFile)) {
                        InputStream is = jarFile.getInputStream(jarEntry);
                        OutputStream out = FileUtils.openOutputStream(currentFile);
                        IOUtils.copy(is, out);
                        is.close();
                        out.close();
                    }
                }
            }
        }
    } catch (IOException e) {
        LOGGER.warn(e.getMessage(), e);
    }
}

From source file:com.katsu.dwm.reflection.JarUtils.java

/**
 * Return a list of resources inside the jar file that their URL start with path
 * @param jar//from w  ww  .  j  a  va 2 s.com
 * @param path
 * @return 
 */
public static List<JarEntry> getJarContent(File jar, String path) {
    try {
        JarFile jf = new JarFile(jar);
        Enumeration<JarEntry> enu = jf.entries();
        List<JarEntry> result = new ArrayList<JarEntry>();
        while (enu.hasMoreElements()) {
            JarEntry je = enu.nextElement();
            if (je.getName().startsWith(path))
                result.add(je);
        }
        return result;
    } catch (Exception e) {
        logger.error(e);
    }
    return null;
}

From source file:org.eclipse.wb.internal.swing.laf.LafUtils.java

/**
 * Opens given <code>jarFile</code>, loads every class inside own {@link ClassLoader} and checks
 * loaded class for to be instance of {@link LookAndFeel}. Returns the array of {@link LafInfo}
 * containing all found {@link LookAndFeel} classes.
 * //w ww . jav  a  2  s  .  c  om
 * @param jarFileName
 *          the absolute OS path pointing to source JAR file.
 * @param monitor
 *          the progress monitor which checked for interrupt request.
 * @return the array of {@link UserDefinedLafInfo} containing all found {@link LookAndFeel}
 *         classes.
 */
public static UserDefinedLafInfo[] scanJarForLookAndFeels(String jarFileName, IProgressMonitor monitor)
        throws Exception {
    List<UserDefinedLafInfo> lafList = Lists.newArrayList();
    File jarFile = new File(jarFileName);
    URLClassLoader ucl = new URLClassLoader(new URL[] { jarFile.toURI().toURL() });
    JarFile jar = new JarFile(jarFile);
    Enumeration<?> entries = jar.entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = (JarEntry) entries.nextElement();
        String entryName = entry.getName();
        if (entry.isDirectory() || !entryName.endsWith(".class") || entryName.indexOf('$') >= 0) {
            continue;
        }
        String className = entryName.replace('/', '.').replace('\\', '.');
        className = className.substring(0, className.lastIndexOf('.'));
        Class<?> clazz = null;
        try {
            clazz = ucl.loadClass(className);
        } catch (Throwable e) {
            continue;
        }
        // check loaded class to be a non-abstract subclass of javax.swing.LookAndFeel class
        if (LookAndFeel.class.isAssignableFrom(clazz) && !Modifier.isAbstract(clazz.getModifiers())) {
            // use the class name as name of LAF
            String shortClassName = CodeUtils.getShortClass(className);
            // strip trailing "LookAndFeel"
            String lafName = StringUtils.chomp(shortClassName, "LookAndFeel");
            lafList.add(new UserDefinedLafInfo(StringUtils.isEmpty(lafName) ? shortClassName : lafName,
                    className, jarFileName));
        }
        // check for Cancel button pressed
        if (monitor.isCanceled()) {
            return lafList.toArray(new UserDefinedLafInfo[lafList.size()]);
        }
        // update ui
        while (DesignerPlugin.getStandardDisplay().readAndDispatch()) {
        }
    }
    return lafList.toArray(new UserDefinedLafInfo[lafList.size()]);
}

From source file:com.dragome.compiler.utils.FileManager.java

private static List<String> findClassesInJar(JarFile jarFile) {
    ArrayList<String> result = new ArrayList<String>();

    final Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        final JarEntry entry = entries.nextElement();
        final String entryName = entry.getName();
        if (entryName.endsWith(".class"))
            result.add(entryName.replace('/', File.separatorChar).replace(".class", ""));
    }// w w  w.j  a  v a  2 s  .  co m

    return result;
}