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();
}
}

Java压缩字符串工具类的更多相关文章

  1. 【Java】字符串工具类

    import android.annotation.SuppressLint; import java.io.UnsupportedEncodingException; import java.uti ...

  2. [JAVA][StringUtils]字符串工具类的常用方

    StringUtils 方法的操作对象是 java.lang.String 类型的对象,是 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String 为 ...

  3. Java关于字符串工具类~持续汇总~

    /** * 01 * 描述:String的substring和replace方法使用 * [时间 2019年3月5日下午3:22:08 作者 陶攀峰] */ public static void te ...

  4. StringUtils 字符串工具类

    package com.thinkgem.jeesite.common.utils; import java.io.File; import java.io.IOException; import j ...

  5. java格式处理工具类

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  6. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  7. JAVA 8 日期工具类

    JAVA 8 日期工具类 主题描述 JAVA中日期时间的历史 代码成果 主题描述 JAVA的日期时间一直比较混乱,本来以为joda会是巅峰,但是JAVA 8改变了我的思想.但是即便在JAVA 8面前, ...

  8. JavaSE-基础语法(二)-系统类(java.lang.*)和工具类(java.util.*)

    系统类(java.lang.*)和工具类(java.util.*) 一.系统类(java.lang.*) 这个包下包含java语言的核心类,如String.Math.System和Thread类等,使 ...

  9. java 解析excel工具类

      java 解析excel工具类 CreateTime--2018年3月5日16:48:08 Author:Marydon ReadExcelUtils.java import java.io.Fi ...

随机推荐

  1. JS实现页面刷新方法

    下面介绍全页面刷新方法:有时候可能会用到 window.location.reload()刷新当前页面. parent.location.reload()刷新父亲对象(用于框架) opener.loc ...

  2. 数据库路由中间件MyCat - 源代码篇(14)

    此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 对于表的dataNode对应关系,有个特殊配置即类似dataNode="distributed(d ...

  3. JavaScript 检验变量

    创建: 2019/02/20 迁入: 删除[WIP]标签(因为随时更新, 不存在完成不完成)   从[JavaScript 式与运算符]迁入typeof 更新: 2019/03/25 补充静态变量与参 ...

  4. E20190420-hm

    impact n. 巨大影响; 强大作用; 撞击; 冲撞; 冲击力;      v. (对某事物) 有影响,有作用; 冲击; 撞击; incident  n. 发生的事情(尤指不寻常的或讨厌的); 严 ...

  5. Linux下查看磁盘剩余空间和文件夹大小

    1. du -sh 查看当前文件夹大小 2. du -sh * | sort -n 列出当前文件夹下的所有文件夹及其大小,并按照文件夹大小排序 du - sh *  //查看当前文件夹下所有文件的大小 ...

  6. 洛谷P5173 传球(暴力)

    传送门 真·暴力艹过去 不难发现这个转移其实就是一个循环卷积的形式,设有多项式\(A=x+x^{n-1}\),那么\(f_m=f_0\times A^m\) 直接暴力计算并卡常就行了 //minamo ...

  7. django 模版 语法与使用

    目录 django 模版语法与使用 django模板语言介绍 (摘自官方文档) 链接 什么是模板? 模板语句的 注释 变量 {{ 变量 }} 点(.)在模板语言中有特殊的含义,用来获取对象的相应属性值 ...

  8. 填坑帖 By cellur925

    从今天到noip 记录下我犯的一切愚蠢错误. 7.17~7.19  把文件 注释掉了,输出语句放在了关文件之后 7.19           判断素数的板子 把%写成了& bool prime ...

  9. Codeforces Round #564 (Div. 2) B. Nauuo and Chess

    链接:https://codeforces.com/contest/1173/problem/B 题意: Nauuo is a girl who loves playing chess. One da ...

  10. java.lang.ClassNotFoundException: org.slf4j.LoggerFactory

    缺少slf4j-api.jar和slf4j-log4j12.jar这两个jar包导致的错误.