dongou1970 2013-06-05 18:11
浏览 80
已采纳

如何解压缩io.ReadCloser?

I fetch a data from http service and I want to unzip it on the fly. Here is my current approach:

resp, err := http.Get(url)
if err != nil {
    logger.Fatalf("Can't fatch %s data. %v", url, err)
}
logger.Info("Fetched data from %s", url)
content_zipped, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
    logger.Fatal(err)
}
zip_reader, err := zip.NewReader(bytes.NewReader(content_zipped), int64(len(content_zipped)))

Is there any way to unzip the resp.Body without reading all the content at once (6. line)? I mean to stream the bytes.

  • 写回答

2条回答 默认 最新

  • douhuai4155 2013-06-05 18:39
    关注

    Zip archives require random access for reading, so it's hard to stream bytes. In particular, see the source for zip.Reader.init here: http://golang.org/src/pkg/archive/zip/reader.go?s=1265:1323#L59 . The first thing it does is call readDirectoryEnd which reads from near the end of the file.

    Can you use a different compression method (for example, gzip)? Then you can use gzip.NewReader(resp.Body) to stream the data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记