doukang7858 2018-04-01 08:17 采纳率: 0%
浏览 147
已采纳

将DumpResponse写入文件后解压缩

I have following code:

dump, err := httputil.DumpResponse(response, true)
ioutil.WriteFile(response.Request.Host+".txt", dump, 0644)

I creates following file example.com.txt:

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Accept-Ranges: bytes
Age: 0
Cache-Control: public
Content-Encoding: gzip
Content-Type: text/xml
Date: Sun, 01 Apr 2018 08:52:39 GMT
Last-Modified: Thu, 01 Mar 2018 13:30:10 GMT
Server: Microsoft-IIS/7.5
Vary: Accept-Encoding
Via: 1.1 varnish

8000
�      �`I�����B�⓿�O����?����...

How can I read gzipped content from the file now?

  • 写回答

1条回答 默认 最新

  • dongti7838 2018-04-02 12:42
    关注

    You can deserialize it back to http.Response, and then 'ungzip' body:

    func main()  {
        file, _ := os.Open(`/home/your/path/file.txt`)
        response, _ := http.ReadResponse(bufio.NewReader(file), nil)
        reader, _ := gzip.NewReader(response.Body)
        ungzipped, _ := ioutil.ReadAll(reader)
        fmt.Println(string(ungzipped))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容