![字符串解压缩算法 java](https://i-blog.csdnimg.cn/blog_migrate/33582a29dbec3ebcc8867375c34c09d0.png)
字符串解压缩算法 java
Java使用ZLIB压缩库为常规压缩提供了Deflater类。 它还提供了DeflaterOutputStream,它使用Deflater类通过压缩(压缩)数据流,然后将压缩后的数据写入另一个输出流来过滤数据流。 有等效的Inflater和InflaterOutputStream类来处理减压。
压缩
这是一个如何使用DeflatorOutputStream压缩字节数组的示例。
//Compress byte arraystatic byte[] compressBArray(byte [] bArray) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(); try (DeflaterOutputStream dos = new DeflaterOutputStream(os)) {
dos.write(bArray); }
return os.toByteArray();}
让我们测试一下:
//Testingbyte[] input = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".getBytes();byte[] op = CompressionUtil.compressBArray(input);System.out.println("original data length " + input.length + ", compressed data length " + op.length);
结果是“原始数据长度71,压缩数据长度12”
减压
public static byte [] decompress( byte [] compressedTxt) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(); try (OutputStream ios = new InflaterOutputStream(os)) {
ios.write(compressedTxt); }
return os.toByteArray();}
让我们测试一下:
byte [] input = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" .getBytes(); byte [] op = CompressionUtil.compress(input);
byte [] org = CompressionUtil.decompress(op);
System.out.println( new String(org, StandardCharsets.UTF_8));
这将打印原始的“输入”字符串。
让我们将byte []转换为Base64以使其可移植
在上述示例中,我们以字节数组格式(字节[])获取压缩数据,该格式是数字数组。但是我们可能想将压缩数据传输到文件,json或db,对吗? 因此,为了进行传输,我们可以使用以下命令将其转换为Base64
{
byte [] bytes = {}; //the byte array String b64Compressed = new String(Base64.getEncoder().encode(bytes));
byte [] decompressedBArray = Base64.getDecoder().decode(b64Compressed); new String(decompressedBArray, StandardCharsets.UTF_8); //convert to original string if input was string }
这是完整的代码和测试用例
package compress;
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.zip.Deflater; import java.util.zip.DeflaterOutputStream; import java.util.zip.InflaterOutputStream;
public class CompressionUtil {
public static String compressAndReturnB64(String text) throws IOException {
return new String(Base64.getEncoder().encode(compress(text))); } String(Base64.getEncoder().encode(compress(text))); }
public static String decompressB64(String b64Compressed) throws IOException {
byte [] decompressedBArray = decompress(Base64.getDecoder().decode(b64Compressed)); return new String(decompressedBArray, StandardCharsets.UTF_8); } String(decompressedBArray, StandardCharsets.UTF_8); }
public static byte [] compress(String text) throws IOException {
return compress(text.getBytes()); } compress(text.getBytes()); }
public static byte [] compress( byte [] bArray) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(); try (DeflaterOutputStream dos = new DeflaterOutputStream(os)) {
dos.write(bArray); }
return os.toByteArray(); } os.toByteArray(); }
public static byte [] decompress( byte [] compressedTxt) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(); try (OutputStream ios = new InflaterOutputStream(os)) {
ios.write(compressedTxt); }
return os.toByteArray(); } os.toByteArray(); }
}
测试用例:
package compress;
import org.junit.jupiter.api.Test;
import java.io.IOException; import java.nio.charset.StandardCharsets;
public class CompressionTest {
String testStr = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" ;
@Test void compressByte() throws IOException {
byte [] input = testStr.getBytes(); byte [] op = CompressionUtil.compress(input); System.out.println( [] op = CompressionUtil.compress(input); System.out.println( "original data length " + input.length + ", compressed data length " + op.length);
byte [] org = CompressionUtil.decompress(op); System.out.println(org.length); System.out.println( [] org = CompressionUtil.decompress(op); System.out.println(org.length); System.out.println( [] org = CompressionUtil.decompress(op); System.out.println(org.length); System.out.println( new String(org, StandardCharsets.UTF_8)); } String(org, StandardCharsets.UTF_8)); }
@Test void compress() throws IOException {
String op = CompressionUtil.compressAndReturnB64(testStr); System.out.println( String op = CompressionUtil.compressAndReturnB64(testStr); System.out.println( "Compressed data b64" + op);
String org = CompressionUtil.decompressB64(op); System.out.println( String org = CompressionUtil.decompressB64(op); System.out.println( "Original text" + org);
}
}
注意:由于compress和decompress方法对byte []进行操作,因此我们可以压缩/解压缩任何数据类型。
翻译自: https://www.javacodegeeks.com/2020/07/java-compress-decompress-string-data.html
字符串解压缩算法 java