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. Angular中依赖注入方式的几种写法

    1.第一种写法 angular.module('App').controller('TestCtrl',['$scope', function($scope) {}]); 2.第二种写法 angula ...

  2. [转]PBFT 算法详解

    https://www.cnblogs.com/mafeng/p/8405375.html

  3. 关于fiddler抓取HTTPS请求443的问题

    1.环境:fiddler4.IOS10.3以上 2.需求:使用fiddler抓取IOS上的https请求 3.解决方案 步骤一:设置fiddler 步骤二:手机端安装证书 手机设置代理,打开手机浏览器 ...

  4. Java进程占用内存过高,排查解决方法

    最近收到邮件报警,说内存使作率达到84%.如下图: 解决方法: A:可能是代码原因导致的问题: 1.使用命令:top 查看当前进程的状态 2.从上图可以看到PID:916的java进程占用内存较大.定 ...

  5. CentOS6.7上安装nginx1.8.0

    主题: CentOS6.7上安装nginx1.8.0 环境准备: 1.gcc-c++ 示例:yum install gcc-c++ 安装:gcc-c++ gcc-c++编译工具 2.PCRE(Perl ...

  6. go系列(6)- beego日志模块的使用

    1.安装日志模块 切换到工作目录,下载日志模块 cd /data/work/go/ go get github.com/astaxie/beego/logs 2.导入日志模块 使用的时候,需要导入模块 ...

  7. CC06:像素翻转

    题目 有一副由NxN矩阵表示的图像,这里每个像素用一个int表示,请编写一个算法,在不占用额外内存空间的情况下(即不使用缓存矩阵),将图像顺时针旋转90度. 给定一个NxN的矩阵,和矩阵的阶数N,请返 ...

  8. eclipse plugin

    快速查看目录 org.sf.easyexplore_1.0.4.jar mongo DB net.jumperz.app.MMonjaDB_1.0.16.jar jasper report jaspe ...

  9. WebApi迁移ASP.NET Core2.0

    WebApi迁移ASP.NET Core2.0 一步一步带你做WebApi迁移ASP.NET Core2.0   随着ASP.NET Core 2.0发布之后,原先运行在Windows IIS中的AS ...

  10. ASM 磁盘组的的scrip

    之前经常用如下方式进行查询:步骤 1 以oracle用户登录系统.步骤 2 执行如下命令改变ORACLE_SID环境变量.$ export ORACLE_SID=+ASM1[1或者2]需要通过ps - ...