Java中如何利用zlib进行数据解压

   2024-10-01 9170
核心提示:在Java中,可以使用java.util.zip包中的Inflater类来对数据进行解压缩。以下是一个简单的示例代码:import java.io.ByteArrayInp

在Java中,可以使用java.util.zip包中的Inflater类来对数据进行解压缩。以下是一个简单的示例代码:

import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.util.zip.Inflater;public class ZlibDecompress {    public static byte[] decompress(byte[] compressedData) {        try {            Inflater inflater = new Inflater();            inflater.setInput(compressedData);            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compressedData.length);            byte[] buffer = new byte[1024];            while (!inflater.finished()) {                int count = inflater.inflate(buffer);                outputStream.write(buffer, 0, count);            }            byte[] decompressedData = outputStream.toByteArray();            outputStream.close();            return decompressedData;        } catch (Exception e) {            e.printStackTrace();            return null;        }    }    public static void main(String[] args) {        byte[] compressedData = ...; // 传入压缩过的数据        byte[] decompressedData = decompress(compressedData);        System.out.println("解压后的数据:" + new String(decompressedData));    }}

在上面的示例中,decompress方法接收一个压缩过的数据字节数组,并返回解压后的数据字节数组。在main方法中,我们可以传入压缩过的数据,并打印出解压后的数据。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式 | 网站留言    |  赣ICP备2021007278号