zoukankan      html  css  js  c++  java
  • Java压缩字符串工具类

    StringCompressUtils.java

    package javax.utils;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.util.zip.GZIPInputStream;
    import java.util.zip.GZIPOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    import java.util.zip.ZipOutputStream;
    
    import org.apache.commons.codec.binary.Base64;
    
    /**
     * Java 字符串压缩工具
     * 
     * @author Logan
     * @version 1.0.0
     *
     */
    public class StringCompressUtils {
    
        /**
         * 使用gzip进行压缩
         * 
         * @param str 压缩前的文本
         * @return 返回压缩后的文本
         * @throws IOException 有异常时抛出,由调用者捕获处理
         */
        public static String gzip(String str) throws IOException {
            if (str == null || str.isEmpty()) {
                return str;
            }
    
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            try (
                    GZIPOutputStream gzip = new GZIPOutputStream(out);
            ) {
    
                gzip.write(str.getBytes());
    
            }
    
            return Base64.encodeBase64String(out.toByteArray());
        }
    
        /**
         * 使用gzip进行解压缩
         * 
         * @param compressedStr 压缩字符串
         * @return 解压字符串
         * @throws IOException 有异常时抛出,由调用者捕获处理
         */
        public static String gunzip(String compressedStr) throws IOException {
            if (compressedStr == null || compressedStr.isEmpty()) {
                return compressedStr;
            }
    
            byte[] compressed = Base64.decodeBase64(compressedStr);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayInputStream in = new ByteArrayInputStream(compressed);
    
            try (
                    GZIPInputStream ginzip = new GZIPInputStream(in);
            ) {
    
                byte[] buffer = new byte[4096];
                int len = -1;
                while ((len = ginzip.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
            }
            return out.toString();
        }
    
        /**
         * 使用zip进行压缩
         * 
         * @param str 压缩前的文本
         * @return 返回压缩后的文本
         * @throws IOException 有异常时抛出,由调用者捕获处理
         */
        public static String zip(String str) throws IOException {
            if (null == str || str.isEmpty()) {
                return str;
            }
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            try (
                    ZipOutputStream zout = new ZipOutputStream(out);
            ) {
                zout.putNextEntry(new ZipEntry("0"));
                zout.write(str.getBytes());
                zout.closeEntry();
            }
            return Base64.encodeBase64String(out.toByteArray());
        }
    
        /**
         * 使用zip进行解压缩
         * 
         * @param compressed 压缩后的文本
         * @return 解压后的字符串
         * @throws IOException 有异常时抛出,由调用者捕获处理
         */
        public static final String unzip(String compressedStr) throws IOException {
            if (null == compressedStr || compressedStr.isEmpty()) {
                return compressedStr;
            }
    
            byte[] compressed = Base64.decodeBase64(compressedStr);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayInputStream in = new ByteArrayInputStream(compressed);
    
            try (
                    ZipInputStream zin = new ZipInputStream(in);
            ) {
                zin.getNextEntry();
                byte[] buffer = new byte[4096];
                int len = -1;
                while ((len = zin.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
            }
            return out.toString();
        }
    }
  • 相关阅读:
    php jquery pjax示例源码 (ajax请求,并改变url)
    mysql 中查看指定表的字段名 (可根据字段变量生成c#后台代码)
    原生js Ajax
    ajax basic 认证
    Json序列化问题
    MSSQL 日期操作函数 总结
    用sql语句按周、按月、按季、按年统
    mssql中得到当天数据的语句
    SP_CreateInsertScript 将表内的数据全部拼接成INSERT字符串输出
    MSSQL 获取指定日期所在星期的第一天和最后一天日期 获取指定日期坐在月的第一天和最后一天
  • 原文地址:https://www.cnblogs.com/jonban/p/5023028.html
Copyright © 2011-2022 走看看