【发布时间】:2015-10-10 12:08:50
【问题描述】:
我有一个字节数组,十六进制以这个开头(它很大):
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));
但是我什么也没打印出来..也没有堆栈跟踪...
这是怎么回事?
【问题讨论】:
-
尝试使用IDE调试功能,迟早你需要使用它,可能numberOfBytesDecompressedThisTime为0或inflater.needsInput() = true
-
请注意,
78 9c是 zlib 标头,而不是 lzma 标头。