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. 最优化理论-Simplex线性规划

     Sorry,各位,现在这里面啥也没,之所以开这篇文章,是防止以后用得到:现在研究这些,总感觉有些不合适,本人还不到那个层次:如果之后有机会继续研究simplex-线性规划问题,再回来参考下面的链接进 ...

  2. codevs 3162 抄书问题

    3162 抄书问题 题目描述 Description 现在要把M本有顺序的书分给K个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比如 ...

  3. javascript的程序控制结构及语句------(2)循环控制语句、跳转语句、对话框

    一.循环控制语句 循环语句主要就是在满足条件的情况下反复执行某一个操作,循环控制语句主要包括while语句.do...while语句 和for语句. 1.While语句 语法: While(条件表达式 ...

  4. IT兄弟连 JavaWeb教程 jQuery对AJAX的支持经典案例

    案例需求:编写用户登陆页面的验证码模块,在用户进行登陆时,输入验证码后不需要点击提交按钮,使用AJAX异步地向服务器发送验证验证码的请求.如果验证码正确,可以点击提交按钮,如果验证码输入错误,提示用户 ...

  5. eclipse svn 忽略target .project .classpath等目录文件

    这个build失败的解决方案就是不要把你项目的 target目录放在src repository 里面,还有 .project 和 .classpath 最好也别放到src repository 里. ...

  6. C# 字符串string

    一.引言 在 C# 中,字符串是System.String类的一个引用类型.但与其他引用类型不同的是,C#将字符串视为一个基本类型,它可以申请为一个常量,也可以直接给它赋值. string关键字是Sy ...

  7. [NWPU2016][寒假作业][正常版第三组]I

    素数环,简单的dfs,但这道题我有个小地方写错了半天发现不了..就是flag数组的位置.一定要放在if里面,刚开始没注意,一不小心写到外面了. #include <iostream> #i ...

  8. maven settings.xml windows

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...

  9. Spring注入bean和aop的注意事项

    spring注入类没有构造函数,注入成功抽象类,注入失败不写bean注入的名字,默认是bean第一个字母小写的名字,但是bean名字开头是两个大写,则默认是bean的名字前面所有大写都变小写@Auto ...

  10. window.addeventlistener使用方法

    http://www.jb51.net/article/49858.htm .................................................... (要注意的是div ...