复用代码系列:6种字符串解压缩工具类

2024-02-15 16:48

本文主要是介绍复用代码系列:6种字符串解压缩工具类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、字符串解压缩(gzip方式)代码如下:

package com.compress;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;import org.apache.commons.io.IOUtils;/*** gzip解压缩工具类* @author suncht**/
public abstract class GZIPUtils  {public static byte[] compress(String str, Charset encoding) {if (str == null || str.length() == 0) {return null;}ByteArrayOutputStream out = new ByteArrayOutputStream();GZIPOutputStream gzip = null;try {gzip = new GZIPOutputStream(out);gzip.write(str.getBytes(encoding));gzip.close();} catch ( Exception e) {e.printStackTrace();} finally {IOUtils.closeQuietly(gzip);}return out.toByteArray();}public static byte[] compress(String str) throws IOException {  return compress(str, StandardCharsets.UTF_8);  }public static byte[] uncompress(byte[] bytes) {if (bytes == null || bytes.length == 0) {return null;}ByteArrayOutputStream out = new ByteArrayOutputStream();ByteArrayInputStream in = new ByteArrayInputStream(bytes);GZIPInputStream ungzip = null;try {ungzip = new GZIPInputStream(in);byte[] buffer = new byte[256];int n;while ((n = ungzip.read(buffer)) >= 0) {out.write(buffer, 0, n);}} catch (Exception e) {e.printStackTrace();} finally {IOUtils.closeQuietly(ungzip);IOUtils.closeQuietly(in);IOUtils.closeQuietly(out);}return out.toByteArray();}public static String uncompressToString(byte[] bytes, Charset encoding) {  if (bytes == null || bytes.length == 0) {  return null;  }  ByteArrayOutputStream out = new ByteArrayOutputStream();  ByteArrayInputStream in = new ByteArrayInputStream(bytes); GZIPInputStream ungzip = null;try {ungzip = new GZIPInputStream(in);  byte[] buffer = new byte[256];  int n;  while ((n = ungzip.read(buffer)) >= 0) {  out.write(buffer, 0, n);  }  return out.toString(encoding.name());} catch (Exception e) {e.printStackTrace();} finally {IOUtils.closeQuietly(ungzip);IOUtils.closeQuietly(in);IOUtils.closeQuietly(out);}return null;}public static String uncompressToString(byte[] bytes) {  return uncompressToString(bytes, StandardCharsets.UTF_8);  } public static void main(String[] args) throws IOException {String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";System.out.println("字符串长度:"+s.length());System.out.println("压缩后::"+compress(s).length);System.out.println("解压后:"+uncompress(compress(s)).length);System.out.println("解压字符串后::"+uncompressToString(compress(s)).length());}
}

2、字符串解压缩(snappy方式),代码如下:

package com.compress;import java.io.IOException;
import java.nio.charset.StandardCharsets;import org.xerial.snappy.Snappy;/*** 字符串解压缩(Snappy)* @author suncht**/
public class SnappyUtils {public static byte[] compressHtml(String str) {try {return Snappy.compress(str.getBytes(StandardCharsets.UTF_8));} catch (IOException e) {e.printStackTrace();}return null;}public static String decompressHtml(byte[] bytes) {try {return new String(Snappy.uncompress(bytes), StandardCharsets.UTF_8);} catch (IOException e) {e.printStackTrace();}return null;}public static void main(String[] args) {String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasdfasdfaaaaaaaaa12121sdgfas";System.out.println("字符串长度:" + s.length());System.out.println("压缩后::" + compressHtml(s).length);System.out.println("解压后:" + decompressHtml(compressHtml(s)).length());}
}

需要Snappy依赖:

<dependency><groupId>org.xerial.snappy</groupId><artifactId>snappy-java</artifactId><version>1.1.7.1</version></dependency>

3、字符串解压缩(lz4方式),代码如下:

package com.compress;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;import net.jpountz.lz4.LZ4BlockInputStream;
import net.jpountz.lz4.LZ4BlockOutputStream;
import net.jpountz.lz4.LZ4Compressor;
import net.jpountz.lz4.LZ4Factory;
import net.jpountz.lz4.LZ4FastDecompressor;public class Lz4Utils {public static byte[] compress(byte srcBytes[]) throws IOException {LZ4Factory factory = LZ4Factory.fastestInstance();ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();LZ4Compressor compressor = factory.fastCompressor();LZ4BlockOutputStream compressedOutput = new LZ4BlockOutputStream(byteOutput, 2048, compressor);compressedOutput.write(srcBytes);compressedOutput.close();return byteOutput.toByteArray();}public static byte[] uncompress(byte[] bytes) throws IOException {LZ4Factory factory = LZ4Factory.fastestInstance();ByteArrayOutputStream baos = new ByteArrayOutputStream();LZ4FastDecompressor decompresser = factory.fastDecompressor();LZ4BlockInputStream lzis = new LZ4BlockInputStream(new ByteArrayInputStream(bytes), decompresser);int count;byte[] buffer = new byte[2048];while ((count = lzis.read(buffer)) != -1) {baos.write(buffer, 0, count);}lzis.close();return baos.toByteArray();}public static String uncompressToString(byte[] data) throws IOException {return new String(uncompress(data), StandardCharsets.UTF_8);}public static void main(String[] args) throws IOException {String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasdfasdfaaaaaaaaa12121sdgfas";System.out.println("字符串长度:" + s.length());byte[] data = compress(s.getBytes(StandardCharsets.UTF_8));System.out.println("压缩后::" + data.length);System.out.println("解压后:" + uncompress(data).length);System.out.println("解压后字符串:" + uncompressToString(data));}
}

需要LZ4的依赖:

<dependency><groupId>net.jpountz.lz4</groupId><artifactId>lz4</artifactId><version>1.3.0</version></dependency>

4、字符串解压缩(Bzip2方式),代码如下:

package com.compress;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;public class Bzip2Utils {public static byte[] compress(byte srcBytes[]) throws IOException {ByteArrayOutputStream out = new ByteArrayOutputStream();BZip2CompressorOutputStream bcos = new BZip2CompressorOutputStream(out);bcos.write(srcBytes);bcos.close();return out.toByteArray();}public static byte[] uncompress(byte[] bytes) {ByteArrayOutputStream out = new ByteArrayOutputStream();ByteArrayInputStream in = new ByteArrayInputStream(bytes);try {BZip2CompressorInputStream ungzip = new BZip2CompressorInputStream(in);byte[] buffer = new byte[2048];int n;while ((n = ungzip.read(buffer)) >= 0) {out.write(buffer, 0, n);}} catch (IOException e) {e.printStackTrace();}return out.toByteArray();}public static String uncompressToString(byte[] data) throws IOException {return new String(uncompress(data), StandardCharsets.UTF_8);}public static void main(String[] args) throws IOException {String s = "AAAAAAAAAAAAAAAAAA";System.out.println("字符串长度:" + s.length());byte[] data = compress(s.getBytes(StandardCharsets.UTF_8));System.out.println("压缩后::" + data.length);System.out.println("解压后:" + uncompress(compress(s.getBytes(StandardCharsets.UTF_8))).length);System.out.println("解压后字符串:" + uncompressToString(data));}
}

需要commons-compress依赖:

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-compress</artifactId><version>1.12</version></dependency>

5、字符串解压缩(Deflater/Inflater方式),代码如下:

package com.compress;import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;public class DeflaterUtils {public static byte[] compress(byte input[]) {ByteArrayOutputStream bos = new ByteArrayOutputStream();Deflater compressor = new Deflater(1);try {compressor.setInput(input);compressor.finish();final byte[] buf = new byte[2048];while (!compressor.finished()) {int count = compressor.deflate(buf);bos.write(buf, 0, count);}} finally {compressor.end();}return bos.toByteArray();}public static byte[] uncompress(byte[] input) throws DataFormatException {ByteArrayOutputStream bos = new ByteArrayOutputStream();Inflater decompressor = new Inflater();try {decompressor.setInput(input);final byte[] buf = new byte[2048];while (!decompressor.finished()) {int count = decompressor.inflate(buf);bos.write(buf, 0, count);}} finally {decompressor.end();}return bos.toByteArray();}public static String uncompressToString(byte[] input) throws DataFormatException {return new String(uncompress(input), StandardCharsets.UTF_8);}public static void main(String[] args) throws DataFormatException {String s = "AAAAAAAAAAAAAAAAAA";System.out.println("字符串长度:" + s.length());byte[] data = compress(s.getBytes(StandardCharsets.UTF_8));System.out.println("压缩后::" + data.length);System.out.println("解压后:" + uncompress(compress(s.getBytes(StandardCharsets.UTF_8))).length);System.out.println("解压后字符串:" + uncompressToString(data));}
}

5、字符串解压缩(LZO方式),代码如下:

package com.compress;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;import org.anarres.lzo.LzoAlgorithm;
import org.anarres.lzo.LzoCompressor;
import org.anarres.lzo.LzoDecompressor;
import org.anarres.lzo.LzoInputStream;
import org.anarres.lzo.LzoLibrary;
import org.anarres.lzo.LzoOutputStream;public class LzoUtils {public static byte[] compress(byte srcBytes[]) throws IOException {LzoCompressor compressor = LzoLibrary.getInstance().newCompressor(LzoAlgorithm.LZO1X, null);ByteArrayOutputStream os = new ByteArrayOutputStream();LzoOutputStream cs = new LzoOutputStream(os, compressor);cs.write(srcBytes);cs.close();return os.toByteArray();}public static byte[] uncompress(byte[] bytes) throws IOException {LzoDecompressor decompressor = LzoLibrary.getInstance().newDecompressor(LzoAlgorithm.LZO1X, null);ByteArrayOutputStream baos = new ByteArrayOutputStream();ByteArrayInputStream is = new ByteArrayInputStream(bytes);LzoInputStream us = new LzoInputStream(is, decompressor);int count;byte[] buffer = new byte[2048];while ((count = us.read(buffer)) != -1) {baos.write(buffer, 0, count);}return baos.toByteArray();}public static String uncompressToString(byte[] data) throws IOException {return new String(uncompress(data), StandardCharsets.UTF_8);}public static void main(String[] args) throws IOException {String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasdfasdfaaaaaaaaa12121sdgfas";System.out.println("字符串长度:" + s.length());byte[] data = compress(s.getBytes(StandardCharsets.UTF_8));System.out.println("压缩后::" + data.length);System.out.println("解压后:" + uncompress(data).length);}
}
需要LZO依赖:
<dependency><groupId>org.anarres.lzo</groupId><artifactId>lzo-core</artifactId><version>1.0.5</version></dependency>

这篇关于复用代码系列:6种字符串解压缩工具类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/711953

相关文章

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

一文详解如何在Python中从字符串中提取部分内容

《一文详解如何在Python中从字符串中提取部分内容》:本文主要介绍如何在Python中从字符串中提取部分内容的相关资料,包括使用正则表达式、Pyparsing库、AST(抽象语法树)、字符串操作... 目录前言解决方案方法一:使用正则表达式方法二:使用 Pyparsing方法三:使用 AST方法四:使用字

Java字符串处理全解析(String、StringBuilder与StringBuffer)

《Java字符串处理全解析(String、StringBuilder与StringBuffer)》:本文主要介绍Java字符串处理全解析(String、StringBuilder与StringBu... 目录Java字符串处理全解析:String、StringBuilder与StringBuffer一、St