public static int extractFromClasspathToFile ( String packagePath , File toDir ) throws IOException { String locationPattern = "classpath*:" + packagePath + "/**" ; ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver ( ) ; Resource [ ] resources = resourcePatternResolver . getResources ( locationPattern ) ; if ( resources . length == 0 ) { throw new IOException ( "Nothing found at " + locationPattern ) ; } int counter = 0 ; for ( Resource resource : resources ) { if ( resource . isReadable ( ) ) { // Skip hidden or system files final URL url = resource . getURL ( ) ; String path = url . toString ( ) ; if ( ! path . endsWith ( "/" ) ) { // Skip directories int p = path . lastIndexOf ( packagePath ) + packagePath . length ( ) ; path = path . substring ( p ) ; final File targetFile = new File ( toDir , path ) ; long len = resource . contentLength ( ) ; if ( ! targetFile . exists ( ) || targetFile . length ( ) != len ) { // Only copy new files tryN ( 5 , 500 , new Procedure < IOException > ( ) { @ Override public void apply ( ) throws IOException { FileUtils . copyURLToFile ( url , targetFile ) ; } } ) ; counter ++ ; } } } } if ( counter > 0 ) { Object [ ] info = new Object [ ] { counter , locationPattern , toDir } ; logger . info ( "Unpacked {} files from {} to {}" , info ) ; } return counter ; }
Extract files from a package on the classpath into a directory .