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();
        }
    }
  • 相关阅读:
    路由重分布(二)
    linux系统命令的收集 第一部分
    如何在VM软件中安装Linux系统
    Spring boot连接MongoDB集群
    jQuery中防止表单提交两次的方法
    Java中使用HTTP阻塞式调用服务器API
    本地项目初始化git推送到服务器
    前端页面调用Spring boot接口发生的跨域问题
    jQuery中异步问题:数据传递
    Git中修复bug
  • 原文地址:https://www.cnblogs.com/jonban/p/5023028.html
Copyright © 2011-2022 走看看