在Golang中实现PHP的gzdeflate/gzinflate功能。

huangapple go评论72阅读模式
英文:

PHP gzdeflate/gzinflate functionality on Golang

问题

我需要在go中实现gzdeflate/gzinflate函数(压缩级别为9)。

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

我的当前Go实现如下:

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 need to implement gzdeflate/gzinflate functions in go (compress level 9)

&lt;?php $z = gzdeflate($str, 9); ?&gt;

My current Go realisation looks like this:

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

	w, _ := gzip.NewWriterLevel(&amp;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

答案1

得分: 1

测试的重点不在于压缩结果是否完全相同,而在于压缩后再解压是否能得到与原始数据完全相同的结果,无论压缩器或解压器的实现方式如何。例如,你应该能够将压缩后的数据从Go传递到PHP,或者反过来,然后在那里进行解压缩,得到与原始输入完全相同的结果。

英文:

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.

huangapple
  • 本文由 发表于 2015年12月17日 18:59:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/34332902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定