BASE64压缩与解压

 /**
     * 字符串的压缩
     *
     * @param base64 待压缩的字符串
     *
     * @return 返回压缩后的字符串
     *
     * @throws IOException
     */
    public static String zipBase64(String base64) throws IOException {
        if (null == base64 || base64.length() <= 0) {
            return base64;
        }
        // 创建一个新的 byte 数组输出流
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        // 使用默认缓冲区大小创建新的输出流
        GZIPOutputStream gzip = new GZIPOutputStream( out );
        // 将 b.length 个字节写入此输出流
        gzip.write( base64.getBytes() );
        gzip.close();
        // 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串
        return out.toString( "ISO-8859-1" );
    }

    /**
     * 字符串的解压
     *
     * @param zipBase64 需要解压的字符串
     *
     * @return 返回解压缩后的字符串
     *
     * @throws IOException
     */
    public static String upZip(String zipBase64) throws IOException {
        if (null == zipBase64 || zipBase64.length() <= 0) {
            return zipBase64;
        }
        // 创建一个新的 byte 数组输出流
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        // 创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组
        ByteArrayInputStream in = new ByteArrayInputStream( zipBase64 .getBytes( "ISO-8859-1" ) );

        // 使用默认缓冲区大小创建新的输入流
        GZIPInputStream gzip = new GZIPInputStream( in );

        byte[] buffer = new byte[256];
        int n = 0;
        while ((n = gzip.read( buffer )) >= 0) {// 将未压缩数据读入字节数组
            // 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此 byte数组输出流
            out.write( buffer, 0, n );
        }
        // 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串
        return out.toString( "ISO-8859-1" );
    }

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北漂的菜小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值