GolangNote

Golang笔记

Go 分片切割大文件的例子

Permalink

Go 分片切割大文件

Go: 分片切割大文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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 转摘请注明来源

Related articles

Write a Comment to "Go 分片切割大文件的例子"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 1ms