脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - golang实现多协程下载文件(支持断点续传)

golang实现多协程下载文件(支持断点续传)

2021-12-02 12:03山与路 Golang

本文主要介绍了golang实现多协程下载文件,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

引言

写这篇文章主要是周末休息太无聊,看了看别人代码,发现基本上要么是多协程下载文件要么就只有单协程的断点续传,所以就试了试有进度条的多协程下载文件(支持断点续传)

?
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
 
import (
 "fmt"
 "io"
 "os"
 "regexp"
 "strconv"
 "sync"
 
 "github.com/qianlnk/pgbar"
)
 
/**
* 需求:
1. 多协程下载文件
2.断点续连
**/
func main() {
 //获取要下载文件
 DownloadFileName := "./123.zip"
 //copy的文件
 copyFileName := "./test.zip"
 storgeFileName := "./current.txt"
 //打开文件
 sfile, err := os.Open(DownloadFileName)
 if err != nil {
  panic(err)
 }
 defer sfile.Close()
 //获取文件大小
 info, _ := sfile.Stat()
 downloadSize := info.Size()
 var scount int64 = 1
 if downloadSize%5 == 0 {
  scount *= 5
 } else {
  scount *= 10
 }
 //分给每个协程的大小
 si := downloadSize / scount
 fmt.Printf("文件总大小:%v, 分片数:%v,每个分片大小:%v\n", downloadSize, scount, si)
 //open copy file
 copyFile, err := os.OpenFile(copyFileName, os.O_CREATE|os.O_WRONLY, os.ModePerm)
 if err != nil {
  panic(err)
 }
 storgeFile, err := os.OpenFile(storgeFileName, os.O_CREATE|os.O_RDWR, os.ModePerm)
 if err != nil {
  panic(err)
 }
 defer copyFile.Close()
 
 var currentIndex int64 = 0
 wg := sync.WaitGroup{}
 fmt.Println("协程进度条")
 pgb := pgbar.New("")
 for ; currentIndex < scount; currentIndex++ {
  wg.Add(1)
  go func(current int64) {
   p := pgb.NewBar(fmt.Sprint((current+1))+"st", int(si))
   // p.SetSpeedSection(900, 100)
   b := make([]byte, 1024)
   bs := make([]byte, 16)
   currentIndex, _ := storgeFile.ReadAt(bs, current*16)
   //取出所有整数
   reg := regexp.MustCompile(`\d+`)
   countStr := reg.FindString(string(bs[:currentIndex]))
   total, _ := strconv.ParseInt(countStr, 10, 0)
   progressBar := 1
   for {
    if total >= si {
     wg.Done()
     break
    }
    //从指定位置开始读
    n, err := sfile.ReadAt(b, current*si+total)
    if err == io.EOF {
     wg.Done()
     break
    }
    //从指定位置开始写
    copyFile.WriteAt(b, current*si+total)
    storgeFile.WriteAt([]byte(strconv.FormatInt(total, 10)+" "), current*16)
    total += int64(n)
    if total >= si/10*int64(progressBar) {
     progressBar += 1
     p.Add(int(si / 10))
    }
 
   }
 
  }(currentIndex)
 }
 wg.Wait()
 storgeFile.Close()
 os.Remove(storgeFileName)
 fmt.Println("下载完成")
}

到此这篇关于golang实现多协程下载文件(支持断点续传)的文章就介绍到这了,更多相关golang 多协程下载文件内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/a1309525802/article/details/120814564

延伸 · 阅读

精彩推荐
  • Golanggolang 通过ssh代理连接mysql的操作

    golang 通过ssh代理连接mysql的操作

    这篇文章主要介绍了golang 通过ssh代理连接mysql的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    a165861639710342021-03-08
  • Golanggolang的httpserver优雅重启方法详解

    golang的httpserver优雅重启方法详解

    这篇文章主要给大家介绍了关于golang的httpserver优雅重启的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,...

    helight2992020-05-14
  • Golanggolang json.Marshal 特殊html字符被转义的解决方法

    golang json.Marshal 特殊html字符被转义的解决方法

    今天小编就为大家分享一篇golang json.Marshal 特殊html字符被转义的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    李浩的life12792020-05-27
  • GolangGolang中Bit数组的实现方式

    Golang中Bit数组的实现方式

    这篇文章主要介绍了Golang中Bit数组的实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    天易独尊11682021-06-09
  • Golanggo日志系统logrus显示文件和行号的操作

    go日志系统logrus显示文件和行号的操作

    这篇文章主要介绍了go日志系统logrus显示文件和行号的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    SmallQinYan12302021-02-02
  • Golanggolang如何使用struct的tag属性的详细介绍

    golang如何使用struct的tag属性的详细介绍

    这篇文章主要介绍了golang如何使用struct的tag属性的详细介绍,从例子说起,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看...

    Go语言中文网11352020-05-21
  • Golanggo语言制作端口扫描器

    go语言制作端口扫描器

    本文给大家分享的是使用go语言编写的TCP端口扫描器,可以选择IP范围,扫描的端口,以及多线程,有需要的小伙伴可以参考下。 ...

    脚本之家3642020-04-25
  • GolangGolang通脉之数据类型详情

    Golang通脉之数据类型详情

    这篇文章主要介绍了Golang通脉之数据类型,在编程语言中标识符就是定义的具有某种意义的词,比如变量名、常量名、函数名等等,Go语言中标识符允许由...

    4272021-11-24