zoukankan      html  css  js  c++  java
  • 对数据进行GZIP压缩和解压

    public class GzipUtils {
    
        /**
         * 对字符串进行gzip压缩
         * @param data
         * @return
         * @throws IOException 
         */
        public static String compress(String data) throws IOException {
            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");
        }
        
        /**
         * 对字符串进行解压缩
         * @param data
         * @return
         * @throws Exception 
         */
        public static String unCompress(String data) throws Exception {
            if (null == data && data.length() <= 0) {
                return data;
            }
            //创建一个新的byte数组输出流
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            //创建一个byte数组输入流
            ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes("ISO-8859-1"));
            //创建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,通过解码字节将缓冲区内容转换为字符串
            return out.toString("UTF-8");
        }

    Gzip压缩和解压数据代码

  • 相关阅读:
    bzoj 3196/tyvj p1730 二逼平衡树
    AW201 可见的点 (欧拉函数)
    P3912 素数个数
    P1029 最大公约数和最小公倍数问题
    P1835 素数密度
    P2563 [AHOI2001]质数和分解
    P1075 质因数分解
    AW199 余数之和
    AW198 反素数
    AW197 阶乘分解
  • 原文地址:https://www.cnblogs.com/atomicbomb/p/6647611.html
Copyright © 2011-2022 走看看