fileutils.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package fileutils
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/sirupsen/logrus"
  10. )
  11. // 判断所给路径文件/文件夹是否存在
  12. func Exists(path string) bool {
  13. _, err := os.Stat(path) //os.Stat获取文件信息
  14. if err != nil {
  15. return os.IsExist(err)
  16. }
  17. return true
  18. }
  19. // 判断所给路径是否为文件夹
  20. func IsDir(path string) bool {
  21. s, err := os.Stat(path)
  22. if err != nil {
  23. return false
  24. }
  25. return s.IsDir()
  26. }
  27. // 判断所给路径是否为文件
  28. func IsFile(path string) bool {
  29. return !IsDir(path)
  30. }
  31. func CopyDir(srcPath string, destPath string) error {
  32. //检测目录正确性
  33. if srcInfo, err := os.Stat(srcPath); err != nil {
  34. logrus.Error(err)
  35. return err
  36. } else {
  37. if !srcInfo.IsDir() {
  38. e := errors.New("srcPath不是一个正确的目录!")
  39. logrus.Error(err)
  40. return e
  41. }
  42. }
  43. if destInfo, err := os.Stat(destPath); err != nil {
  44. logrus.Error(err)
  45. return err
  46. } else {
  47. if !destInfo.IsDir() {
  48. e := errors.New("destInfo不是一个正确的目录!")
  49. logrus.Error(e)
  50. return e
  51. }
  52. }
  53. err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
  54. if f == nil {
  55. return err
  56. }
  57. if !f.IsDir() {
  58. path := strings.Replace(path, "\\", "/", -1)
  59. destNewPath := strings.Replace(path, srcPath, destPath, -1)
  60. //fmt.Println("复制文件:" + path + " 到 " + destNewPath)
  61. copyFile(path, destNewPath)
  62. }
  63. return nil
  64. })
  65. if err != nil {
  66. logrus.Error(err)
  67. }
  68. return err
  69. }
  70. //生成目录并拷贝文件
  71. func copyFile(src, dest string) (w int64, err error) {
  72. srcFile, err := os.Open(src)
  73. if err != nil {
  74. fmt.Println(err.Error())
  75. return
  76. }
  77. defer srcFile.Close()
  78. //分割path目录
  79. destSplitPathDirs := strings.Split(dest, "/")
  80. //检测时候存在目录
  81. destSplitPath := ""
  82. for index, dir := range destSplitPathDirs {
  83. if index < len(destSplitPathDirs)-1 {
  84. destSplitPath = destSplitPath + dir + "/"
  85. b, _ := pathExists(destSplitPath)
  86. if !b {
  87. fmt.Println("创建目录:" + destSplitPath)
  88. //创建目录
  89. err := os.Mkdir(destSplitPath, os.ModePerm)
  90. if err != nil {
  91. logrus.Error(err)
  92. }
  93. }
  94. }
  95. }
  96. dstFile, err := os.Create(dest)
  97. if err != nil {
  98. logrus.Error(err)
  99. return
  100. }
  101. defer dstFile.Close()
  102. return io.Copy(dstFile, srcFile)
  103. }
  104. //检测文件夹路径时候存在
  105. func pathExists(path string) (bool, error) {
  106. _, err := os.Stat(path)
  107. if err == nil {
  108. return true, nil
  109. }
  110. if os.IsNotExist(err) {
  111. return false, nil
  112. }
  113. return false, err
  114. }