我在磁盘上有gzi压缩的文件,我希望将其流式传输到未压缩的HTTP客户端。为此,我需要发送一个长度标头,然后将未压缩的文件流式传输到客户端。我知道gzip协议存储了未压缩数据的原始长度,但据我所知,golang的“compress/gzip”包似乎没有办法抓住这个长度。我已经诉诸于将文件读入变量,然后从中获取字符串长度,但这非常低效并且浪费内存,尤其是在较大的文件上。Bellow 我已经发布了我最终使用的代码:DownloadHandler(w http.ResponseWriter, r *http.Request) {path := "/path/to/thefile.gz";openfile, err := os.Open(path);if err != nil { w.WriteHeader(http.StatusNotFound); fmt.Fprint(w, "404"); return;}defer openfile.Close();fz, err := gzip.NewReader(openfile);if err != nil { w.WriteHeader(http.StatusNotFound); fmt.Fprint(w, "404"); return;}defer fz.Close()// Wastefully read data into a string so I can get the length.s, err := ioutil.ReadAll(fz);r := strings.NewReader(string(s));//Send the headersw.Header().Set("Content-Disposition", "attachment; filename=test");w.Header().Set("Content-Length", strconv.Itoa(len(s))); // Send length to client.w.Header().Set("Content-Type", "text/csv");io.Copy(w, r) //'Copy' the file to the client}我希望能够做的是这样的:DownloadHandler(w http.ResponseWriter, r *http.Request) {path := "/path/to/thefile.gz";openfile, err := os.Open(path);if err != nil { w.WriteHeader(http.StatusNotFound); fmt.Fprint(w, "404"); return;}defer openfile.Close();fz, err := gzip.NewReader(openfile);if err != nil { w.WriteHeader(http.StatusNotFound); fmt.Fprint(w, "404"); return;}defer fz.Close()//Send the headersw.Header().Set("Content-Disposition", "attachment; filename=test");w.Header().Set("Content-Length", strconv.Itoa(fz.Length())); // Send length to client.w.Header().Set("Content-Type", "text/csv");io.Copy(w, fz) //'Copy' the file to the client}有谁知道如何在golang中获取gzip压缩文件的未压缩长度?
- 1 回答
- 0 关注
- 130 浏览
添加回答
举报
0/150
提交
取消