fileutils.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package fileutils
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "io"
  6. "log"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. )
  11. func PathExists(path string) bool {
  12. _, err := os.Stat(path)
  13. if err == nil {
  14. return true
  15. }
  16. if os.IsNotExist(err) {
  17. return false
  18. }
  19. return false
  20. }
  21. func GetDirFiles(dir string) ([]string, error) {
  22. dirList, err := os.ReadDir(dir)
  23. if err != nil {
  24. return nil, err
  25. }
  26. filesRet := make([]string, 0)
  27. for _, file := range dirList {
  28. if file.IsDir() {
  29. files, err := GetDirFiles(filepath.Join(dir, file.Name()))
  30. if err != nil {
  31. return nil, err
  32. }
  33. filesRet = append(filesRet, files...)
  34. } else {
  35. filesRet = append(filesRet, filepath.Join(dir, file.Name()))
  36. }
  37. }
  38. return filesRet, nil
  39. }
  40. func ZipDir(dirPath string, savePath string) error {
  41. archive, err := os.Create(savePath)
  42. if err != nil {
  43. return err
  44. }
  45. defer func(archive *os.File) {
  46. err := archive.Close()
  47. if err != nil {
  48. log.Println(err)
  49. return
  50. }
  51. }(archive)
  52. zipWriter := zip.NewWriter(archive)
  53. defer func(zipWriter *zip.Writer) {
  54. err := zipWriter.Close()
  55. if err != nil {
  56. log.Println(err)
  57. return
  58. }
  59. }(zipWriter)
  60. err = filepath.Walk(dirPath,
  61. func(path string, f os.FileInfo, err error) error {
  62. if err != nil {
  63. return err
  64. }
  65. if f.IsDir() {
  66. return nil
  67. }
  68. zipPath := strings.TrimPrefix(path, dirPath)
  69. err = writeZipFile(path, zipPath, zipWriter)
  70. if err != nil {
  71. return err
  72. }
  73. return nil
  74. })
  75. if err != nil {
  76. return err
  77. }
  78. return nil
  79. }
  80. func UnzipFile(srcFilePath string, destDir string) error {
  81. srcFile, err := os.Open(srcFilePath)
  82. if err != nil {
  83. return err
  84. }
  85. defer func() {
  86. err := srcFile.Close()
  87. if err != nil {
  88. log.Println(err)
  89. return
  90. }
  91. }()
  92. srcFileInfo, err := srcFile.Stat()
  93. if err != nil {
  94. return err
  95. }
  96. return unzip(srcFile, srcFileInfo.Size(), destDir)
  97. }
  98. func UnzipBytes(zipFileBytes []byte, destDir string) error {
  99. return unzip(bytes.NewReader(zipFileBytes), int64(len(zipFileBytes)), destDir)
  100. }
  101. func writeZipFile(filePath string, zipPath string, zipWriter *zip.Writer) error {
  102. file, err := os.Open(filePath)
  103. if err != nil {
  104. return err
  105. }
  106. defer func() {
  107. err := file.Close()
  108. if err != nil {
  109. log.Println(err)
  110. return
  111. }
  112. }()
  113. writer, err := zipWriter.Create(zipPath)
  114. if err != nil {
  115. return err
  116. }
  117. _, err = io.Copy(writer, file)
  118. if err != nil {
  119. return err
  120. }
  121. return nil
  122. }
  123. func unzip(r io.ReaderAt, size int64, destDir string) error {
  124. zipReader, err := zip.NewReader(r, size)
  125. if err != nil {
  126. return err
  127. }
  128. for _, f := range zipReader.File {
  129. fPath := filepath.Join(destDir, f.Name)
  130. if f.FileInfo().IsDir() {
  131. if err := os.MkdirAll(fPath, os.ModePerm); err != nil {
  132. return err
  133. }
  134. } else {
  135. if err := os.MkdirAll(filepath.Dir(fPath), os.ModePerm); err != nil {
  136. return err
  137. }
  138. inFile, err := f.Open()
  139. if err != nil {
  140. return err
  141. }
  142. outFile, err := os.OpenFile(fPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
  143. if err != nil {
  144. return err
  145. }
  146. if _, err = io.Copy(outFile, inFile); err != nil {
  147. return err
  148. }
  149. if err = inFile.Close(); err != nil {
  150. return err
  151. }
  152. if err = outFile.Close(); err != nil {
  153. return err
  154. }
  155. }
  156. }
  157. return nil
  158. }