public static File compress ( File sourceFile , File targetDir , String algorithm , boolean deleteSourceFileAfterCompressed ) throws FileAlreadyExistsException , IOException { // make sure the destination directory is a directory if ( ! targetDir . isDirectory ( ) ) { throw new IOException ( "Cannot compress file since target directory " + targetDir + " neither exists or is a directory" ) ; } // try to find compression algorithm Algorithm a = Algorithm . findByName ( algorithm ) ; // was it found? if ( a == null ) { throw new IOException ( "Compression algorithm '" + algorithm + "' is not supported" ) ; } // create a target file with the default file extension for this algorithm File targetFile = new File ( targetDir , sourceFile . getName ( ) + "." + a . getFileExtension ( ) ) ; compress ( a , sourceFile , targetFile , deleteSourceFileAfterCompressed ) ; return targetFile ; }
Compresses the source file using a variety of supported compression algorithms . This method will create a destination file in the destination directory and will append a file extension matching the compression algorithm used . For example using gzip will mean a source file of app . log would be compressed to app . log . gz and placed in the destination directory .