Deflating strings in Coldfusion

Deflating a string is nothing else but compressing it using gzip.
You cannot use the built-in Coldfusion zip function because it doesn’t support gzip compression so you need to fall back to Java. It is less scary than it sounds.
The function below comes from a SAML flow taking an XML object, compressing it and returning it as a binary:

 
    private Binary function _deflate(required String deflatable){
 
        var emptyByteArray = createObject("java", "java.io.ByteArrayOutputStream").init().toByteArray();
        var byteClass = emptyByteArray.getClass().getComponentType();
        var deflated = createObject("java","java.lang.reflect.Array").newInstance(byteClass, 500);
 
        var deflater = createObject("java", "java.util.zip.Deflater");
            deflater.init(9,true);
            deflater.setInput(deflatable.getBytes("UTF-8"));
            deflater.finish();
            deflater.deflate(deflated);
 
        return deflated;
    }

If your subject is a SAMLAuthnRequest you need to convert the binary to base64 the result.
Please note that ADFS requires you to urlencode the string as it hates the “=” paddings:

    return urlencodedformat(toBase64(xmlAuthnRequestDeflated,"UTF-8"));

This example works with Coldfusion 11/Java 8.