2015-08-01 07:20
浏览 256


Why can't decompress gzip data by Go in my PHP demo, but PHP gzip data to Go is successful? I need post gzip JSON data from Go to PHP API service.

Test result

 -> |  php   |  go 
php |  ok    |  ok
go  |  fail  |  ok

PHP code

class GzipDemo
    public function gzen($data, $file){
       $json_data = json_encode($data);
       $gz_data = gzencode($json_data,9);

    public function gzdn($file){
       $data = file_get_contents($file);
        $unpacked = gzdecode($data);
        if ($unpacked === FALSE)
             print($file. " result Data :".$unpacked ."

$demo = new GzipDemo();

This result: PHP to PHP okay. Go to PHP fail.

Go code

package main

import (

func main() {
    gzen("data", "gogzip.txt")
func gzen(data string, file string) {
    b, _ := json.Marshal(data)
    buffer := new(bytes.Buffer)
    w, _ := gzip.NewWriterLevel(buffer, gzip.BestCompression)
    defer w.Close()
    ioutil.WriteFile(file, buffer.Bytes(), os.ModePerm)
func gden(file string) {
    b, _ := ioutil.ReadFile(file)
    buffer := new(bytes.Buffer)
    r, _ := gzip.NewReader(buffer)
    defer r.close()
    data, _ := ioutil.ReadAll(r)
    fmt.Println(file, " result Data:", string(data))

This result: Go to Go okay. PHP to Go okay.

图片转代码服务由CSDN问答提供 功能建议

为什么无法通过我的PHP演示中的Go解压缩gzip数据,但PHP gzip数据到Go是否成功? 我需要从Go到PHP API服务发布gzip JSON数据。


   - >  |  php |  go 
php | 好的 好的
go | 失败|  ok 


  class GzipDemo 
 public function  gzen($ data,$ file){
 $ json_data = json_encode($ data); 
 $ gz_data = gzencode($ json_data,9); 
 file_put_contents($ file,$ gz_data); 
 \  n public function gzdn($ file){
 $ data = file_get_contents($ file); 
 $ unpacked = gzdecode($ data); 
 if($ unpacked === FALSE)
 print(“ 失败:“。$ file。”
} else {
 print($ file。”result Data:“。$ unpacked。”
} \  n 
 $ demo = new GzipDemo(); 
 $ file =“phpgzip.txt”; 
 $ demo-> gzen(“data”,$ file); 
 $ demo-> gzdn($ file  ); 
 $ demo-> gzdn(“gogzip.txt”); 

这个结果: 对PHP的错误。 转到PHP失败。


  package main 
“compress  / gzip“
”encoding / json“
”io / ioutil“
func main(){
 gzen(”data“,”gogzip.txt  “)
 gden(  “gogzip.txt”)
 nunc gzen(数据字符串,文件字符串){
b,_:= json.Marshal(data)
 buffer:= new(bytes)  .Buffer)
w,_:= gzip.NewWriterLevel(buffer,gzip.BestCompression)
 ioutil.WriteFile(file,  buffer.Bytes(),os.ModePerm)
func gden(文件字符串){
b,_:= ioutil.ReadFile(file)
 buffer:= new(bytes.Buffer)
 buffer.Write(  b)
r,_:= gzip.NewReader(buffer)
 defer r.close()
 data,_:= ioutil.ReadAll(r)
 fmt.Println(file,“result Data:”,string  (数据))

这个结果: 转到去吧。 非常好。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqhr76378 2015-08-01 08:19

    It works with the following changes:

    • In your PHP code you want to use gzdecode instead of gzinflate. And you don't need this substr($data, 10) stuff if you use that. I didn't read up on how deflate relates to gzip but the simplicity is that gzencode/gzdecode match what the golang gzip package does and what the gz* family of GNU command line tools do.
    • In your Go code move your gzip.Writer.Close() call to be done before you read from buffer. As you can see here: there is some additional stuff that is written to the underlying stream upon close, so in your example above what you are writing is incomplete. (The defer statement causes Close() to be run after the containing function exits.) Most likely the Go gzip decoding is managing to decode anyway whereas the PHP implementation is not - in any case you should properly close the stream to your in memory buffer to ensure it is complete before writing it out to a file.

    OBLIGATORY NOTE: You are ignoring all of your errors in your Go code. This code looks like just a test so I won't belabor the point but you definitely want to be doing proper error handling (either reporting the problem to the user or to the caller of your function).

    点赞 打赏 评论