|
| 1 | +package File |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "xiaowumin-SFM/Struct" |
| 11 | +) |
| 12 | + |
| 13 | +func CopyFile(date Struct.SandCopyFile) error { |
| 14 | + if date.Path[len(date.Path)-1] != '/' { |
| 15 | + date.Path += "/" |
| 16 | + } |
| 17 | + if date.ToPath[len(date.ToPath)-1] != '/' { |
| 18 | + date.ToPath += "/" |
| 19 | + } |
| 20 | + fmt.Println(date) |
| 21 | + for i := 0; i < len(date.CopyName); i++ { |
| 22 | + if date.CopyName[i].IsDir { |
| 23 | + var content string |
| 24 | + if date.ToPath[len(date.ToPath)-1] == '/' { |
| 25 | + content = date.ToPath[:len(date.ToPath)-1] |
| 26 | + } |
| 27 | + fmt.Println("[dir]path:", date.Path+date.CopyName[i].Name, "topath:", content) |
| 28 | + err := CopyDir(date.Path+date.CopyName[i].Name, content) |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + content = "" |
| 33 | + } else { |
| 34 | + var content string |
| 35 | + if date.ToPath[len(date.ToPath)-1] != '/' { |
| 36 | + content = date.ToPath + "/" |
| 37 | + } |
| 38 | + fmt.Println("[file]path:", date.Path+date.CopyName[i].Name, "topath:", content) |
| 39 | + err := copyFile(date.Path+date.CopyName[i].Name, content, 0) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + content = "" |
| 44 | + } |
| 45 | + } |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +// 使用os.Read()和os.Write() |
| 50 | +func copyFile(src, des string, bufSize int) (err error) { |
| 51 | + if bufSize <= 0 { |
| 52 | + bufSize = 1 * 1024 * 1024 //1M |
| 53 | + } |
| 54 | + buf := make([]byte, bufSize) |
| 55 | + |
| 56 | + srcFile, err := os.Open(src) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + defer srcFile.Close() |
| 61 | + |
| 62 | + //获取源文件的权限 |
| 63 | + fi, _ := srcFile.Stat() |
| 64 | + perm := fi.Mode() |
| 65 | + |
| 66 | + desFile, err := os.OpenFile(des, os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + defer desFile.Close() |
| 71 | + |
| 72 | + count := 0 |
| 73 | + for { |
| 74 | + n, err := srcFile.Read(buf) |
| 75 | + if err != nil && err != io.EOF { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + if n == 0 { |
| 80 | + break |
| 81 | + } |
| 82 | + |
| 83 | + if wn, err := desFile.Write(buf[:n]); err != nil { |
| 84 | + return err |
| 85 | + } else { |
| 86 | + count += wn |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +func CopyDir(srcPath, desPath string) error { |
| 94 | + //检查目录是否正确 |
| 95 | + if srcInfo, err := os.Stat(srcPath); err != nil { |
| 96 | + return err |
| 97 | + } else { |
| 98 | + if !srcInfo.IsDir() { |
| 99 | + return errors.New("源路径不是一个正确的目录!") |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if desInfo, err := os.Stat(desPath); err != nil { |
| 104 | + return err |
| 105 | + } else { |
| 106 | + if !desInfo.IsDir() { |
| 107 | + return errors.New("目标路径不是一个正确的目录!") |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if strings.TrimSpace(srcPath) == strings.TrimSpace(desPath) { |
| 112 | + return errors.New("源路径与目标路径不能相同!") |
| 113 | + } |
| 114 | + |
| 115 | + err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error { |
| 116 | + if f == nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + //复制目录是将源目录中的子目录复制到目标路径中,不包含源目录本身 |
| 121 | + if path == srcPath { |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + //生成新路径 |
| 126 | + destNewPath := strings.Replace(path, srcPath, desPath, -1) |
| 127 | + |
| 128 | + if !f.IsDir() { |
| 129 | + copyFile(path, destNewPath, 0) |
| 130 | + } else { |
| 131 | + if !FileIsExisted(destNewPath) { |
| 132 | + return MakeDir(destNewPath) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return nil |
| 137 | + }) |
| 138 | + |
| 139 | + return err |
| 140 | +} |
| 141 | + |
| 142 | +func FileIsExisted(filename string) bool { |
| 143 | + existed := true |
| 144 | + if _, err := os.Stat(filename); os.IsNotExist(err) { |
| 145 | + existed = false |
| 146 | + } |
| 147 | + return existed |
| 148 | +} |
| 149 | + |
| 150 | +func MakeDir(dir string) error { |
| 151 | + if !FileIsExisted(dir) { |
| 152 | + if err := os.MkdirAll(dir, 0777); err != nil { //os.ModePerm |
| 153 | + fmt.Println("MakeDir failed:", err) |
| 154 | + return err |
| 155 | + } |
| 156 | + } |
| 157 | + return nil |
| 158 | +} |
0 commit comments