将DumpResponse写入文件后Ungzip

时间:2018-04-01 08:17:51

标签: file http go gzip

我有以下代码:

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

我创建了以下文件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����?����...

如何从文件中读取gzip压缩内容?

1 个答案:

答案 0 :(得分:0)

您可以将其反序列化回http.Response,然后“ungzip”'体:

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))
}