code: public static byte[] compressByGzip(byte[] sourceBytes) {
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          try {
              GZIPOutputStream gzip = null;
              try {
                  gzip = new GZIPOutputStream(out);
                  gzip.write(sourceBytes);
                  gzip.close();
              } catch (IOException ex) {
                  return null;
              } finally {
                  try {
                      if (gzip != null) {
                          gzip.close();
                      }
                  } catch (IOException ex) {
                  }
              }
  
              return out.toByteArray();
          } finally {
              try {
                  out.close();
              } catch (IOException ex) {
              }
          }
      }
docs: to gzip