utils.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. )
  12. func GetCurrentPath() (string, error) {
  13. file, err := exec.LookPath(os.Args[0])
  14. if err != nil {
  15. return "", err
  16. }
  17. path, err := filepath.Abs(file)
  18. if err != nil {
  19. return "", err
  20. }
  21. //fmt.Println("path111:", path)
  22. if runtime.GOOS == "windows" {
  23. path = strings.Replace(path, "\\", "/", -1)
  24. }
  25. //fmt.Println("path222:", path)
  26. i := strings.LastIndex(path, "/")
  27. if i < 0 {
  28. return "", errors.New(`Can't find "/" or "\".`)
  29. }
  30. //fmt.Println("path333:", path)
  31. return string(path[0 : i+1]), nil
  32. }
  33. /**
  34. * 拷贝文件夹,同时拷贝文件夹中的文件
  35. * @param srcPath 需要拷贝的文件夹路径: D:/test
  36. * @param destPath 拷贝到的位置: D:/backup/
  37. */
  38. func CopyDir(path ...string) error {
  39. if len(path) < 2 {
  40. return errors.New("args illegal")
  41. }
  42. srcPath := ""
  43. for i := 0; i < len(path)-1; i++ {
  44. srcPath1 := path[i]
  45. srcPath = strings.Replace(srcPath1, "\\", "/", -1)
  46. srcInfo, err := os.Stat(srcPath)
  47. //检测目录正确性
  48. if err != nil {
  49. srcPath = ""
  50. continue
  51. }
  52. if !srcInfo.IsDir() {
  53. srcPath = ""
  54. } else {
  55. break
  56. }
  57. }
  58. if srcPath == "" {
  59. err := fmt.Errorf("src path %v error", path[:len(path)-1])
  60. fmt.Println(err.Error())
  61. return err
  62. }
  63. destPath1 := ""
  64. if len(path) >= 2 {
  65. destPath1 = path[len(path)-1]
  66. }
  67. destPath := strings.Replace(destPath1, "\\", "/", -1)
  68. if destInfo, err := os.Stat(destPath); err != nil {
  69. fmt.Println(err.Error())
  70. return err
  71. } else {
  72. if !destInfo.IsDir() {
  73. e := errors.New("destInfo不是一个正确的目录!")
  74. fmt.Println(e.Error())
  75. return e
  76. }
  77. }
  78. //加上拷贝时间:不用可以去掉
  79. //destPath = destPath + "_" + time.Now().Format("20060102150405")
  80. err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
  81. if f == nil {
  82. return err
  83. }
  84. if !f.IsDir() {
  85. path := strings.Replace(path, "\\", "/", -1)
  86. destNewPath := strings.Replace(path, srcPath, destPath, -1)
  87. fmt.Println("复制文件:" + path + " 到 " + destNewPath)
  88. copyFile(path, destNewPath)
  89. }
  90. return nil
  91. })
  92. if err != nil {
  93. fmt.Printf(err.Error())
  94. }
  95. return err
  96. }
  97. //生成目录并拷贝文件
  98. func copyFile(src, dest string) (w int64, err error) {
  99. srcFile, err := os.Open(src)
  100. if err != nil {
  101. fmt.Println(err.Error())
  102. return
  103. }
  104. defer srcFile.Close()
  105. //分割path目录
  106. destSplitPathDirs := strings.Split(dest, "/")
  107. //检测时候存在目录
  108. destSplitPath := ""
  109. for index, dir := range destSplitPathDirs {
  110. if index < len(destSplitPathDirs)-1 {
  111. destSplitPath = destSplitPath + dir + "/"
  112. b, _ := pathExists(destSplitPath)
  113. if b == false {
  114. fmt.Println("创建目录:" + destSplitPath)
  115. //创建目录
  116. err := os.Mkdir(destSplitPath, os.ModePerm)
  117. if err != nil {
  118. fmt.Println(err)
  119. }
  120. }
  121. }
  122. }
  123. dstFile, err := os.Create(dest)
  124. if err != nil {
  125. fmt.Println(err.Error())
  126. return
  127. }
  128. defer dstFile.Close()
  129. return io.Copy(dstFile, srcFile)
  130. }
  131. //检测文件夹路径时候存在
  132. func pathExists(path string) (bool, error) {
  133. _, err := os.Stat(path)
  134. if err == nil {
  135. return true, nil
  136. }
  137. if os.IsNotExist(err) {
  138. return false, nil
  139. }
  140. return false, err
  141. }