zoukankan      html  css  js  c++  java
  • 对数据进行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");
        }
  • 相关阅读:
    CodeForces
    CodeForces
    springboot 入门七-静态资源处理
    springboot 入门六-多环境日志配置
    springboot 入门五-日志一
    springboot 入门四-时间类型处理
    springboot 入门三- 读取配置信息二(读取属性文件方式)
    springboot 入门二- 读取配置信息一
    springboot 入门一 hello world!
    SVN提交小结(转)
  • 原文地址:https://www.cnblogs.com/lijianda/p/10394114.html
Copyright © 2011-2022 走看看