private String copyFileToDisk ( InputStream inputStream , String pathToExtractTo , String filename ) throws IOException { if ( ! overwriteFilesThatExist ) { File [ ] existingFiles = new File ( pathToExtractTo ) . listFiles ( ) ; if ( null != existingFiles && existingFiles . length > 0 ) { for ( File existingFile : existingFiles ) { String existingFilename = existingFile . getName ( ) ; if ( existingFilename . equals ( filename ) ) { LOG . info ( "Binary '" + existingFilename + "' exists: true" ) ; LOG . info ( "Using existing '" + existingFilename + " 'binary." ) ; return existingFile . getAbsolutePath ( ) ; } } } } File outputFile = new File ( pathToExtractTo , filename ) ; try { if ( ! outputFile . exists ( ) && ! outputFile . getParentFile ( ) . mkdirs ( ) && ! outputFile . createNewFile ( ) ) { throw new IOException ( "Unable to create " + outputFile . getAbsolutePath ( ) ) ; } LOG . info ( "Extracting binary '" + filename + "'..." ) ; FileUtils . copyInputStreamToFile ( inputStream , outputFile ) ; LOG . info ( "Binary copied to " + outputFile . getAbsolutePath ( ) ) ; if ( ! outputFile . setExecutable ( true ) && ! outputFile . canExecute ( ) ) { LOG . warn ( "Unable to set executable flag (+x) on " + filename ) ; } } finally { inputStream . close ( ) ; } return outputFile . getAbsolutePath ( ) ; }
Copy a file from an inputsteam to disk