复用代码系列: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

相关文章

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

MySQL 获取字符串长度及注意事项

《MySQL获取字符串长度及注意事项》本文通过实例代码给大家介绍MySQL获取字符串长度及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 获取字符串长度详解 核心长度函数对比⚠️ 六大关键注意事项1. 字符编码决定字节长度2

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,