Browse Source

获取文件MD5

marion 5 years ago
parent
commit
13cadf3e05
1 changed files with 30 additions and 0 deletions
  1. 30 0
      utils/file_utils.go

+ 30 - 0
utils/file_utils.go

@@ -0,0 +1,30 @@
+package utils
+
+import (
+	"crypto/md5"
+	"encoding/hex"
+	"io"
+	"os"
+	"strings"
+)
+
+// 获取文件MD5码
+func GetFileMD5(fileName string) (string, error) {
+	fileName = strings.TrimSpace(fileName)
+	if fileName == "" {
+		return "", nil
+	}
+	f, err := os.Open(fileName)
+	if err != nil {
+		return "", err
+	}
+	defer func(f *os.File) {
+		_ = f.Close()
+	}(f)
+	md5Encoder := md5.New()
+	_, err = io.Copy(md5Encoder, f)
+	if err != nil {
+		return "", err
+	}
+	return hex.EncodeToString(md5Encoder.Sum(nil)), nil
+}