1. package ziptest
    2. import (
    3. "archive/zip"
    4. "io"
    5. "os"
    6. "strings"
    7. )
    8. //压缩文件
    9. //files 文件数组,可以是不同dir下的文件或者文件夹
    10. //dest 压缩文件存放地址
    11. func Compress(files []*os.File, dest string) error {
    12. d, _ := os.Create(dest)
    13. defer d.Close()
    14. w := zip.NewWriter(d)
    15. defer w.Close()
    16. for _, file := range files {
    17. err := compress(file, "", w)
    18. if err != nil {
    19. return err
    20. }
    21. }
    22. return nil
    23. }
    24. func compress(file *os.File, prefix string, zw *zip.Writer) error {
    25. info, err := file.Stat()
    26. if err != nil {
    27. return err
    28. }
    29. if info.IsDir() {
    30. prefix = prefix + "/" + info.Name()
    31. fileInfos, err := file.Readdir(-1)
    32. if err != nil {
    33. return err
    34. }
    35. for _, fi := range fileInfos {
    36. f, err := os.Open(file.Name() + "/" + fi.Name())
    37. if err != nil {
    38. return err
    39. }
    40. err = compress(f, prefix, zw)
    41. if err != nil {
    42. return err
    43. }
    44. }
    45. } else {
    46. header, err := zip.FileInfoHeader(info)
    47. header.Name = prefix + "/" + header.Name
    48. if err != nil {
    49. return err
    50. }
    51. writer, err := zw.CreateHeader(header)
    52. if err != nil {
    53. return err
    54. }
    55. _, err = io.Copy(writer, file)
    56. file.Close()
    57. if err != nil {
    58. return err
    59. }
    60. }
    61. return nil
    62. }
    63. //解压
    64. func DeCompress(zipFile, dest string) error {
    65. reader, err := zip.OpenReader(zipFile)
    66. if err != nil {
    67. return err
    68. }
    69. defer reader.Close()
    70. for _, file := range reader.File {
    71. rc, err := file.Open()
    72. if err != nil {
    73. return err
    74. }
    75. defer rc.Close()
    76. filename := dest + file.Name
    77. err = os.MkdirAll(getDir(filename), 0755)
    78. if err != nil {
    79. return err
    80. }
    81. w, err := os.Create(filename)
    82. if err != nil {
    83. return err
    84. }
    85. defer w.Close()
    86. _, err = io.Copy(w, rc)
    87. if err != nil {
    88. return err
    89. }
    90. w.Close()
    91. rc.Close()
    92. }
    93. return nil
    94. }
    95. func getDir(path string) string {
    96. return subString(path, 0, strings.LastIndex(path, "/"))
    97. }
    98. func subString(str string, start, end int) string {
    99. rs := []rune(str)
    100. length := len(rs)
    101. if start < 0 || start > length {
    102. panic("start is wrong")
    103. }
    104. if end < start || end > length {
    105. panic("end is wrong")
    106. }
    107. return string(rs[start:end])
    108. }

    测试代码:

    1. package ziptest
    2. import (
    3. "os"
    4. "testing"
    5. )
    6. func TestCompress(t *testing.T) {
    7. f1, err := os.Open("/home/zzw/test_data/ziptest/gophercolor16x16.png")
    8. if err != nil {
    9. t.Fatal(err)
    10. }
    11. defer f1.Close()
    12. f2, err := os.Open("/home/zzw/test_data/ziptest/readme.notzip")
    13. if err != nil {
    14. t.Fatal(err)
    15. }
    16. defer f2.Close()
    17. f3, err := os.Open("/home/zzw/test_data")
    18. if err != nil {
    19. t.Fatal(err)
    20. }
    21. defer f3.Close()
    22. var files = []*os.File{f1, f2, f3}
    23. dest := "/home/zzw/test_data/test.zip"
    24. err = Compress(files, dest)
    25. if err != nil {
    26. t.Fatal(err)
    27. }
    28. }
    29. func TestDeCompress(t *testing.T) {
    30. err := DeCompress("/home/zzw/test_data/test.zip", "/home/zzw/test_data/de")
    31. if err != nil {
    32. t.Fatal(err)
    33. }
    34. }

    golang zip压缩,解压(含目录文件) - 图1