file.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. )
  7. func CopyFile(dstName, srcName string) (written int64, err error) {
  8. if dstName == srcName {
  9. return
  10. }
  11. src, err := os.Open(srcName)
  12. if err != nil {
  13. fmt.Println("copy file " + srcName + " error")
  14. return
  15. }
  16. defer src.Close()
  17. os.Remove(dstName)
  18. dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644)
  19. if err != nil {
  20. fmt.Println("open file " + dstName + " error")
  21. return
  22. }
  23. defer dst.Close()
  24. return io.Copy(dst, src)
  25. }
  26. func copyDir(srcPath string, destPath string) {
  27. os.MkdirAll(destPath, 0777)
  28. dir, err := os.ReadDir(srcPath)
  29. if err != nil {
  30. return
  31. }
  32. for _, fi := range dir {
  33. if fi.IsDir() { // 忽略目录
  34. outPath := destPath + PthSep + fi.Name()
  35. copyDir(srcPath+PthSep+fi.Name(), outPath)
  36. } else {
  37. srcFile := srcPath + PthSep + fi.Name()
  38. destFile := destPath + PthSep + fi.Name()
  39. CopyFile(destFile, srcFile)
  40. }
  41. }
  42. }
  43. func isFileEqual(file1 string, file2 string) bool {
  44. // 打开文件
  45. dat1, err1 := os.ReadFile(file1)
  46. if err1 != nil {
  47. return false
  48. }
  49. dat2, err2 := os.ReadFile(file2)
  50. if err2 != nil {
  51. return false
  52. }
  53. byteData1 := []byte(dat1)
  54. dataLen1 := len(byteData1)
  55. byteData2 := []byte(dat2)
  56. dataLen2 := len(byteData2)
  57. if dataLen1 != dataLen2 {
  58. return false
  59. }
  60. //fmt.Println(fmt.Errorf("EncodePicFile %s len = %d", srcFile, dataLen))
  61. for i := 0; i < dataLen1; i++ {
  62. if byteData1[i] != byteData2[i] {
  63. return false
  64. }
  65. }
  66. return true
  67. }
  68. func doCompareFolder(srcPath string, destPath string, outputPath string) int {
  69. diffCount := 0
  70. dir, err := os.ReadDir(srcPath)
  71. if err != nil {
  72. return diffCount
  73. }
  74. for _, fi := range dir {
  75. if fi.IsDir() { // 忽略目录
  76. diffCount += doCompareFolder(srcPath+PthSep+fi.Name(), destPath+PthSep+fi.Name(), outputPath+PthSep+fi.Name())
  77. } else {
  78. //fmt.Println(dirPth + PthSep + fi.Name())
  79. srcFile := srcPath + PthSep + fi.Name()
  80. destFile := destPath + PthSep + fi.Name()
  81. outFile := outputPath + PthSep + fi.Name()
  82. //func(src, dest, outName, outPath string) {
  83. if !isFileEqual(srcFile, destFile) {
  84. os.MkdirAll(outputPath, 0777)
  85. CopyFile(outFile, srcFile)
  86. diffCount++
  87. }
  88. //}(srcFile, destFile, outFile, outputPath)
  89. }
  90. }
  91. return diffCount
  92. }
  93. func isDeltaEmpty(srcPath string) bool {
  94. dir, err := os.ReadDir(srcPath)
  95. if err != nil {
  96. return true
  97. }
  98. fileCount := 0
  99. for _, fi := range dir {
  100. if fi.IsDir() { // 忽略目录
  101. return false
  102. } else {
  103. fileCount++
  104. }
  105. }
  106. return fileCount <= 2
  107. }
  108. func compareFolder(srcPath string, destPath string, outputPath string) {
  109. fmt.Printf("compareFolder %v:%v to %v\n", srcPath, destPath, outputPath)
  110. if doCompareFolder(srcPath, destPath, outputPath) <= 2 {
  111. // 只有版本文件不一样,删除目录
  112. fmt.Printf("Removing delta folder %v\n", outputPath)
  113. os.RemoveAll(outputPath)
  114. }
  115. }