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

相关文章

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

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

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

Springboot3+将ID转为JSON字符串的详细配置方案

《Springboot3+将ID转为JSON字符串的详细配置方案》:本文主要介绍纯后端实现Long/BigIntegerID转为JSON字符串的详细配置方案,s基于SpringBoot3+和Spr... 目录1. 添加依赖2. 全局 Jackson 配置3. 精准控制(可选)4. OpenAPI (Spri

使用Python实现base64字符串与图片互转的详细步骤

《使用Python实现base64字符串与图片互转的详细步骤》要将一个Base64编码的字符串转换为图片文件并保存下来,可以使用Python的base64模块来实现,这一过程包括解码Base64字符串... 目录1. 图片编码为 Base64 字符串2. Base64 字符串解码为图片文件3. 示例使用注意

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=