字符串解压缩算法 java_Java压缩/解压缩字符串/数据

字符串解压缩算法 java

字符串解压缩算法 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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值