Golang笔记

Go 分片切割大文件的例子

Go 分片切割大文件

package main

import (
    "fmt"
    "io/ioutil"
    "math"
    "os"
    "strconv"
)

func main() {

    fileToBeChunked := "./somebigfile"

    file, err := os.Open(fileToBeChunked)

    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    defer file.Close()

    fileInfo, _ := file.Stat()

    var fileSize int64 = fileInfo.Size()

    const fileChunk = 1 * (1 << 20) // 1 MB, change this to your requirement

    // calculate total number of parts the file will be chunked into

    totalPartsNum := uint64(math.Ceil(float64(fileSize) / float64(fileChunk)))

    fmt.Printf("Splitting to %d pieces.\n", totalPartsNum)

    for i := uint64(1); i < totalPartsNum; i++ {

        partSize := int(math.Min(fileChunk, float64(fileSize-int64(i*fileChunk))))
        partBuffer := make([]byte, partSize)

        file.Read(partBuffer)

        // write to disk
        fileName := "somebigfile_" + strconv.FormatUint(i, 10)
        _, err := os.Create(fileName)

        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }

        // write/save buffer to disk
        ioutil.WriteFile(fileName, partBuffer, os.ModeAppend)

        fmt.Println("Split to : ", fileName)
    }
}
本文网址: https://golangnote.com/topic/125.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Captcha image
Relative Articles