github.com/iikira/iikira-go-utils@v0.0.0-20230610031953-f2cb11cde33a/utils/checksum/file.go (about) 1 package checksum 2 3 import ( 4 "bytes" 5 "path/filepath" 6 ) 7 8 // EqualLengthMD5 检测md5和大小是否相同 9 func (lfm *LocalFileMeta) EqualLengthMD5(m *LocalFileMeta) bool { 10 if lfm.Length != m.Length { 11 return false 12 } 13 if bytes.Compare(lfm.MD5, m.MD5) != 0 { 14 return false 15 } 16 return true 17 } 18 19 // CompleteAbsPath 补齐绝对路径 20 func (lfm *LocalFileMeta) CompleteAbsPath() { 21 if filepath.IsAbs(lfm.Path) { 22 return 23 } 24 25 absPath, err := filepath.Abs(lfm.Path) 26 if err != nil { 27 return 28 } 29 30 lfm.Path = absPath 31 } 32 33 // GetFileSum 获取文件的大小, md5, 前256KB切片的 md5, crc32 34 func GetFileSum(localPath string, flag int) (lfc *LocalFileChecksum, err error) { 35 lfc = NewLocalFileChecksum(localPath, 256*1024) 36 defer lfc.Close() 37 38 err = lfc.OpenPath() 39 if err != nil { 40 return nil, err 41 } 42 43 err = lfc.Sum(flag) 44 if err != nil { 45 return nil, err 46 } 47 return lfc, nil 48 }