Alexander Telpis
Alexander Telpis

Reputation: 41

PHP gzdeflate/gzinflate functionality on Golang

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

Upvotes: 0

Views: 526

Answers (1)

Mark Adler
Mark Adler

Reputation: 112412

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.

Upvotes: 1

Related Questions