Java 解压 zlib

Java decompress zlib

我有一个字节数组,十六进制开始(很大)是这样的:

78 9C 9D 5B 4B 6F A4 38 10 FE 2F 9C 19 89 F2 83 47 1F 77 46 7B DA D3 CE 6A 2F A3 08 91 B4 93 B4 96 40 0B E8 64 A2 28 FF 7D A1 B1 3B 2E C0 ED 72 4F 94 E9 04 EA 61 57 7D F5 32 E4 23 7A AB 5E 55 D9 9C 5E A2 5D 01 10 47 55 B3 EF DA C3 BE 7C A8 0F AA 19 A2 DD D0 9D

它以 0x789c 开始,它是 lzma headers。

我是运行它通过以下方法:

public byte[] decompress(byte[] bytesToDecompress)
    {
        byte[] returnValues = null;

        Inflater inflater = new Inflater();

        int numberOfBytesToDecompress = bytesToDecompress.length;

        inflater.setInput
        (
            bytesToDecompress,
            0,
            numberOfBytesToDecompress
        );

        int bufferSizeInBytes = numberOfBytesToDecompress;

        int numberOfBytesDecompressedSoFar = 0;
        List<Byte> bytesDecompressedSoFar = new ArrayList<Byte>();

        try
        {
            while (inflater.needsInput() == false)
            {
                byte[] bytesDecompressedBuffer = new byte[bufferSizeInBytes];

                int numberOfBytesDecompressedThisTime = inflater.inflate
                (
                    bytesDecompressedBuffer
                );

                numberOfBytesDecompressedSoFar += numberOfBytesDecompressedThisTime;

                for (int b = 0; b < numberOfBytesDecompressedThisTime; b++)
                {
                    bytesDecompressedSoFar.add(bytesDecompressedBuffer[b]);
                }
            }

            returnValues = new byte[bytesDecompressedSoFar.size()];
            for (int b = 0; b < returnValues.length; b++) 
            {
                returnValues[b] = (byte)(bytesDecompressedSoFar.get(b));
            }

        }
        catch (DataFormatException dfe)
        {
            dfe.printStackTrace();
        }

        inflater.end();

        return returnValues;
    }

    public String decompressToString(byte[] bytesToDecompress)
    {   
        byte[] bytesDecompressed = this.decompress
        (
            bytesToDecompress
        );

        String returnValue = null;

        try
        {
            returnValue = new String
            (
                bytesDecompressed,
                0,
                bytesDecompressed.length,
                "UTF-8"
            );  
        }
        catch (UnsupportedEncodingException uee)
        {
            uee.printStackTrace();
        }

        return returnValue;
    }

像这样: System.out.println(decompressToString(jsonDecompressed));

但是我什么也没有打印出来。也没有堆栈跟踪...

怎么会这样?

您的代码已经正确,您只需使用以下问题中的方法将十六进制字符串转换为字节数组:

Convert a string representation of a hex dump to a byte array using Java?

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

然后您可以对十六进制字符串(不带空格)使用之前的方法

 public static void main(String[] args) {
    String strData = "789C9D5B4B6FA43810FE2F9C1989F283471F77467BDAD3CE6A2FA30891B493B496400BE864A228FF7DA1B13B2EC0ED724F94E904EA61577DF532E4237AAB5E55D99C5EA25D01104755B3EFDAC3BE7CA80FAA19A2DDD09D";
    byte[] jsonDecompressed = hexStringToByteArray(strData);
    System.out.println(decompressToString(jsonDecompressed));
}

得到正确的解压值:

{"wave_num":911,"android_client":tr