Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; public class Main { public static byte[] compress(String str) { if (str == null || str.length() == 0) { return str.getBytes(); } final ByteArrayOutputStream out = new ByteArrayOutputStream(); try { final GZIPOutputStream gzip = new GZIPOutputStream(out); gzip.write(str.getBytes()); gzip.close(); } catch (final IOException e) { e.printStackTrace(); } return out.toByteArray(); } }