Golang:实现断点续传

Posted by Viletyy on 2020-05-21 10:01

断点续传

Seeker接口

1
2
3
type Seeker interface {
    Seek(offset int64, whence int) (int64, error)
}

第一个参数:偏移量, 第二个参数: 0:seekStart表示相对于文件开始 1:seekCurrent表示相对于当前偏移量 2:seekend表示相对于文件结尾

1
2
3
4
5
const (
    SeekStart = 0 
    SeekCurrent = 1
    SeekEnd = 2
)

示例

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
func main() {
    srcFile := "/Users/ruby/Documents/pro/a/guliang.jpeg"
    destFile := "guliang4.jpeg"
    tempFile := destFile + "temp.txt"
    file1, _ := os.Open(srcFile) 
    file2, _ := os.OpenFile(destFile, os.O_CREATE|os.O_WRONLY, os.ModePerm)
    file3, _ := os.OpenFile(tempFile, os.O_CREATE|os.O_RDWR, os.ModePerm)
    
    defer file1.Close() 
    defer file2.Close()
    
    file3.Seek(0, io.SeekStart)
    bs := make([]byte, 100, 100)
    n1, err := file3.Read(bs)
    fmt.Println(n1)
    countStr := string(bs[:n1])
    fmt.Println(countStr)
    count, _ := strconv.ParseInt(countStr, 10, 64)
    fmt.Println(count)
    
    // 设置读,写的偏移量
    file1.Seek(count, 0)
    file2.Seek(count, 0)
    data := make([]byte, 1024, 1024)
    n2 := -1 
    n3 := -1 
    total := int(count)
    
    for{
        n2, err  = file1.Read(data)
        if err == io.EOF {
            fmt.Println("文件复制完毕...")
            file3.Close()
            os.Remove(tempFile)
            break
        }
        // 将数据写入到目标文件
        n3, _ = file2.Write(data[:n2])
        total += n3 
        file3.Seek(0, io.SeekStart)
        file3.WriteString(strconv.Itoa(total))
    }
}