Java解压缩zlib

时间:2015-10-10 12:08:50

标签: java zlib lzma

我有一个字节数组,Hex开始(它很大)用这个:

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头。

我通过以下方法运行它:

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));

但是我没有打印出任何内容..也没有堆栈跟踪...

为什么会这样?

1 个答案:

答案 0 :(得分:1)

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

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