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

相关文章

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

Java使用正则提取字符串中的内容的详细步骤

《Java使用正则提取字符串中的内容的详细步骤》:本文主要介绍Java中使用正则表达式提取字符串内容的方法,通过Pattern和Matcher类实现,涵盖编译正则、查找匹配、分组捕获、数字与邮箱提... 目录1. 基础流程2. 关键方法说明3. 常见场景示例场景1:提取所有数字场景2:提取邮箱地址4. 高级