0

I need to implement gzdeflate/gzinflate functions in go (compress level 9)

<?php $z = gzdeflate($str, 9); ?>

My current Go realisation looks like this:

func gzdeflate(str string) string {
    var b bytes.Buffer

    w, _ := gzip.NewWriterLevel(&b, 9)
    w.Write([]byte(str))
    w.Close()
    return b.String()
}

func gzinflate(str string) string {
    b := bytes.NewReader([]byte(str))
    r, _ := gzip.NewReader(b)
    bb2 := new(bytes.Buffer)
    _, _ = io.Copy(bb2, r)
    r.Close()
    byts := bb2.Bytes()
    return string(byts)
}

I receive different results

maazza
  • 7,016
  • 15
  • 63
  • 96
  • 7
    Compression is often considered a _non-deterministic_ operation. The same data may be compressed to different byte streams, but when decoded, they will give you back the same, original data. Possible duplicate of [Why do gzip of Java and Go get different results?](http://stackoverflow.com/questions/29002769/why-do-gzip-of-java-and-go-get-different-results) – icza Dec 17 '15 at 11:18

1 Answers1

1

The test is not wether the result of compression is identical or not. The test is whether compression followed by decompression results in the exact same thing as what you started with, regardless of where or how the compressor or decompressor is implemented. E.g. you should be able to pass the compressed data from Go to PHP or vice versa, and have the decompression there give the original input exactly.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158