文章目录

大文件分割与合并例子

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
58
59
60
61
62
63
package main

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

const chunkSize = 1 << (10 * 2)

func main() {
fileInfo, err := os.Stat("cbd.mp4")
if err != nil {
panic(err)
}

num := math.Ceil(float64(fileInfo.Size()) / chunkSize)

fi, err := os.OpenFile("cbd.mp4", os.O_RDONLY, os.ModePerm)
if err != nil {
fmt.Println(err)
return
}
b := make([]byte, chunkSize)
var i int64 = 1
for ; i <= int64(num); i++ {
fi.Seek((i-1)*chunkSize, 0)
if len(b) > int(fileInfo.Size()-(i-1)*chunkSize) {
b = make([]byte, fileInfo.Size()-(i-1)*chunkSize)
}
fi.Read(b)

f, err := os.OpenFile("./"+strconv.Itoa(int(i))+".db", os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
panic(err)
}
f.Write(b)
f.Close()
}
fi.Close()

fii, err := os.OpenFile("all.mp4", os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
if err != nil {
panic(err)
return
}
for i := 1; i <= int(num); i++ {
f, err := os.OpenFile("./"+strconv.Itoa(int(i))+".db", os.O_RDONLY, os.ModePerm)
if err != nil {
fmt.Println(err)
return
}
b, err := ioutil.ReadAll(f)
if err != nil {
fmt.Println(err)
return
}
fii.Write(b)
f.Close()
}
}
文章目录