Public final class ziputil{public static string CompressToBase64 (String string) {try {BYTEARRAYOUTP Utstream OS = new Bytearrayoutputstream (String.Length ()); Gzipoutputstream Gos = new Gzipoutputstream (OS); Gos.write (String.getbytes ()); Gos.close (); byte[] Compressed = Os.tobytearray (); Os.close (); String result = base64.encodetostring (compressed, base64.default); return result; } catch (IOException e) {e.printstacktrace (); } catch (Exception ex) {} return ""; } public static string DecompressToBase64 (String texttodecode) {//string Texttodecode = "H4SIAAAAAAAAAPNIZCNJBWC Cidh3bqaaaa==\n "; try {byte[] compressed = Base64.decode (Texttodecode, Base64.default); Final int buffer_size = 32; Bytearrayinputstream InputStream = new Bytearrayinputstream (compressed); Gzipinputstream GIS =New Gzipinputstream (InputStream, buffer_size); Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); byte[] data = new Byte[buffer_size]; int bytesread; while ((Bytesread = gis.read (data))! =-1) {baos.write (data, 0, bytesread); } return Baos.tostring ("UTF-8"); } catch (IOException e) {e.printstacktrace (); } catch (Exception ex) {} return ""; }}
Java Gzip Class-Base64 compression and decompression