zip.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package utils
  2. import (
  3. "archive/zip"
  4. "fmt"
  5. "io"
  6. "io/fs"
  7. "log"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. )
  13. func Zip(zipPath string, paths ...string) error {
  14. // Create zip file and it's parent dir.
  15. if err := os.MkdirAll(filepath.Dir(zipPath), os.ModePerm); err != nil {
  16. return err
  17. }
  18. archive, err := os.Create(zipPath)
  19. if err != nil {
  20. return err
  21. }
  22. defer archive.Close()
  23. // New zip writer.
  24. zipWriter := zip.NewWriter(archive)
  25. defer zipWriter.Close()
  26. // Traverse the file or directory.
  27. for _, rootPath := range paths {
  28. // Remove the trailing path separator if path is a directory.
  29. rootPath = strings.TrimSuffix(rootPath, string(os.PathSeparator))
  30. // Visit all the files or directories in the tree.
  31. err = filepath.Walk(rootPath, walkFunc(rootPath, zipWriter))
  32. if err != nil {
  33. return err
  34. }
  35. }
  36. return nil
  37. }
  38. func walkFunc(rootPath string, zipWriter *zip.Writer) filepath.WalkFunc {
  39. return func(path string, info fs.FileInfo, err error) error {
  40. if err != nil {
  41. return err
  42. }
  43. // If a file is a symbolic link it will be skipped.
  44. if info.Mode()&os.ModeSymlink != 0 {
  45. return nil
  46. }
  47. // Create a local file header.
  48. header, err := zip.FileInfoHeader(info)
  49. if err != nil {
  50. return err
  51. }
  52. // Set compression method.
  53. header.Method = zip.Deflate
  54. // Set relative path of a file as the header name.
  55. header.Name, err = filepath.Rel(rootPath, path)
  56. if err != nil {
  57. return err
  58. }
  59. if info.IsDir() {
  60. header.Name += string(os.PathSeparator)
  61. }
  62. // Create writer for the file header and save content of the file.
  63. headerWriter, err := zipWriter.CreateHeader(header)
  64. if err != nil {
  65. return err
  66. }
  67. if info.IsDir() {
  68. return nil
  69. }
  70. fmt.Println(header.Name)
  71. f, err := os.Open(path)
  72. if err != nil {
  73. return err
  74. }
  75. defer f.Close()
  76. _, err = io.Copy(headerWriter, f)
  77. return err
  78. }
  79. }
  80. func Unzip(zipPath, dstDir string, cb func(per1000 int)) error {
  81. // open zip file
  82. reader, err := zip.OpenReader(zipPath)
  83. if err != nil {
  84. return err
  85. }
  86. defer reader.Close()
  87. total := len(reader.File)
  88. for index, file := range reader.File {
  89. per1000 := int(float32(index+1) / float32(total) * 1000)
  90. // log.Println("unzip=>", file.Name, index, "/", len(reader.File))
  91. log.Println("unzip=>", file.Name)
  92. if cb != nil {
  93. cb(per1000)
  94. }
  95. if err := unzipFile(file, dstDir); err != nil {
  96. return err
  97. }
  98. }
  99. return nil
  100. }
  101. func unzipFile(file *zip.File, dstDir string) error {
  102. // create the directory of file
  103. filePath := path.Join(dstDir, file.Name)
  104. if file.FileInfo().IsDir() {
  105. if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
  106. return err
  107. }
  108. return nil
  109. }
  110. if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
  111. return err
  112. }
  113. // open the file
  114. rc, err := file.Open()
  115. if err != nil {
  116. return err
  117. }
  118. defer rc.Close()
  119. // create the file
  120. w, err := os.Create(filePath)
  121. if err != nil {
  122. return err
  123. }
  124. defer w.Close()
  125. // save the decompressed file content
  126. _, err = io.Copy(w, rc)
  127. return err
  128. }