本帖最后由 vaintwyt 于 2014-04-18 12:13:14 编辑

解决方案 »

  1.   


    你好,我根据下面网址使用Deflater压缩,Inflater解压。没有出现问题。不过,原本1046长度的字符串,压缩后变成1144长。而我用gzip的话,可以压缩到16。http://navylee.iteye.com/blog/1661733
      

  2.   

    隔了一段时间,再看看。终于解决了问题。原来是获得输出流的数据之前,需要将gzip关闭。另外,我上面说的压缩到16,是因为之前出现异常,所以才那么小。其实应该是8百多。结帖啦
        public static String GetCompress(String src)
        {
            if (src == null || src.isEmpty()) {
                            return src;
            }
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = null;
            String des = null;
            try {
                gzip = new GZIPOutputStream(out);
                gzip.write(src.getBytes());
                            //由于压缩后的数据需要传输,所以用了BASE64编码
                //des = new BASE64Encoder().encode(out.toByteArray()); //原先在这个获得压缩数据
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(gzip!=null)
                {
                    try {
                        gzip.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return new BASE64Encoder().encode(out.toByteArray());//最终改成这里获得压缩数据
        }