为了账号安全,请及时绑定邮箱和手机立即绑定

Golang:为什么 compress/gzip Read 函数不读取文件内容?

Golang:为什么 compress/gzip Read 函数不读取文件内容?

Go
一只甜甜圈 2021-11-01 15:13:48
我制作了一个文本文件,然后用gzip. 然后我运行以下go程序来读取该压缩文件的内容。package mainimport (    "compress/gzip"    "fmt"    "os")func main() {    handle, err := os.Open("zipfile.gz")    if err != nil {        fmt.Println("[ERROR] File Open:", err)    }    defer handle.Close()    zipReader, err := gzip.NewReader(handle)    if err != nil {        fmt.Println("[ERROR] New gzip reader:", err)    }    defer zipReader.Close()    var fileContents []byte    bytesRead, err := zipReader.Read(fileContents)    if err != nil {        fmt.Println("[ERROR] Reading gzip file:", err)    }    fmt.Println("[INFO] Number of bytes read from the file:", bytesRead)    fmt.Printf("[INFO] Uncompressed contents: '%s'\n", fileContents)}我得到的答复如下:$ go run zipRead.go[INFO] Number of bytes read from the file: 0[INFO] Uncompressed contents: ''为什么我没有从文件中获取任何内容?我在 OS X 和 Ubuntu 上都创建了 zip 文件。我已经go在 OS X 和 Ubuntu 上构建了这个程序,结果相同。
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 164 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号