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:org.getobjects.foundation.NSJavaRuntime.java

private static void getClassNamesFromJarFile(String _jarPath, String _pkg, Set<String> classes_) {
    FileInputStream fos;/*from w w w.  ja  v a 2 s  .c  o  m*/
    try {
        fos = new FileInputStream(_jarPath);
    } catch (FileNotFoundException e1) {
        return; // TBD: log
    }

    JarInputStream jarFile;
    try {
        jarFile = new JarInputStream(fos);
    } catch (IOException e) {
        return; // TBD: log
    }

    do {
        JarEntry jarEntry;
        try {
            jarEntry = jarFile.getNextJarEntry();
        } catch (IOException e) {
            jarEntry = null;
        }

        if (jarEntry == null)
            break;

        String className = jarEntry.getName();
        if (!className.endsWith(".class"))
            continue;

        className = stripFilenameExtension(className);
        if (className.startsWith(_pkg))
            classes_.add(className.replace('/', '.'));
    } while (true);

    if (jarFile != null) {
        try {
            jarFile.close();
        } catch (IOException e) {
            // TBD: log?
        }
    }
}

From source file:net.ymate.platform.commons.util.ClassUtils.java

/**
 * ? Jar .class??'$'??Java"com/ymatesoft/common/A.class"~"com.ymatesoft.common.A"
 * //w  ww  . j a  va  2s  .c  om
 * @param collections
 * @param clazz
 * @param packageName
 * @param jarFile
 * @param callingClass
 */
@SuppressWarnings("unchecked")
protected static <T> void __doFindClassByJar(Collection<Class<T>> collections, Class<T> clazz,
        String packageName, JarFile jarFile, Class<?> callingClass) {
    Enumeration<JarEntry> _entriesEnum = jarFile.entries();
    for (; _entriesEnum.hasMoreElements();) {
        JarEntry _entry = _entriesEnum.nextElement();
        // ??? '/'  '.'?.class???'$'??
        String _className = _entry.getName().replaceAll("/", ".");
        if (_className.endsWith(".class") && _className.indexOf('$') < 0) {
            if (_className.startsWith(packageName)) {
                Class<?> _class = null;
                try {
                    _class = ResourceUtils.loadClass(_className.substring(0, _className.lastIndexOf('.')),
                            callingClass);
                    if (_class != null) {
                        if (clazz.isAnnotation()) {
                            if (isAnnotationOf(_class, (Class<Annotation>) clazz)) {
                                collections.add((Class<T>) _class);
                            }
                        } else if (clazz.isInterface()) {
                            if (isInterfaceOf(_class, clazz)) {
                                collections.add((Class<T>) _class);
                            }
                        } else if (isSubclassOf(_class, clazz)) {
                            collections.add((Class<T>) _class);
                        }
                    }
                } catch (NoClassDefFoundError e) {
                    _LOG.warn("", RuntimeUtils.unwrapThrow(e));
                } catch (ClassNotFoundException e) {
                    _LOG.warn("", RuntimeUtils.unwrapThrow(e));
                }
            }
        }
    }
}

From source file:com.amalto.core.query.SystemStorageTest.java

private static Collection<String> getConfigFiles() throws Exception {
    URL data = InitDBUtil.class.getResource("data"); //$NON-NLS-1$
    List<String> result = new ArrayList<String>();
    if ("jar".equals(data.getProtocol())) { //$NON-NLS-1$
        JarURLConnection connection = (JarURLConnection) data.openConnection();
        JarEntry entry = connection.getJarEntry();
        JarFile file = connection.getJarFile();
        Enumeration<JarEntry> entries = file.entries();
        while (entries.hasMoreElements()) {
            JarEntry e = entries.nextElement();
            if (e.getName().startsWith(entry.getName()) && !e.isDirectory()) {
                result.add(IOUtils.toString(file.getInputStream(e)));
            }//  www.j a  va 2s.c  om
        }
    } else {
        Collection<File> files = FileUtils.listFiles(new File(data.toURI()), new IOFileFilter() {

            @Override
            public boolean accept(File file) {
                return true;
            }

            @Override
            public boolean accept(File file, String s) {
                return true;
            }
        }, new IOFileFilter() {

            @Override
            public boolean accept(File file) {
                return !".svn".equals(file.getName()); //$NON-NLS-1$
            }

            @Override
            public boolean accept(File file, String s) {
                return !".svn".equals(file.getName()); //$NON-NLS-1$
            }
        });
        for (File f : files) {
            result.add(IOUtils.toString(new FileInputStream(f)));
        }
    }
    return result;
}

From source file:com.flagleader.builder.BuilderConfig.java

public static void jarExtracting(String paramString1, String paramString2) {
    int i1 = 2048;
    BufferedOutputStream localBufferedOutputStream = null;
    BufferedInputStream localBufferedInputStream = null;
    JarEntry localJarEntry = null;
    JarFile localJarFile = null;//w  w w . j  av a2s. c  om
    Enumeration localEnumeration = null;
    try {
        localJarFile = new JarFile(paramString2);
        localEnumeration = localJarFile.entries();
        while (localEnumeration.hasMoreElements()) {
            localJarEntry = (JarEntry) localEnumeration.nextElement();
            if (localJarEntry.isDirectory()) {
                new File(paramString1 + localJarEntry.getName()).mkdirs();
            } else {
                localBufferedInputStream = new BufferedInputStream(localJarFile.getInputStream(localJarEntry));
                byte[] arrayOfByte = new byte[i1];
                FileOutputStream localFileOutputStream = new FileOutputStream(
                        paramString1 + localJarEntry.getName());
                localBufferedOutputStream = new BufferedOutputStream(localFileOutputStream, i1);
                int i2;
                while ((i2 = localBufferedInputStream.read(arrayOfByte, 0, i1)) != -1)
                    localBufferedOutputStream.write(arrayOfByte, 0, i2);
                localBufferedOutputStream.flush();
                localBufferedOutputStream.close();
                localBufferedInputStream.close();
            }
        }
    } catch (Exception localException1) {
        localException1.printStackTrace();
        try {
            if (localBufferedOutputStream != null) {
                localBufferedOutputStream.flush();
                localBufferedOutputStream.close();
            }
            if (localBufferedInputStream != null)
                localBufferedInputStream.close();
        } catch (Exception localException2) {
            localException2.printStackTrace();
        }
    } finally {
        try {
            if (localBufferedOutputStream != null) {
                localBufferedOutputStream.flush();
                localBufferedOutputStream.close();
            }
            if (localBufferedInputStream != null)
                localBufferedInputStream.close();
        } catch (Exception localException3) {
            localException3.printStackTrace();
        }
    }
}

From source file:net.sf.keystore_explorer.crypto.signing.JarSigner.java

private static boolean ignoreJarEntry(JarEntry jarEntry) {

    String entryName = jarEntry.getName();

    // Entries to be ignored are all in the "META-INF" folder
    if (entryName.startsWith("META-INF/")) {
        if (entryName.toUpperCase().equals(MANIFEST_LOCATION)) {
            return true; // Manifest file - ignore
        }/*from  w w  w .j ava2 s. co  m*/

        if (entryName.toUpperCase().endsWith(SIGNATURE_EXT)) {
            return true; // Signature file - ignore
        }

        if (entryName.toUpperCase().endsWith(RSA_SIG_BLOCK_EXT)) {
            return true; // RSA signature block file - ignore
        }

        if (entryName.toUpperCase().endsWith(DSA_SIG_BLOCK_EXT)) {
            return true; // DSA signature block file - ignore
        }
    }

    return false;
}

From source file:AndroidUninstallStock.java

public static LinkedList<String> getLibsInApk(Path apk) throws IOException {
    LinkedList<String> libs = new LinkedList<String>();
    try (JarInputStream jar = new JarInputStream(
            Files.newInputStream(apk, new StandardOpenOption[] { StandardOpenOption.READ }))) {
        JarEntry jent;
        int pos;//from w w w  .  ja  v  a  2 s  .c  om
        while ((jent = jar.getNextJarEntry()) != null) {
            if (!jent.isDirectory() && ((pos = jent.getName().indexOf("lib/")) == 0 || pos == 1)) {
                libs.add(jent.getName());
            }
        }
    }
    return libs;
}

From source file:net.sf.keystore_explorer.crypto.signing.JarSigner.java

/**
 * Does the named signature already exist in the JAR file?
 *
 * @param jarFile//  w  w  w  .j a  va  2  s.c om
 *            JAR file
 * @param signatureName
 *            Signature name
 * @return True if it does, false otherwise
 * @throws IOException
 *             If an I/O problem occurs while examining the JAR file
 */
public static boolean hasSignature(File jarFile, String signatureName) throws IOException {
    JarFile jar = null;

    try {
        // Look for signature file (DSA or RSA)
        jar = new JarFile(jarFile);

        for (Enumeration<?> jarEntries = jar.entries(); jarEntries.hasMoreElements();) {
            JarEntry jarEntry = (JarEntry) jarEntries.nextElement();
            if (!jarEntry.isDirectory()) {
                if ((jarEntry.getName().equalsIgnoreCase(
                        MessageFormat.format(METAINF_FILE_LOCATION, signatureName, DSA_SIG_BLOCK_EXT)))
                        || (jarEntry.getName().equalsIgnoreCase(MessageFormat.format(METAINF_FILE_LOCATION,
                                signatureName, RSA_SIG_BLOCK_EXT)))) {
                    return true;
                }
            }
        }

        return false;
    } finally {
        IOUtils.closeQuietly(jar);
    }
}

From source file:org.colombbus.tangara.FileUtils.java

public static int extractJar(File jar, File directory) {
    JarEntry entry = null;
    File currentFile = null;/*from  w w  w.ja  va  2 s  .  c om*/
    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:com.databasepreservation.visualization.utils.SolrUtils.java

public static void setupSolrCloudConfigsets(String zkHost) {
    // before anything else, try to get a zookeeper client
    CloudSolrClient zkClient = new CloudSolrClient(zkHost);

    // get resources and copy them to a temporary directory
    Path databaseDir = null;//  w ww. ja v a  2 s.co m
    Path tableDir = null;
    Path savedSearchesDir = null;
    try {
        final File jarFile = new File(
                SolrManager.class.getProtectionDomain().getCodeSource().getLocation().toURI());

        // if it is a directory the application in being run from an IDE
        // in that case do not setup (assuming setup is done)
        if (!jarFile.isDirectory()) {
            databaseDir = Files.createTempDirectory("dbv_db_");
            tableDir = Files.createTempDirectory("dbv_tab_");
            savedSearchesDir = Files.createTempDirectory("dbv_tab_");
            final JarFile jar = new JarFile(jarFile);
            final Enumeration<JarEntry> entries = jar.entries();

            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                String name = entry.getName();

                String nameWithoutOriginPart = null;
                Path destination = null;
                if (name.startsWith(ViewerSafeConstants.SOLR_CONFIGSET_DATABASE_RESOURCE + "/")) {
                    nameWithoutOriginPart = name
                            .substring(ViewerSafeConstants.SOLR_CONFIGSET_DATABASE_RESOURCE.length() + 1);
                    destination = databaseDir;
                } else if (name.startsWith(ViewerSafeConstants.SOLR_CONFIGSET_TABLE_RESOURCE + "/")) {
                    nameWithoutOriginPart = name
                            .substring(ViewerSafeConstants.SOLR_CONFIGSET_TABLE_RESOURCE.length() + 1);
                    destination = tableDir;
                } else if (name.startsWith(ViewerSafeConstants.SOLR_CONFIGSET_SEARCHES_RESOURCE + "/")) {
                    nameWithoutOriginPart = name
                            .substring(ViewerSafeConstants.SOLR_CONFIGSET_SEARCHES_RESOURCE.length() + 1);
                    destination = savedSearchesDir;
                } else {
                    continue;
                }

                Path output = destination.resolve(nameWithoutOriginPart);
                if (name.endsWith("/")) {
                    Files.createDirectories(output);
                } else {
                    InputStream inputStream = SolrManager.class.getResourceAsStream("/" + name);
                    output = Files.createFile(output);
                    OutputStream outputStream = Files.newOutputStream(output, StandardOpenOption.CREATE,
                            StandardOpenOption.WRITE);
                    IOUtils.copy(inputStream, outputStream);
                    inputStream.close();
                    outputStream.close();
                }
            }
            jar.close();
        }
    } catch (IOException | URISyntaxException e) {
        LOGGER.error("Could not extract Solr configset", e);
        if (databaseDir != null) {
            try {
                FileUtils.deleteDirectoryRecursive(databaseDir);
            } catch (IOException e1) {
                LOGGER.debug("IO error deleting temporary folder: " + databaseDir, e1);
            }
        }
        if (tableDir != null) {
            try {
                FileUtils.deleteDirectoryRecursive(tableDir);
            } catch (IOException e1) {
                LOGGER.debug("IO error deleting temporary folder: " + tableDir, e1);
            }
        }
        databaseDir = null;
        tableDir = null;
    }

    // copy configurations to solr
    if (databaseDir != null && tableDir != null) {
        try {
            zkClient.uploadConfig(databaseDir, ViewerSafeConstants.SOLR_CONFIGSET_DATABASE);
        } catch (IOException e) {
            LOGGER.debug("IO error uploading database config to solr cloud", e);
        }
        try {
            zkClient.uploadConfig(tableDir, ViewerSafeConstants.SOLR_CONFIGSET_TABLE);
        } catch (IOException e) {
            LOGGER.debug("IO error uploading table config to solr cloud", e);
        }
        try {
            zkClient.uploadConfig(savedSearchesDir, ViewerSafeConstants.SOLR_CONFIGSET_SEARCHES);
        } catch (IOException e) {
            LOGGER.debug("IO error uploading saved searches config to solr cloud", e);
        }

        try {
            FileUtils.deleteDirectoryRecursive(databaseDir);
        } catch (IOException e1) {
            LOGGER.debug("IO error deleting temporary folder: " + databaseDir, e1);
        }
        try {
            FileUtils.deleteDirectoryRecursive(tableDir);
        } catch (IOException e1) {
            LOGGER.debug("IO error deleting temporary folder: " + tableDir, e1);
        }
        try {
            FileUtils.deleteDirectoryRecursive(savedSearchesDir);
        } catch (IOException e1) {
            LOGGER.debug("IO error deleting temporary folder: " + savedSearchesDir, e1);
        }
    }

    try {
        zkClient.close();
    } catch (IOException e) {
        LOGGER.debug("IO error closing connection to solr cloud", e);
    }
}

From source file:org.apache.storm.utils.ServerUtils.java

/**
 * Unpack matching files from a jar. Entries inside the jar that do
 * not match the given pattern will be skipped.
 *
 * @param jarFile the .jar file to unpack
 * @param toDir the destination directory into which to unpack the jar
 *///from  w  w  w  .ja  v  a 2 s  .  c  om
public static void unJar(File jarFile, File toDir) throws IOException {
    JarFile jar = new JarFile(jarFile);
    try {
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = jar.getInputStream(entry);
                try {
                    File file = new File(toDir, entry.getName());
                    ensureDirectory(file.getParentFile());
                    OutputStream out = new FileOutputStream(file);
                    try {
                        copyBytes(in, out, 8192);
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        jar.close();
    }
}