在实际案例的操作过程中,我们可能会遇到“Golang怎样实现多协程下载文件,方法及代码是什么”这样的问题,那么我们该如何处理和解决这样的情况呢?这篇小编就给大家总结了一些方法,具有一定的借鉴价值,希望对大家有所帮助,接下来就让小编带领大家一起了解看看吧。

引言

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

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怎样实现多协程下载文件,方法及代码是什么的介绍,本文内容仅供参考,有需要的朋友可以借鉴了解看看,希望对大家学习或工作,想要了解更多欢迎关注群英网络,小编每天都会为大家更新不同的知识。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。