code: 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;
      }
docs: 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.
  @param sourceFile The uncompressed file
  @param targetDir The target directory or null if same directory as sourceFile
  @param algorithm The compression algorithm to use
  @param deleteSourceFileAfterCompressed Delete the original source file
  only if the compression was successful.
  @return The compressed file
  @throws FileAlreadyExistsException Thrown if the target file already
  exists and an overwrite is not permitted.  This exception is a
  subclass of IOException, so its safe to only catch an IOException
  if no specific action is required for this case.
  @throws IOException Thrown if an error occurs while attempting to
  compress the source file.