搜索
您的当前位置:首页正文

go 断点续传

来源:榕意旅游网
package main

import (
	"fmt"
	"os"
	"strconv"
	"io"
)

func main() {
	//文件目录
	srcFile:="E:\\企业级四层负载均衡lVS\\L100 - 企业级开源四层负载均衡解决方案-LVS(完整版) - 199元\\第1章 课程简介\\1-1 LVS导学视频.mp4"
	//拷贝目录
	destFile:="E:\\cpoy\\LVS第一章.mp4"
	//临时目录
	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()
	//1.读取临时文件中的数据,根据seek
	// Seek 设置文件指针偏移量
	file3.Seek(0,io.SeekStart)
	bs:=make([]byte,100,100)
	// read  读取 len(bs) 字节
	n1,err:=file3.Read(bs)
	fmt.Printf("读取字节大小%v\n",n1)

	countStr:=string(bs[:n1])
	fmt.Printf("转换为字符串%v\n",countStr)
	//count,_:=strconv.Atoi(countStr)
	count,_:=strconv.ParseInt(countStr,10,64)
	fmt.Printf("转换为INT64%v\n",count)
	//2. 设置读,写的偏移量
	file1.Seek(count,0)
	file2.Seek(count,0)
	data:=make([]byte,1024,1024)
	n2:=-1// 读取的数据量
	n3:=-1//写出的数据量
	total :=int(count)//读取的总量

	for{
		//3.读取数据
		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))


		//假装断电
		if total>8000000{
		panic("假装断电了。。。,假装的。。。")
		}
	}

}

因篇幅问题不能全部显示,请点此查看更多更全内容

Top