From 63a4653eb2af6de3f9eca57350f9c52f09174689 Mon Sep 17 00:00:00 2001 From: yanweidong Date: Thu, 21 Aug 2025 10:59:18 +0800 Subject: [PATCH] add CopyFile --- utils/file.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/utils/file.go b/utils/file.go index 21932ad..bcc47a8 100644 --- a/utils/file.go +++ b/utils/file.go @@ -2,6 +2,7 @@ package utils import ( "errors" + "fmt" "io" "os" "path/filepath" @@ -21,6 +22,32 @@ func StringToFile(path, content string) error { return nil } +// 文件复制 +func CopyFile(src, dst string) (int64, error) { + sourceFileStat, err := os.Stat(src) + if err != nil { + return 0, err + } + if !sourceFileStat.Mode().IsRegular() { + return 0, fmt.Errorf("%s is not a regular file", src) + } + + source, err := os.Open(src) + if err != nil { + return 0, err + } + defer source.Close() + + destination, err := os.Create(dst) + if err != nil { + return 0, err + } + defer destination.Close() + + nBytes, err := io.Copy(destination, source) + return nBytes, err +} + // 递归遍历文件夹 // rootDir: 文件夹根目录 // s: 存储文件名的切片