1

我正在寻找通过 java 的 Deflater 往返字节并遇到问题。首先是输出,然后是代码。我在这里做错了什么,我怎样才能正确地往返这些流?

输出:

scala> new String(decompress(compress("face".getBytes)))
(crazy output string of length 20)

代码:

  def compress(bytes: Array[Byte]): Array[Byte] = {
    val deflater = new java.util.zip.Deflater
    val baos = new ByteArrayOutputStream
    val dos = new DeflaterOutputStream(baos, deflater)
    dos.write(bytes)
    baos.close
    dos.finish
    dos.close
    baos.toByteArray
  }

  def decompress(bytes: Array[Byte]): Array[Byte] = {
    val deflater = new java.util.zip.Deflater
    val baos = new ByteArrayOutputStream(512)
    val bytesIn = new ByteArrayInputStream(bytes)
    val in = new DeflaterInputStream(bytesIn, deflater)
    var go = true
    while (go) {
      val b = in.read
      if (b == -1)
        go = false
      else
        baos.write(b)
    }
    baos.close
    in.close
    baos.toByteArray
  }
4

1 回答 1

3

Deflater当你应该-ing 它时,你正在 (re-) -ing 原始通货紧缩的结果Inflater......

于 2013-02-26T03:08:30.613 回答