package util import ( "crypto/sha256" "encoding/base64" "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 } // base64 encode // url safe func Bas64end(str string) string { // bdata:= return base64.URLEncoding.EncodeToString([]byte(str)) } // base64 url safe uneconde func Base64dec(bsstr string) string { dedc, _ := base64.URLEncoding.DecodeString(bsstr) return string(dedc) } // 计算文件的hash func CalacHash(rfile string) string { // 获取到真实地址 // file, err := os.Open(rfile) if err != nil { panic(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 }