大文件的 GOLANG Base64 编码和解码大小不匹配

GOLANG Base64 Encode and Decode size mismatched for large files

当我尝试使用 golang 对大文件进行 base64 编码和解码时,原始文件和解码文件的字节长度不匹配。

在我测试文本文件时,新行不匹配(1 字节),二进制文件不匹配(2 字节)。

什么可能导致这些字节丢失?

package main

import (
    "encoding/base64"
    "io"
    "os"
    "log"
)

func Encode(infile, outfile string) error {
    input, err := os.Open(infile)
    if err != nil {
        return err
    }
    // Close input file
    defer input.Close()

    // Open output file
    output, err := os.Create(outfile)
    if err != nil {
        return err
    }
    // Close output file
    defer output.Close()

    encoder := base64.NewEncoder(base64.StdEncoding, output)
    l, err := io.Copy(encoder, input)
    if err!=nil {
        log.Printf("Failed to encode file:%v",err)
        return err
    } else {
        log.Printf("Wrote %v bytes",l)
    }

    return nil
}

func Decode(infile, outfile string) error {
    input, err := os.Open(infile)
    if err != nil {
        return err
    }
    // Close input file
    defer input.Close()

    // Open output file
    output, err := os.Create(outfile)
    if err != nil {
        return err
    }
    // Close output file
    defer output.Close()

    decoder := base64.NewDecoder(base64.StdEncoding, input)
    l, err := io.Copy(output, decoder)
    if err!=nil {
        log.Printf("Failed to encode file:%v",err)
        return err
    } else {
        log.Printf("Wrote %v bytes",l)
    }

    return nil
}

您没有 Close() encoder,因此它不会刷新所有数据。来自 docs(强调我的):

func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser

NewEncoder returns a new base64 stream encoder. Data written to the returned writer will be encoded using enc and then written to w. Base64 encodings operate in 4-byte blocks; when finished writing, the caller must Close the returned encoder to flush any partially written blocks.

我还引用了文档中的示例,其中有很好的评论:

package main

import (
    "encoding/base64"
    "os"
)

func main() {
    input := []byte("foo\x00bar")
    encoder := base64.NewEncoder(base64.StdEncoding, os.Stdout)
    encoder.Write(input)
    // Must close the encoder when finished to flush any partial blocks.
    // If you comment out the following line, the last partial block "r"
    // won't be encoded.
    encoder.Close()
}