1 Star 0 Fork 2

XXFork/utils

forked from aesoper/utils 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
file.go 2.24 KB
一键复制 编辑 原始数据 按行查看 历史
aesoper 提交于 2020-07-05 15:59 . 添加读取文件函数
/**
* @Author: aesoper
* @Description:
* @File: file
* @Version: 1.0.0
* @Date: 2020/5/19 21:11
*/
package utils
import (
"errors"
"io/ioutil"
"os"
)
func FileExists(file string) bool {
_, err := os.Stat(file)
return err == nil || os.IsExist(err)
}
func FileIsDir(file string) bool {
f, err := os.Stat(file)
if err != nil {
return false
}
return f.IsDir()
}
func ReadFile(filename string) (string, error) {
body, err := ioutil.ReadFile(filename)
return string(body), err
}
func IsRegularFile(fi os.FileInfo) bool {
return fi.Mode()&(os.ModeDir|os.ModeSymlink|os.ModeNamedPipe|os.ModeSocket|os.ModeDevice) == 0
}
// 按分块数分块
func SplitFileByPartNum(fileSize int64, chunkNum int) ([]FileChunk, error) {
if chunkNum <= 0 || chunkNum > 10000 {
return nil, errors.New("chunkNum invalid")
}
if int64(chunkNum) > fileSize {
return nil, errors.New("oss: chunkNum invalid")
}
var chunks []FileChunk
var chunk = FileChunk{}
var chunkN = (int64)(chunkNum)
for i := int64(0); i < chunkN; i++ {
chunk.Number = int(i + 1)
chunk.Offset = i * (fileSize / chunkN)
if i == chunkN-1 {
chunk.Size = fileSize/chunkN + fileSize%chunkN
} else {
chunk.Size = fileSize / chunkN
}
chunks = append(chunks, chunk)
}
return chunks, nil
}
type FileChunk struct {
Number int // Chunk number
Offset int64 // Chunk offset
Size int64 // Chunk size.
}
// 按大小分块, 参考阿里云
func SplitFileByPartSize(fileSize int64, chunkSize int64) ([]FileChunk, error) {
if chunkSize <= 0 {
return nil, errors.New("chunkSize invalid")
}
if fileSize <= 0 {
return nil, errors.New("fileSize invalid")
}
if chunkSize > fileSize {
return nil, errors.New("chunkSize must less then fileSize")
}
var chunkN = fileSize / chunkSize
if chunkN >= 10000 {
return nil, errors.New("Too many parts, please increase part size ")
}
var chunks []FileChunk
var chunk = FileChunk{}
for i := int64(0); i < chunkN; i++ {
chunk.Number = int(i + 1)
chunk.Offset = i * chunkSize
chunk.Size = chunkSize
chunks = append(chunks, chunk)
}
if fileSize%chunkSize > 0 {
chunk.Number = len(chunks) + 1
chunk.Offset = int64(len(chunks)) * chunkSize
chunk.Size = fileSize % chunkSize
chunks = append(chunks, chunk)
}
return chunks, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/XXFork/utils.git
[email protected]:XXFork/utils.git
XXFork
utils
utils
master

搜索帮助