Example usage for java.util.jar JarFile getName

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the path name of the ZIP file.

Usage

From source file:org.wso2.carbon.automation.test.utils.common.FileManager.java

public static void copyJarFile(File sourceFile, String destinationDirectory) throws IOException {
    File destinationFileDirectory = new File(destinationDirectory);
    JarFile jarFile = new JarFile(sourceFile);
    String fileName = jarFile.getName();
    String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
    File destinationFile = new File(destinationFileDirectory, fileNameLastPart);
    JarOutputStream jarOutputStream = null;
    try {/*from ww  w  .  j av a2  s . c  o  m*/
        jarOutputStream = new JarOutputStream(new FileOutputStream(destinationFile));
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            InputStream inputStream = jarFile.getInputStream(jarEntry);
            //jarOutputStream.putNextEntry(jarEntry);
            //create a new jarEntry to avoid ZipException: invalid jarEntry compressed size
            jarOutputStream.putNextEntry(new JarEntry(jarEntry.getName()));
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                jarOutputStream.write(buffer, 0, bytesRead);
            }
            inputStream.close();
            jarOutputStream.flush();
            jarOutputStream.closeEntry();
        }
    } finally {
        if (jarOutputStream != null) {
            try {
                jarOutputStream.close();
            } catch (IOException e) {
            }
        }
    }
}

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./* ww  w .  j  a v a 2  s .co  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;
}

From source file:net.technicpack.launchercore.util.ZipUtils.java

public static void copyMinecraftJar(File minecraft, File output) throws IOException {
    String[] security = { "MOJANG_C.DSA", "MOJANG_C.SF", "CODESIGN.RSA", "CODESIGN.SF" };
    JarFile jarFile = new JarFile(minecraft);
    try {/*from   w ww . ja v a  2 s.  com*/
        String fileName = jarFile.getName();
        String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));

        JarOutputStream jos = new JarOutputStream(new FileOutputStream(output));
        Enumeration<JarEntry> entries = jarFile.entries();

        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if (containsAny(entry.getName(), security)) {
                continue;
            }
            InputStream is = jarFile.getInputStream(entry);

            //jos.putNextEntry(entry);
            //create a new entry to avoid ZipException: invalid entry compressed size
            jos.putNextEntry(new JarEntry(entry.getName()));
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = is.read(buffer)) != -1) {
                jos.write(buffer, 0, bytesRead);
            }
            is.close();
            jos.flush();
            jos.closeEntry();
        }
        jos.close();
    } finally {
        jarFile.close();
    }

}

From source file:org.mrgeo.utils.ClassLoaderUtil.java

public static Collection<String> getMostJars() {

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try {/*from   w w w  . j a  v a  2  s  .c  o m*/
        // this seems to populate more jars. Odd.
        getChildResources("META-INF/services");
        getChildResources("");
        getChildResources("/");

    } catch (Exception e1) {
        e1.printStackTrace();
    }
    Thief t = new Thief(classLoader);
    Package[] packages = t.getPackages();
    TreeSet<String> result = new TreeSet<String>();

    for (Package p : packages) {
        Enumeration<URL> urls;
        try {
            String path = p.getName().replace(".", "/");

            urls = classLoader.getResources(path);
            while (urls.hasMoreElements()) {
                URL resource = urls.nextElement();
                if (resource.getProtocol().equalsIgnoreCase("jar")) {
                    JarURLConnection conn = (JarURLConnection) resource.openConnection();
                    JarFile jarFile = conn.getJarFile();
                    result.add(jarFile.getName());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return result;
}

From source file:org.wso2.esb.integration.common.utils.common.FileManager.java

public static void copyJarFile(File sourceFile, String destinationDirectory) throws IOException {
    File destinationFileDirectory = new File(destinationDirectory);
    JarFile jarFile = new JarFile(sourceFile);
    String fileName = jarFile.getName();
    String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
    File destinationFile = new File(destinationFileDirectory, fileNameLastPart);
    JarOutputStream jarOutputStream = null;
    try {//  w  ww.  ja  v a2 s.  c  o m
        jarOutputStream = new JarOutputStream(new FileOutputStream(destinationFile));
        Enumeration<JarEntry> entries = jarFile.entries();

        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            InputStream inputStream = jarFile.getInputStream(jarEntry);

            //jarOutputStream.putNextEntry(jarEntry);
            //create a new jarEntry to avoid ZipException: invalid jarEntry compressed size
            jarOutputStream.putNextEntry(new JarEntry(jarEntry.getName()));
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                jarOutputStream.write(buffer, 0, bytesRead);
            }
            inputStream.close();
            jarOutputStream.flush();
            jarOutputStream.closeEntry();
        }
    } finally {
        if (jarOutputStream != null) {
            try {
                jarOutputStream.close();
            } catch (IOException e) {

            }
        }
    }

}

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

/**
 * ??jar????// ww w .j  av  a  2  s  .c  o  m
 * 
 * @param jarFile Jar
 * @param handler ?
 * @throws TraversalHandlerException ???????
 * @throws IllegalArgumentException ?{@code null}???
 */
public static void forEach(JarFile jarFile, ClassHandler handler) throws TraversalHandlerException {
    Validate.notNull(jarFile);
    Validate.notNull(handler);
    boolean hasWarExtension = jarFile.getName().endsWith(WAR_FILE_EXTENSION);

    Enumeration<JarEntry> enumeration = jarFile.entries();
    while (enumeration.hasMoreElements()) {
        JarEntry entry = enumeration.nextElement();
        String entryName = entry.getName().replace('\\', '/');
        if (entryName.endsWith(CLASS_EXTENSION)) {
            int startPos = hasWarExtension && entryName.startsWith(WEB_INF_CLASSES_PATH)
                    ? WEB_INF_CLASSES_PATH.length()
                    : 0;
            String className = entryName.substring(startPos, entryName.length() - CLASS_EXTENSION.length())
                    .replace('/', '.');
            int pos = className.lastIndexOf('.');
            String packageName = (pos == -1) ? null : className.substring(0, pos);
            String shortClassName = (pos == -1) ? className : className.substring(pos + 1);
            handler.processClass(packageName, shortClassName);
        }
    }
}

From source file:org.wso2.carbon.integration.common.utils.FileManager.java

public static void copyJarFile(File sourceFile, String destinationDirectory) throws IOException {
    File destinationFileDirectory = new File(destinationDirectory);
    JarFile jarFile = new JarFile(sourceFile);
    String fileName = jarFile.getName();
    String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
    File destinationFile = new File(destinationFileDirectory, fileNameLastPart);
    JarOutputStream jarOutputStream = null;
    try {//from w  ww  . j  ava 2  s .co  m
        jarOutputStream = new JarOutputStream(new FileOutputStream(destinationFile));
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            InputStream inputStream = jarFile.getInputStream(jarEntry);
            //jarOutputStream.putNextEntry(jarEntry);
            //create a new jarEntry to avoid ZipException: invalid jarEntry compressed size
            jarOutputStream.putNextEntry(new JarEntry(jarEntry.getName()));
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                jarOutputStream.write(buffer, 0, bytesRead);
            }
            inputStream.close();
            jarOutputStream.flush();
            jarOutputStream.closeEntry();
        }
    } finally {
        if (jarOutputStream != null) {
            try {
                jarOutputStream.close();
            } catch (IOException e) {
                //ignore
            }
        }
    }
}

From source file:com.web.server.util.ClassLoaderUtil.java

public static CopyOnWriteArrayList closeClassLoader(ClassLoader cl) {
    CopyOnWriteArrayList jars = new CopyOnWriteArrayList();
    boolean res = false;
    Class classURLClassLoader = null;
    if (cl instanceof URLClassLoader)
        classURLClassLoader = URLClassLoader.class;
    else if (cl instanceof VFSClassLoader) {
        classURLClassLoader = VFSClassLoader.class;
    }/*w  w  w . j a v a2 s  .  co  m*/
    Field f = null;
    try {
        f = classURLClassLoader.getDeclaredField("ucp");
        System.out.println(f);
    } catch (NoSuchFieldException e1) {
        // e1.printStackTrace();
        // log.info(e1.getMessage(), e1);

    }
    if (f != null) {
        f.setAccessible(true);
        Object obj = null;
        try {
            obj = f.get(cl);
        } catch (IllegalAccessException e1) {
            // e1.printStackTrace();
            // log.info(e1.getMessage(), e1);
        }
        if (obj != null) {
            final Object ucp = obj;
            f = null;
            try {
                f = ucp.getClass().getDeclaredField("loaders");
                System.out.println(f);
            } catch (NoSuchFieldException e1) {
                // e1.printStackTrace();
                // log.info(e1.getMessage(), e1);
            }
            if (f != null) {
                f.setAccessible(true);
                ArrayList loaders = null;
                try {
                    loaders = (ArrayList) f.get(ucp);
                    res = true;
                } catch (IllegalAccessException e1) {
                    // e1.printStackTrace();
                }
                for (int i = 0; loaders != null && i < loaders.size(); i++) {
                    obj = loaders.get(i);
                    f = null;
                    try {
                        f = obj.getClass().getDeclaredField("jar");
                        // log.info(f);
                    } catch (NoSuchFieldException e) {
                        // e.printStackTrace();
                        // log.info(e.getMessage(), e);
                    }
                    if (f != null) {
                        f.setAccessible(true);
                        try {
                            obj = f.get(obj);
                        } catch (IllegalAccessException e1) {
                            // e1.printStackTrace();
                            // log.info(e1.getMessage(), e1);
                        }
                        if (obj instanceof JarFile) {
                            final JarFile jarFile = (JarFile) obj;
                            System.out.println(jarFile.getName());
                            jars.add(jarFile.getName().replace("/", "\\").trim().toUpperCase());
                            // try {
                            // jarFile.getManifest().clear();
                            // } catch (IOException e) {
                            // // ignore
                            // }
                            try {
                                jarFile.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                                // ignore
                                // log.info(e.getMessage(), e);
                            }
                        }
                    }
                }
            }
        }
    }
    return jars;
}

From source file:com.app.server.util.ClassLoaderUtil.java

public static boolean cleanupJarFileFactory(CopyOnWriteArrayList setJarFileNames2Close) {
    boolean res = false;
    Class classJarURLConnection = null;
    try {/* w  w w  .jav a2  s  .c om*/
        classJarURLConnection = Class.forName("sun.net.www.protocol.jar.JarURLConnection");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    if (classJarURLConnection == null) {
        return res;
    }
    Field f = null;
    try {
        f = classJarURLConnection.getDeclaredField("factory");
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    if (f == null) {
        return res;
    }
    f.setAccessible(true);
    Object obj = null;
    try {
        obj = f.get(null);
    } catch (IllegalAccessException e) {
        // ignore
    }
    if (obj == null) {
        return res;
    }
    Class classJarFileFactory = obj.getClass();
    //
    HashMap fileCache = null;
    try {
        f = classJarFileFactory.getDeclaredField("fileCache");
        f.setAccessible(true);
        obj = f.get(null);
        if (obj instanceof HashMap) {
            fileCache = (HashMap) obj;
        }
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    HashMap urlCache = null;
    try {
        f = classJarFileFactory.getDeclaredField("urlCache");
        f.setAccessible(true);
        obj = f.get(null);
        if (obj instanceof HashMap) {
            urlCache = (HashMap) obj;
        }
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    if (urlCache != null) {
        HashMap urlCacheTmp = (HashMap) urlCache.clone();
        Iterator it = urlCacheTmp.keySet().iterator();
        while (it.hasNext()) {
            obj = it.next();
            if (!(obj instanceof JarFile)) {
                continue;
            }
            JarFile jarFile = (JarFile) obj;
            if (setJarFileNames2Close.contains(jarFile.getName().trim().toUpperCase())) {
                try {
                    jarFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (fileCache != null) {
                    fileCache.remove(jarFile);
                }
                urlCache.remove(jarFile);
            }
        }
        res = true;
    } else if (fileCache != null) {
        // urlCache := null
        HashMap fileCacheTmp = (HashMap) fileCache.clone();
        Iterator it = fileCacheTmp.keySet().iterator();
        while (it.hasNext()) {
            Object key = it.next();
            obj = fileCache.get(key);
            if (!(obj instanceof JarFile)) {
                continue;
            }
            JarFile jarFile = (JarFile) obj;

            try {
                jarFile.close();
            } catch (IOException e) {
                // ignore
            }
            fileCache.remove(key);

        }
        res = true;
    }
    setJarFileNames2Close.clear();
    return res;
}

From source file:ee.ioc.cs.vsle.ccl.PackageClassLoader.java

/**
 * Helper method for getWebStartClasspath() method.
 * Returns a local URL of a jar for the classpath.
 * If jar is a system lib, it is ignored.
 * @param url//from  w ww .j  a v  a 2  s  .  c o m
 * @param systemLibs
 * @return
 */
private static URL getLocalJarURL(URL url, Collection<String> systemLibs) {

    String urlStrJar = getJarPath(url);

    //ignore if it is a system lib
    if (systemLibs.contains(urlStrJar))
        return null;

    InputStream inputStreamJar = null;
    File tempJar;
    try {
        //check if the file is already local
        if (url.getPath().startsWith("file:"))
            return new File(urlStrJar).toURI().toURL();

        JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile();
        inputStreamJar = new FileInputStream(jar.getName());

        String strippedName = urlStrJar;
        int dotIndex = strippedName.lastIndexOf('.');
        if (dotIndex >= 0) {
            strippedName = strippedName.substring(0, dotIndex);
            strippedName = strippedName.replace("/", File.separator);
            strippedName = strippedName.replace("\\", File.separator);
            int slashIndex = strippedName.lastIndexOf(File.separator);
            if (slashIndex >= 0) {
                strippedName = strippedName.substring(slashIndex + 1);
            }
        }
        tempJar = File.createTempFile(strippedName, ".jar");
        tempJar.deleteOnExit();
        FileUtils.copyInputStreamToFile(inputStreamJar, tempJar);
        return tempJar.toURI().toURL();
    } catch (Exception ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (inputStreamJar != null) {
                inputStreamJar.close();
            }
        } catch (IOException ioe) {
        }
    }
    return null;
}