自动更新管控端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 lines
1.2 KiB

package util
import (
"crypto/sha256"
"fmt"
"io"
"os"
"path/filepath"
)
// 遍历目录下的文件
// 是否只扫相对路径下
func GetDirFilePaths(dirPath string, relativeOnly bool) ([]string, error) {
var filePaths []string
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
if relativeOnly {
fileName := filepath.Base(path)
relativePath := filepath.ToSlash(filepath.Join(filepath.Base(dirPath), fileName))
filePaths = append(filePaths, relativePath)
} else {
filePaths = append(filePaths, filepath.ToSlash(path))
}
}
return nil
})
if err != nil {
return nil, err
}
return filePaths, nil
}
// 计算文件的hash
func CalacHash(rfile string) string {
// 获取到真实地址
// rpath := filepath.Join(config.G.FilePath, rfile)
//
file, err := os.Open(rfile)
if err != nil {
// panic(err)
fmt.Printf("err %s\n", err)
}
defer file.Close()
// initlize hash object
hash := sha256.New()
// write file into hash object
if _, err := io.Copy(hash, file); err != nil {
panic(err)
}
// get hash value
hashBytes := hash.Sum(nil)
//converto to hash string
hastString := fmt.Sprintf("%x", hashBytes)
return hastString
}