2

我正在尝试使用 snappy-java 解压缩字节串

ByteString(0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, 1, 14, 0, 0, 38, -104, 43, -49, 0, 0, 0, 6, 0, 0, 0, 0, 79, 75)

它包含两帧,第一帧的块值为 0xff(流标识符),长度为 6,第二帧的块类型为 1(未压缩),长度为 14。根据 [此处] 找到的协议规范(http://code. google.com/p/snappy/source/browse/trunk/framing_format.txt

用于解压的代码在这里

val c = ByteString(0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, 1, 14, 0, 0, 38, -104, 43, -49, 0, 0, 0, 6, 0, 0, 0, 0, 79, 75)
Snappy.uncompress(c.toArray)

代码抛出 FAILED_TO_UNCOMPRESS 错误,这是 jna 的一部分。我正在使用 scala v2.11.3 和 snappy-java v1.0.5.4

Exception in thread "main" java.io.IOException: FAILED_TO_UNCOMPRESS(5)
at org.xerial.snappy.SnappyNative.throw_error(SnappyNative.java:78)
at org.xerial.snappy.SnappyNative.rawUncompress(Native Method)
at org.xerial.snappy.Snappy.rawUncompress(Snappy.java:395)
at org.xerial.snappy.Snappy.uncompress(Snappy.java:431)
at org.xerial.snappy.Snappy.uncompress(Snappy.java:407)
4

1 回答 1

3

无法解压错误是因为 Snappy.uncompress 不支持框架输入。框架格式最近最终确定,并在 SnappyFramedInputStream 中添加了实现。源码位于这里

以下是解压 snappy 帧的代码

def decompress(contents: Array[Byte]): Array[Byte] = {
    val is = new SnappyFramedInputStream(new ByteArrayInputStream(contents))
    val os = new ByteArrayOutputStream(Snappy.uncompressedLength(contents))
    is.transferTo(os)
    os.close()
    os.toByteArray
}
于 2014-10-18T14:16:08.130 回答