@ SneakyThrows public static String compress ( final String srcTxt ) { try ( val rstBao = new ByteArrayOutputStream ( ) ; val zos = new GZIPOutputStream ( rstBao ) ) { zos . write ( srcTxt . getBytes ( StandardCharsets . UTF_8 ) ) ; zos . flush ( ) ; zos . finish ( ) ; val bytes = rstBao . toByteArray ( ) ; val base64 = StringUtils . remove ( EncodingUtils . encodeBase64 ( bytes ) , ' ' ) ; return new String ( StandardCharsets . UTF_8 . encode ( base64 ) . array ( ) , StandardCharsets . UTF_8 ) ; } }
Use ZipOutputStream to zip text to byte array then convert byte array to base64 string so it can be transferred via http request .