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. [Xcode 实际操作]五、使用表格-(9)删除UITableView单元格(手势左滑调出删除按钮)

    目录:[Swift]Xcode实际操作 本文将演示如何删除某一行单元格.手势左滑调出删除按钮. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIK ...

  2. [Xcode 实际操作]七、文件与数据-(3)创建文本文件、属性列表文件、图片文件

    目录:[Swift]Xcode实际操作 本文将演示如何创建各种类型的文件. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class V ...

  3. Sublime Text 3 最新注册码激活码 和 Sublime Text 2 注册码

    Sublime是一款很好用的很轻巧的编辑器,堪称一代神级编辑器.此篇文章用于简单学习记录下神器的激活码,不作其他用途.如有侵权,请联系删除,谢谢~~   1.官方下载地址: http://www.su ...

  4. React中方法的this绑定

    第一种 在组件(类)的constructor中绑定this class Demo extends Component { constructor(this) { super(this) this.st ...

  5. jquery jtemplates.js模板渲染引擎的详细用法第三篇

    jquery jtemplates.js模板渲染引擎的详细用法第三篇 <span style="font-family:Microsoft YaHei;font-size:14px;& ...

  6. python基本数据类型2——操作

    字符串 name = "alex" # 移除两边的空格 print(name.strip()) #strip不修改值 # 是否以"al"开头 print(nam ...

  7. Codeforces Round #561 (Div. 2) B. All the Vowels Please

    链接:https://codeforces.com/contest/1166/problem/B 题意: Tom loves vowels, and he likes long words with ...

  8. list remove元素

    public class TestList { public static void main(String[] args) { List<Integer> list = new Link ...

  9. java.sql.SQLException: No suitable driver found for jdbc:hive://localhost:10000/default

    error: java.sql.SQLException: No suitable driver found for jdbc:hive://localhost:10000/default at ja ...

  10. Netty(1-1)Discard

    一.DiscardServerHandler import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext ...