douchong8393 2017-03-02 22:31
浏览 10
已采纳

ReadAll from File不能按预期工作

I am trying to create a temporary gzip file and write to the file. The problem is that I am not understanding what is going on with ReadAll. I expected ReadAll to return the bytes written to the file... however there are none. Yet the File.Stat command shows that there is indeed data.

filename := "test"
file, err := ioutil.TempFile("", filename)
if err != nil {
    fmt.Println(err)
}
defer func() {
    if err := os.Remove(file.Name()); err != nil {
        fmt.Println(err)
    }
}()

w := gzip.NewWriter(file)
_, err = w.Write([]byte("hell0"))
if err != nil {
    fmt.Println(err)
}

fileInfo, err := file.Stat()
if err != nil {
    fmt.Println(err)
}
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
    fmt.Println(err)
}
if err := w.Close(); err != nil {
    fmt.Println(err)
}
fmt.Println("SIZE1:", fileInfo.Size())
fmt.Println("SIZE2:", len(fileBytes))

Here is a playground link https://play.golang.org/p/zX8TSCAbRL

Why are there no returned bytes? How do I get the returned bytes?

  • 写回答

2条回答 默认 最新

  • dsh8009271 2017-03-02 23:02
    关注

    Close the file before reading it.

    From the documentation of gzip:

    Write writes a compressed form of p to the underlying io.Writer. The compressed bytes are not necessarily flushed until the Writer is closed.

    The solution therefore is to Close both the gzip Writer as well as the underlying io.Writer before attempting to read the number of bytes.

        func main() {
            basename := "test"
            file, err := ioutil.TempFile("", basename)
            tempFilename := file.Name()
            if err != nil {
                fmt.Println(err)
            }
            defer func() {
                if err := os.Remove(file.Name()); err != nil {
                    fmt.Println(err)
                }
            }()
    
            w := gzip.NewWriter(file)
            _, err = w.Write([]byte("hell0"))
            if err != nil {
                fmt.Println(err)
            }
    
            w.Close()
            file.Close()
    
            file, err = os.Open(tempFilename)
            fileInfo, err := file.Stat()
            if err != nil {
                fmt.Println(err)
            }
            fileBytes, err := ioutil.ReadAll(file)
            if err != nil {
                fmt.Println(err)
            }
            if err := w.Close(); err != nil {
                fmt.Println(err)
            }
            fmt.Println("SIZE1:", fileInfo.Size())
            fmt.Println("SIZE2:", len(fileBytes))
    
    }
    

    View on Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题