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();
}
}
Java压缩字符串工具类的更多相关文章
- 【Java】字符串工具类
import android.annotation.SuppressLint; import java.io.UnsupportedEncodingException; import java.uti ...
- [JAVA][StringUtils]字符串工具类的常用方
StringUtils 方法的操作对象是 java.lang.String 类型的对象,是 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String 为 ...
- Java关于字符串工具类~持续汇总~
/** * 01 * 描述:String的substring和replace方法使用 * [时间 2019年3月5日下午3:22:08 作者 陶攀峰] */ public static void te ...
- StringUtils 字符串工具类
package com.thinkgem.jeesite.common.utils; import java.io.File; import java.io.IOException; import j ...
- java格式处理工具类
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
- JAVA 8 日期工具类
JAVA 8 日期工具类 主题描述 JAVA中日期时间的历史 代码成果 主题描述 JAVA的日期时间一直比较混乱,本来以为joda会是巅峰,但是JAVA 8改变了我的思想.但是即便在JAVA 8面前, ...
- JavaSE-基础语法(二)-系统类(java.lang.*)和工具类(java.util.*)
系统类(java.lang.*)和工具类(java.util.*) 一.系统类(java.lang.*) 这个包下包含java语言的核心类,如String.Math.System和Thread类等,使 ...
- java 解析excel工具类
java 解析excel工具类 CreateTime--2018年3月5日16:48:08 Author:Marydon ReadExcelUtils.java import java.io.Fi ...
随机推荐
- HTML5学习笔记(四)语义元素
语义元素能够清楚的描述其意义给浏览器和开发者. 无语义 元素实例: <div> 和 <span> - 无需考虑内容. 语义元素实例: <form>, <tab ...
- ue4 retarge记录
动画重定位(相同骨骼) https://docs.unrealengine.com/latest/CHN/Engine/Animation/AnimationRetargeting/index.htm ...
- json 打印
JsonObject jsonObj = new JSONObject(); jsonObj.put("success",true); jsonObj.put("msg& ...
- 【NOI2012】迷失游乐园
题目链接:迷失游乐园(BZOJ) 迷失游乐园(Luogu) 独立完成的题,写一发题解纪念一波~ 模拟完样例大概可以知道是道树形DP了. 观察数据范围,发现是基环树,至少会有一个环. 先从树的部分开始 ...
- C语言实现折半插入算法
#include <stdio.h> int BInsertSort(int array[],int left,int right){ //接收主函数调用语句中的实参传到这里的形参里 in ...
- Centos 自动更新git
首先,要先配置好自己的Git,然后在某一处进行脚本的编写. 比如项目目录为:/home/project,那参考如下来进行 vim /home/project/automatic_git.sh #/bi ...
- Jaspersoft Studio简介
参考来源:https://community.jaspersoft.com/documentation/tibco-jaspersoft-studio-user-guide/v640/introduc ...
- 一般处理程序aspx
public bool IsReusable { get { return false; } }属性,将该属性的值改为true,为什么不起作用?按照MSDN的解释,该属性的意思是: “获取一个值,该值 ...
- linux-2.6.22.6/Makefile:416: *** mixed implicit and normal rules: deprecated syntax
今天在按照韦东山大哥的教程流程编译内核的时候出现了这个问题 linux-2.6.22.6/Makefile:416: *** mixed implicit and normal rules: ...
- 【简抄】h5 新增的几个背景属性和文本属性
一.背景图像显示: ①background-size:规定背景图像的大小: 值:像素值.百分比.auto.cover.contain ②background-origin :规定背景图像的起始位置: ...