file.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package files
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "sort"
  7. "strings"
  8. )
  9. // 按文件名排序,可扩展至文件时间
  10. type byName []os.FileInfo
  11. //func (f byName) Less(i, j int) bool { return f[i].Name() < f[j].Name() } // 文件名升序,默认方式
  12. func (f byName) Less(i, j int) bool { return f[i].Name() > f[j].Name() } // 文件名倒序
  13. func (f byName) Len() int { return len(f) }
  14. func (f byName) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
  15. // GetFileListBySuffix returns an ordered list of file paths.
  16. // It recognize if given path is a file, and don't do recursive find.
  17. func GetFileListBySuffix(dirPath, suffix string, needDir bool, isDescend bool, num int) ([]string, error) {
  18. if !IsExist(dirPath) {
  19. return nil, fmt.Errorf("given path does not exist: %s", dirPath)
  20. } else if IsFile(dirPath) {
  21. return []string{dirPath}, nil
  22. }
  23. // Given path is a directory.
  24. dir, err := os.Open(dirPath)
  25. if err != nil {
  26. return nil, err
  27. }
  28. fis, err := dir.Readdir(0)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if isDescend {
  33. sort.Sort(byName(fis))
  34. }
  35. if num == 0 {
  36. num = len(fis)
  37. }
  38. files := make([]string, 0, num)
  39. for i := 0; i < num; i++ {
  40. fi := fis[i]
  41. if strings.HasSuffix(fi.Name(), suffix) {
  42. if needDir {
  43. files = append(files, filepath.Join(dirPath, fi.Name()))
  44. } else {
  45. files = append(files, fi.Name())
  46. }
  47. }
  48. }
  49. return files, nil
  50. }
  51. // as GetFileListBySuffix, but for Prefix
  52. func GetFileListByPrefix(dirPath, suffix string, needDir bool, isDescend bool, num int) ([]string, error) {
  53. if !IsExist(dirPath) {
  54. return nil, fmt.Errorf("given path does not exist: %s", dirPath)
  55. } else if IsFile(dirPath) {
  56. return []string{dirPath}, nil
  57. }
  58. // Given path is a directory.
  59. dir, err := os.Open(dirPath)
  60. if err != nil {
  61. return nil, err
  62. }
  63. fis, err := dir.Readdir(0)
  64. if err != nil {
  65. return nil, err
  66. }
  67. if isDescend {
  68. sort.Sort(byName(fis))
  69. }
  70. if num == 0 {
  71. num = len(fis)
  72. }
  73. files := make([]string, 0, num)
  74. for i := 0; i < num; i++ {
  75. fi := fis[i]
  76. if strings.HasPrefix(fi.Name(), suffix) {
  77. if needDir {
  78. files = append(files, filepath.Join(dirPath, fi.Name()))
  79. } else {
  80. files = append(files, fi.Name())
  81. }
  82. }
  83. }
  84. return files, nil
  85. }
  86. // 根据关键字查找
  87. func GetFileListByKey(dirPath, key string, needDir bool, isDescend bool, num int) ([]string, error) {
  88. if !IsExist(dirPath) {
  89. return nil, fmt.Errorf("given path does not exist: %s", dirPath)
  90. } else if IsFile(dirPath) {
  91. return []string{dirPath}, nil
  92. }
  93. // Given path is a directory.
  94. dir, err := os.Open(dirPath)
  95. if err != nil {
  96. return nil, err
  97. }
  98. fis, err := dir.Readdir(0)
  99. if err != nil {
  100. return nil, err
  101. }
  102. if isDescend {
  103. sort.Sort(byName(fis))
  104. }
  105. if num == 0 {
  106. num = len(fis)
  107. }
  108. files := make([]string, 0, num)
  109. for i := 0; i < num; i++ {
  110. fi := fis[i]
  111. if strings.Contains(fi.Name(), key) {
  112. if needDir {
  113. files = append(files, filepath.Join(dirPath, fi.Name()))
  114. } else {
  115. files = append(files, fi.Name())
  116. }
  117. }
  118. }
  119. return files, nil
  120. }
  121. func IsDir(path string) bool {
  122. s, err := os.Stat(path)
  123. if err != nil {
  124. return false
  125. }
  126. return s.IsDir()
  127. }
  128. func IsFile(path string) bool {
  129. return !IsDir(path)
  130. }
  131. func IsExist(path string) bool {
  132. _, err := os.Stat(path)
  133. if err != nil {
  134. if os.IsExist(err) {
  135. return true
  136. }
  137. return false
  138. }
  139. return true
  140. }