Using Java Deflater/Inflater for Compression and Decompression with Base64 Encoding
This article demonstrates how to use Java's Deflater and DeflaterOutputStream classes to compress data into Base64 strings, how to decompress them with InflaterOutputStream, and includes Spock test cases that compare compressed and original lengths for various inputs.
Java provides a compression library with the Deflater class for general compression. It also offers DeflaterOutputStream , which uses Deflater to compress a data stream and write the compressed bytes to another output stream. Corresponding Inflater and InflaterOutputStream classes handle decompression.
Compression
Below is an example of using DeflaterOutputStream to compress a byte array and return a Base64‑encoded string.
/**
* Compress a string, default UTF‑8
*
* @param text
* @return
*/
public static String zipBase64(String text) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
try (DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out)) {
deflaterOutputStream.write(text.getBytes(Constant.UTF_8));
}
return new String(DecodeEncode.base64Encode(out.toByteArray()));
} catch (IOException e) {
logger.error("压缩文本失败:{}", text, e);
}
return EMPTY;
}Test the compression method:
public static void main(String[] args) throws IOException {
String text = DecodeEncode.zipBase64("5615616119688refdaf888888888888888865555555555555511111111111111111111111119999999999999999999999999999999911111111111111111111333333333333333333");
output(text);
String s = DecodeEncode.unzipBase64(text);
output(s);
output(text.length() + TAB + s.length());
}Console output:
INFO-> eJwzNTM0NTM0MzS0NLOwKEpNS0lMs0ADZqYowBAXsCQAsOkxxgAALV0fBw==
INFO-> 5615616119688refdaf888888888888888865555555555555511111111111111111111111119999999999999999999999999999999911111111111111111111333333333333333333
INFO-> 60 145Decompression
/**
* Decompress a string, default UTF‑8
*
* @param text
* @return
*/
public static String unzipBase64(String text) {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
try (OutputStream outputStream = new InflaterOutputStream(os)) {
outputStream.write(DecodeEncode.base64Byte(text));
}
return new String(os.toByteArray(), Constant.UTF_8);
} catch (IOException e) {
logger.error("解压文本失败:{}", text, e);
}
return EMPTY;
}Test the decompression method:
public static void main(String[] args) throws IOException {
String text = "eJwzNTM0NTM0MzS0NLOwKEpNS0lMs0ADZqYowBAXsCQAsOkxxgAALV0fBw==";
String s = DecodeEncode.unzipBase64(text);
output(s);
output(text.length() + TAB + s.length());
}Console output:
INFO-> 当前用户:fv,IP:10.60.192.21,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
INFO-> 5615616119688refdaf888888888888888865555555555555511111111111111111111111119999999999999999999999999999999911111111111111111111333333333333333333
INFO-> 60 145Test Cases
The Spock framework is used to verify that the compressed string is shorter than the original for a variety of inputs.
def "测试加密解密"() {
expect:
name.length() > DecodeEncode.zipBase64(name).length()
where:
name << [
"00000000000000000000000000000000000000000000000000000",
"51666666666666666666666666666666666666666666666666666",
"(&%^&%*&%(^(^(*&^*(&^(*&^(*^(*&%^%$^%##@#!#@!~~#@",
"发大房东放大反动发动机吧就产国产过高冬季佛冬季风戳分床三佛",
"gkjdgjdgjlfdjgldkgjfdsafoiwehoirehtoiewho"
]
}Test results show that for some strings (especially those containing many digits) the compression is effective, while for others the compressed size can be larger than the original.
Overall, the article provides a practical guide for Java developers to perform in‑memory compression and decompression, demonstrates how to encode the result in Base64, and shows how to validate the behavior with automated Spock tests.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.