0
点赞
收藏
分享

微信扫一扫

Deflater 和 Inflater 的用法


资料

Deflater 和 Inflater 的用法

String inputString = "blahblahblah??";
        byte[] input = inputString.getBytes(StandardCharsets.UTF_8);
        System.out.println("input length -> " + input.length);

        // compress the bytes
        byte[] output = new byte[100];
        Deflater compresser = new Deflater();
        compresser.setInput(input);
        compresser.finish(); // done
        int compressedDataLength = compresser.deflate(output); // compress
        System.out.println("compressedDataLength -> " + compressedDataLength);

        // Decompress the bytes
        Inflater decompresser = new Inflater();
        decompresser.setInput(output, 0, compressedDataLength);
        // 对byte[]进行解压,同事可以要解压的数据包中的某一段数据,就好像从zip中解压出某一个文件一样
        byte[] result = new byte[100];
        int resultLength = decompresser.inflate(result); // 返回的是解压后的数据包大小
        decompresser.end();

        // Decode the bytes into a String
        String outputString = new String(result, 0, resultLength, StandardCharsets.UTF_8);
        System.out.println(outputString);


举报

相关推荐

0 条评论