public Jar addPackageOf ( Class < ? > clazz , Filter filter ) throws IOException { try { final String path = clazz . getPackage ( ) . getName ( ) . replace ( ' ' , ' ' ) ; URL dirURL = clazz . getClassLoader ( ) . getResource ( path ) ; if ( dirURL != null && dirURL . getProtocol ( ) . equals ( "file" ) ) addDir ( Paths . get ( path ) , Paths . get ( dirURL . toURI ( ) ) , filter , false ) ; else { if ( dirURL == null ) // In case of a jar file, we can't actually find a directory. dirURL = clazz . getClassLoader ( ) . getResource ( clazz . getName ( ) . replace ( ' ' , ' ' ) + ".class" ) ; if ( dirURL . getProtocol ( ) . equals ( "jar" ) ) { final URI jarUri = new URI ( dirURL . getPath ( ) . substring ( 0 , dirURL . getPath ( ) . indexOf ( ' ' ) ) ) ; try ( JarInputStream jis1 = newJarInputStream ( Files . newInputStream ( Paths . get ( jarUri ) ) ) ) { for ( JarEntry entry ; ( entry = jis1 . getNextJarEntry ( ) ) != null ; ) { try { if ( entry . getName ( ) . startsWith ( path + ' ' ) ) { if ( filter == null || filter . filter ( entry . getName ( ) ) ) addEntryNoClose ( jos , entry . getName ( ) , jis1 ) ; } } catch ( ZipException e ) { if ( ! e . getMessage ( ) . startsWith ( "duplicate entry" ) ) throw e ; } } } } else throw new AssertionError ( ) ; } return this ; } catch ( URISyntaxException e ) { throw new AssertionError ( e ) ; } }
Adds the contents of a Java package to this JAR .