Example usage for java.net JarURLConnection getJarFile

List of usage examples for java.net JarURLConnection getJarFile

Introduction

In this page you can find the example usage for java.net JarURLConnection getJarFile.

Prototype

public abstract JarFile getJarFile() throws IOException;

Source Link

Document

Return the JAR file for this connection.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URL("jar:file:/c://my.jar!/");
    JarURLConnection conn = (JarURLConnection) url.openConnection();
    JarFile jarfile = conn.getJarFile();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URL("jar:file:/c://my.jar!/");
    JarURLConnection conn = (JarURLConnection) url.openConnection();

    JarFile jarFile = conn.getJarFile();
    System.out.println(jarFile);//w  ww .ja  v  a2  s . c o  m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URL("jar:file:/c://my.jar!/");
    JarURLConnection conn = (JarURLConnection) url.openConnection();

    conn = (JarURLConnection) url.openConnection();
    JarFile jarfile = conn.getJarFile();
}

From source file:org.zoneproject.extractor.plugin.langdetect.LangDetect.java

public static void init(String profileDirectory) throws LangDetectException, IOException {
    Enumeration<URL> en = Detector.class.getClassLoader().getResources(profileDirectory);
    List<String> profiles = new ArrayList<String>();
    if (en.hasMoreElements()) {
        URL url = en.nextElement();
        JarURLConnection urlcon = (JarURLConnection) url.openConnection();
        JarFile jar = urlcon.getJarFile();
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            String entry = entries.nextElement().getName();
            if (entry.startsWith(profileDirectory)) {
                InputStream in = Detector.class.getClassLoader().getResourceAsStream(entry);
                profiles.add(IOUtils.toString(in));
            }//from w  ww. jav  a  2  s  .  c o m
        }
    }
    DetectorFactory.loadProfile(profiles);

}

From source file:org.lilyproject.lilyservertestfw.ConfUtil.java

public static void copyFromJar(File confDir, String confResourcePath, JarURLConnection jarConnection)
        throws IOException {
    JarFile jarFile = jarConnection.getJarFile();
    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        if (entry.getName().startsWith(confResourcePath)) {
            String fileName = StringUtils.removeStart(entry.getName(), confResourcePath);
            if (entry.isDirectory()) {
                File subDir = new File(confDir, fileName);
                subDir.mkdirs();//from  w  w  w .j  a  v a 2 s.c  o m
            } else {
                InputStream entryInputStream = null;
                try {
                    entryInputStream = jarFile.getInputStream(entry);
                    FileUtils.copyInputStreamToFile(entryInputStream, new File(confDir, fileName));
                } finally {
                    if (entryInputStream != null) {
                        entryInputStream.close();
                    }
                }
            }
        }
    }
}

From source file:com.athomas.androidkickstartr.util.ResourcesUtils.java

private static void copyResourcesToFromJar(File target, URL url) throws IOException {
    JarURLConnection connection = (JarURLConnection) url.openConnection();
    JarFile jarFile = connection.getJarFile();

    Enumeration<JarEntry> entries = jarFile.entries();

    while (entries.hasMoreElements()) {
        JarEntry jarEntry = entries.nextElement();
        InputStream is = jarFile.getInputStream(jarEntry);
        String entryPath = jarEntry.getName();

        File file = null;/*  ww  w  . ja v  a 2s . c  o m*/
        String dirs = "";
        if (entryPath.contains("/")) {
            int lastIndexOf = entryPath.lastIndexOf("/");
            dirs = (String) entryPath.subSequence(0, lastIndexOf + 1);
        }

        File parent = new File(target, dirs);
        parent.mkdirs();

        if (!jarEntry.isDirectory()) {
            String[] splitedPath = entryPath.split("/");
            String fileName = splitedPath[splitedPath.length - 1];
            file = new File(parent, fileName);
            FileUtils.copyInputStreamToFile(is, file);
        }
    }
}

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 .j  a  v  a2  s  . com
 *
 * @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.topclouders.releaseplugin.helper.FileHelper.java

/**
 * This method will copy resources from the jar file of the current thread
 * and extract it to the destination folder.
 * //from   www .  ja v  a 2s  .co m
 * 
 * @param resourcePath
 * @param destination
 */
public static void copyResourceToDirectory(String resourcePath, File destination) {
    FileHelper.validateArguments(resourcePath, destination);

    try {
        URL resource = FileHelper.class.getClassLoader().getResource(resourcePath);
        JarURLConnection jarURLConnection = (JarURLConnection) resource.openConnection();
        JarFile jarFile = jarURLConnection.getJarFile();

        // Create a new file helper
        FileHelper fileHelper = new FileHelper(jarURLConnection, jarFile);
        fileHelper.copyResource(resourcePath, destination);
    } catch (IOException e) {
        throw new RuntimeException(
                String.format("Failed to copy classpath resource to destination directory %s",
                        destination.getAbsolutePath()),
                e);
    }

}

From source file:org.javaweb.utils.ClassUtils.java

/**
 * ?jarclass/*from  w  w  w. java  2  s. c  om*/
 *
 * @param url
 * @param classList
 * @throws IOException
 */
public static void getAllJarClass(URL url, Set<String> classList) throws IOException {
    if (url != null) {
        JarURLConnection juc = (JarURLConnection) url.openConnection();
        JarFile jf = juc.getJarFile();
        Enumeration<JarEntry> je = jf.entries();

        while (je.hasMoreElements()) {
            JarEntry jar = je.nextElement();

            if (jar.getName().endsWith(".class")) {
                String classPath = jar.getName().replaceAll("\\\\", "/").replaceAll("/+", ".");
                classList.add(classPath.substring(0, classPath.length() - ".class".length()));
            }
        }
    }
}

From source file:net.sourceforge.lept4j.util.LoadLibs.java

/**
 * Copies resources from the jar file of the current thread and extract it
 * to the destination directory./*from   ww w  . j a va2 s .  c o  m*/
 *
 * @param jarConnection
 * @param destDir
 */
static void copyJarResourceToDirectory(JarURLConnection jarConnection, File destDir) {
    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(destDir, filename);

                if (jarEntry.isDirectory()) {
                    currentFile.mkdirs();
                } else {
                    currentFile.deleteOnExit();
                    InputStream is = jarFile.getInputStream(jarEntry);
                    OutputStream out = FileUtils.openOutputStream(currentFile);
                    IOUtils.copy(is, out);
                    is.close();
                    out.close();
                }
            }
        }
    } catch (IOException e) {
        logger.log(Level.WARNING, e.getMessage(), e);
    }
}