对数据进行GZIP压缩或解压缩

/**
     * 对data进行GZIP解压缩
     * @param data
     * @return
     * @throws Exception 
     */
    public static String unCompress(byte[] data) throws Exception {
        if (null == data && data.length <= 0) {
            return null;
        }
        String reString = "";
        try {
            //创建一个新的byte数组输出流
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            //创建一个byte数组输入流
            ByteArrayInputStream in = new ByteArrayInputStream(data);
            //创建gzip输入流
            GZIPInputStream gzip = new GZIPInputStream(in);
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = gzip.read(buf)) >= 0) {
                out.write(buf, 0, len);
            }
            // 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串
            reString = out.toString("UTF-8");
            out.close();
            in.close();
            gzip.close();
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return reString;
    }
    
    /**
     * 对字符串进行gzip压缩
     * @param data
     * @return
     * @throws IOException 
     */
    public static String compress(String data) throws Exception {
        if (null == data || data.length() <= 0) {
            return data;
        }
        //创建一个新的byte数组输出流
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        //使用默认缓冲区大小创建新的输出流
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        //将b.length个字节写入此输出流
        gzip.write(data.getBytes());
        gzip.flush();
        gzip.close();
        
        //使用指定的charsetName,通过解码字节将缓冲区内容转换为字符串
        return out.toString("ISO-8859-1");
    }

 

posted on 2019-02-18 10:23  LJD泊水  阅读(2108)  评论(0编辑  收藏  举报