douzhanyan5015 2014-06-07 12:11
浏览 9

去gzip不输出预期的输出

I'm trying to compress a string with Gzip and uncompress it, but it doesn't work as I expected.

My code is the following where I compress "hello World" and then read/decompress it

  s := []byte("hello world")

  var b bytes.Buffer
  gz := gzip.NewWriter(&b)
  defer gz.Close()
  _, err = gz.Write(s)
  if err != nil {
    panic(err)
  }

  r, err := gzip.NewReader(&b)
  defer r.Close()
  if err != nil {
    panic(err)
  }

  l, _ := r.Read(s)

  log.Println(l)

I expect it to return "hello world" but it returns 0..

If I remove l, _ := r.Read(s) and replace the last line with log.Println(r) I get this which doesn't quite makes sense

&{{ [] 2042-07-14 02:04:00 +0100 CET  255} 0x1847b780 0x185aa000 0x18400db8 0 0 [31 139 8 0 0 9 110 136 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] <nil>}

Can someone explain me where I'm wrong?

What I want is just compress a string and return its decompressed result later.

  • 写回答

1条回答 默认 最新

  • dongyuan6949 2014-06-07 12:42
    关注

    Finish writing the output. For example,

    package main
    
    import (
        "bytes"
        "compress/gzip"
        "log"
    )
    
    func main() {
        var b bytes.Buffer
    
        w := gzip.NewWriter(&b)
        s := []byte("hello world")
        _, err := w.Write(s)
        if err != nil {
            panic(err)
        }
        err = w.Flush()
        if err != nil {
            panic(err)
        }
        err = w.Close()
        if err != nil {
            panic(err)
        }
    
        r, err := gzip.NewReader(&b)
        if err != nil {
            panic(err)
        }
        defer r.Close()
        t := make([]byte, len(s))
        l, err := r.Read(t)
        if err != nil {
            panic(err)
        }
    
        log.Println(l, string(t))
    }
    

    Output:

    2009/11/10 23:00:00 11 hello world
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化