Recently doing Android development, socket to pass message information, message information is JSON format, the data is very high repeatability so we found a method of compressing the string, the larger the data compression more obvious.
Data transfer, sometimes need to compress and decompress, this example uses Gzipoutputstream/gzipinputstream implementation.
1, using iso-8859-1 as the intermediary code, can guarantee accurate restore data
2, when the character encoding is determined, you can explicitly specify the encoding in the last sentence of the Uncompress method import java.io.bytearrayinputstream; import java.io.bytearrayoutputstream; import java.io.ioexception; import java.util.zip.gzipinputstream; import java.util.zip.gzipoutputstream; public class ziputil { // compression public static string compress (String  STR) throws ioexception { if (str == null | | str.length () == 0) { return str; } bytearrayoutputstream out = new bytearrayoutputstream (); &nbSp GZIPOutputStream gzip = new Gzipoutputstream (out); gzip.write (Str.getbytes ()); gzip.close (); return out.tostring ("iso-8859-1");  } / / Decompression public static string uncompress (STRING STR) throws ioexception { if (str == null | | str.length () == 0) { return str; }     &NBsp; bytearrayoutputstream out = new bytearrayoutputstream (); ByteArrayInputStream in = new Bytearrayinputstream (str