Example usage for java.net JarURLConnection getInputStream

List of usage examples for java.net JarURLConnection getInputStream

Introduction

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

Prototype

public InputStream getInputStream() throws IOException 

Source Link

Document

Returns an input stream that reads from this open connection.

Usage

From source file:JarUtils.java

/** Given a URL check if its a jar url(jar:<url>!/archive) and if it is,
 extract the archive entry into the given dest directory and return a file
 URL to its location. If jarURL is not a jar url then it is simply returned
 as the URL for the jar./*  w  w w . j ava 2  s  . c o m*/
        
 @param jarURL the URL to validate and extract the referenced entry if its
   a jar protocol URL
 @param dest the directory into which the nested jar will be extracted.
 @return the file: URL for the jar referenced by the jarURL parameter.
 * @throws IOException 
 */
public static URL extractNestedJar(URL jarURL, File dest) throws IOException {
    // This may not be a jar URL so validate the protocol 
    if (jarURL.getProtocol().equals("jar") == false)
        return jarURL;

    String destPath = dest.getAbsolutePath();
    URLConnection urlConn = jarURL.openConnection();
    JarURLConnection jarConn = (JarURLConnection) urlConn;
    // Extract the archive to dest/jarName-contents/archive
    String parentArchiveName = jarConn.getJarFile().getName();
    // Find the longest common prefix between destPath and parentArchiveName
    int length = Math.min(destPath.length(), parentArchiveName.length());
    int n = 0;
    while (n < length) {
        char a = destPath.charAt(n);
        char b = parentArchiveName.charAt(n);
        if (a != b)
            break;
        n++;
    }
    // Remove any common prefix from parentArchiveName
    parentArchiveName = parentArchiveName.substring(n);

    File archiveDir = new File(dest, parentArchiveName + "-contents");
    if (archiveDir.exists() == false && archiveDir.mkdirs() == false)
        throw new IOException(
                "Failed to create contents directory for archive, path=" + archiveDir.getAbsolutePath());
    String archiveName = jarConn.getEntryName();
    File archiveFile = new File(archiveDir, archiveName);
    File archiveParentDir = archiveFile.getParentFile();
    if (archiveParentDir.exists() == false && archiveParentDir.mkdirs() == false)
        throw new IOException(
                "Failed to create parent directory for archive, path=" + archiveParentDir.getAbsolutePath());
    InputStream archiveIS = jarConn.getInputStream();
    FileOutputStream fos = new FileOutputStream(archiveFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    byte[] buffer = new byte[4096];
    int read;
    while ((read = archiveIS.read(buffer)) > 0) {
        bos.write(buffer, 0, read);
    }
    archiveIS.close();
    bos.close();

    // Return the file url to the extracted jar
    return archiveFile.toURL();
}

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

/**
 * jar???/*from   ww w  .j  a  v  a2  s.  c  o m*/
 *
 * @param url
 * @return
 * @throws IOException
 */
public static InputStream getResourceFromJarFile(URL url) throws IOException {
    URLConnection urlConnection = url.openConnection();

    if (urlConnection instanceof JarURLConnection) {
        JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
        return jarConnection.getInputStream();
    } else {
        throw new IOException("URL ??JarURLConnection.");
    }
}