package utils import ( "context" "crypto/md5" "encoding/hex" "github.com/mholt/archiver/v4" "io" "os" "path/filepath" "strings" ) func IsExist(path string) bool { _, err := os.Stat(path) if os.IsNotExist(err) { return false } return true } func TarXz(dst string, src string) error { src = filepath.Clean(src) dst = filepath.Clean(dst) if !strings.HasSuffix(src, string(os.PathSeparator)) { src += string(os.PathSeparator) } files, err := archiver.FilesFromDisk(nil, map[string]string{ src: "", }) if err != nil { return err } out, err := os.Create(dst) if err != nil { return err } defer out.Close() var compression archiver.Compression if strings.HasSuffix(dst, "gz") { compression = archiver.Gz{} } else if strings.HasSuffix(dst, "xz") { compression = archiver.Xz{} } else { compression = archiver.Gz{} } format := archiver.CompressedArchive{ Compression: compression, Archival: archiver.Tar{}, } err = format.Archive(context.Background(), out, files) if err != nil { return err } return nil } func CalcMd5(content string) string { pass := md5.Sum([]byte(content)) return hex.EncodeToString(pass[:]) } func CopyFile(src, dst string) (err error) { in, err := os.Open(src) if err != nil { return } defer in.Close() out, err := os.Create(dst) if err != nil { return } defer out.Close() _, err = io.Copy(out, in) return }