utils.go 2.7 KB

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