不读就确定 Golang Gzip 文件的长度?
Determine Length Of Golang Gzip File Without Reading It?
我在磁盘上有 gzip 文件,我希望将这些文件以未压缩的方式流式传输到 HTTP 客户端。为此,我需要发送一个长度 header,然后将未压缩的文件流式传输到客户端。我知道 gzip 协议存储未压缩数据的原始长度,但据我所知,golang 的“compress/gzip”包似乎没有办法获取这个长度。我已经求助于将文件读入一个变量,然后从中获取字符串长度,但这非常低效并且浪费内存,尤其是在较大的文件上。
下面我发布了我最终使用的代码:
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 headers
w.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 headers
w.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 文件的未压缩长度?
gzip 格式似乎提供了未压缩的长度,但实际上并没有。不幸的是,获得未压缩长度的唯一可靠方法是解压缩 gzip 流。 (您可以只计算字节数,而不是在任何地方保存未压缩的数据。)
请参阅 this answer 了解原因。
我在磁盘上有 gzip 文件,我希望将这些文件以未压缩的方式流式传输到 HTTP 客户端。为此,我需要发送一个长度 header,然后将未压缩的文件流式传输到客户端。我知道 gzip 协议存储未压缩数据的原始长度,但据我所知,golang 的“compress/gzip”包似乎没有办法获取这个长度。我已经求助于将文件读入一个变量,然后从中获取字符串长度,但这非常低效并且浪费内存,尤其是在较大的文件上。
下面我发布了我最终使用的代码:
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 headers
w.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 headers
w.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 文件的未压缩长度?
gzip 格式似乎提供了未压缩的长度,但实际上并没有。不幸的是,获得未压缩长度的唯一可靠方法是解压缩 gzip 流。 (您可以只计算字节数,而不是在任何地方保存未压缩的数据。)
请参阅 this answer 了解原因。