src/main/java/org/jboss/util/file/JarUtils.java
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();
   }
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();
   }
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 ( ) ; }
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.

@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
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 .