filetil.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package filetil
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "io"
  7. "fmt"
  8. "math"
  9. )
  10. //==================================
  11. //更多文件和目录的操作,使用filepath包和os包
  12. //==================================
  13. //返回的目录扫描结果
  14. type FileList struct {
  15. IsDir bool //是否是目录
  16. Path string //文件路径
  17. Ext string //文件扩展名
  18. Name string //文件名
  19. Size int64 //文件大小
  20. ModTime int64 //文件修改时间戳
  21. }
  22. //目录扫描
  23. //@param dir 需要扫描的目录
  24. //@return fl 文件列表
  25. //@return err 错误
  26. func ScanFiles(dir string) (fl []FileList, err error) {
  27. err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  28. if err == nil {
  29. path = strings.Replace(path, "\\", "/", -1) //文件路径处理
  30. fl = append(fl, FileList{
  31. IsDir: info.IsDir(),
  32. Path: path,
  33. Ext: strings.ToLower(filepath.Ext(path)),
  34. Name: info.Name(),
  35. Size: info.Size(),
  36. ModTime: info.ModTime().Unix(),
  37. })
  38. }
  39. return err
  40. })
  41. return
  42. }
  43. //拷贝文件
  44. func CopyFile(source string, dst string) (err error) {
  45. sourceFile, err := os.Open(source)
  46. if err != nil {
  47. return err
  48. }
  49. defer sourceFile.Close()
  50. _,err = os.Stat(filepath.Dir(dst))
  51. if err != nil {
  52. if os.IsNotExist(err) {
  53. os.MkdirAll(filepath.Dir(dst),0766)
  54. }else{
  55. return err
  56. }
  57. }
  58. destFile, err := os.Create(dst)
  59. if err != nil {
  60. return err
  61. }
  62. defer destFile.Close()
  63. _, err = io.Copy(destFile, sourceFile)
  64. if err == nil {
  65. sourceInfo, err := os.Stat(source)
  66. if err != nil {
  67. err = os.Chmod(dst, sourceInfo.Mode())
  68. }
  69. }
  70. return
  71. }
  72. //拷贝目录
  73. func CopyDir(source string, dest string) (err error) {
  74. // get properties of source dir
  75. sourceInfo, err := os.Stat(source)
  76. if err != nil {
  77. return err
  78. }
  79. // create dest dir
  80. err = os.MkdirAll(dest, sourceInfo.Mode())
  81. if err != nil {
  82. return err
  83. }
  84. directory, _ := os.Open(source)
  85. objects, err := directory.Readdir(-1)
  86. for _, obj := range objects {
  87. sourceFilePointer := filepath.Join(source , obj.Name())
  88. destinationFilePointer := filepath.Join(dest, obj.Name())
  89. if obj.IsDir() {
  90. // create sub-directories - recursively
  91. err = CopyDir(sourceFilePointer, destinationFilePointer)
  92. if err != nil {
  93. fmt.Println(err)
  94. }
  95. } else {
  96. // perform copy
  97. err = CopyFile(sourceFilePointer, destinationFilePointer)
  98. if err != nil {
  99. fmt.Println(err)
  100. }
  101. }
  102. }
  103. return
  104. }
  105. func RemoveDir(dir string) error {
  106. return os.RemoveAll(dir)
  107. }
  108. func AbsolutePath(p string) (string, error) {
  109. if strings.HasPrefix(p, "~") {
  110. home := os.Getenv("HOME")
  111. if home == "" {
  112. panic(fmt.Sprintf("can not found HOME in envs, '%s' AbsPh Failed!", p))
  113. }
  114. p = fmt.Sprint(home, string(p[1:]))
  115. }
  116. s, err := filepath.Abs(p)
  117. if nil != err {
  118. return "", err
  119. }
  120. return s, nil
  121. }
  122. // FileExists reports whether the named file or directory exists.
  123. func FileExists(name string) bool {
  124. if _, err := os.Stat(name); err != nil {
  125. if os.IsNotExist(err) {
  126. return false
  127. }
  128. }
  129. return true
  130. }
  131. func FormatBytes(size int64) string {
  132. units := []string{" B", " KB", " MB", " GB", " TB"}
  133. s := float64(size)
  134. i := 0
  135. for ; s >= 1024 && i < 4; i++ {
  136. s /= 1024
  137. }
  138. return fmt.Sprintf("%.2f%s", s, units[i])
  139. }
  140. func Round(val float64, places int) float64 {
  141. var t float64
  142. f := math.Pow10(places)
  143. x := val * f
  144. if math.IsInf(x, 0) || math.IsNaN(x) {
  145. return val
  146. }
  147. if x >= 0.0 {
  148. t = math.Ceil(x)
  149. if (t - x) > 0.50000000001 {
  150. t -= 1.0
  151. }
  152. } else {
  153. t = math.Ceil(-x)
  154. if (t + x) > 0.50000000001 {
  155. t -= 1.0
  156. }
  157. t = -t
  158. }
  159. x = t / f
  160. if !math.IsInf(x, 0) {
  161. return x
  162. }
  163. return t
  164. }