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

相关文章

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法

《golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法》:本文主要介绍golang获取当前时间、时间戳和时间字符串及它们之间的相互转换,本文通过实例代码给大家介绍的非常详细,感兴趣... 目录1、获取当前时间2、获取当前时间戳3、获取当前时间的字符串格式4、它们之间的相互转化上篇文章给大家介

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘