package main

import (
   "archive/zip"
   "io"
   "os"
   "path/Filepath"
   "strings"
)

//zip压缩
func Zip(srcFile string, destZip string) error {
   zipfile, err := os.Create(destZip)
   if err != nil {
       return err
   }
   defer zipfile.Close()

   archive := zip.NewWriter(zipfile)
   defer archive.Close()

   filepath.Walk(srcFile, func(path string, info os.FileInfo, err error) error {
       if err != nil {
           return err
       }

       Header, err := zip.FileInfoHeader(info)
       if err != nil {
           return err
       }

       header.Name = path
       if info.IsDir() {
           header.Name  = "/"
       } else {
           header.Method = zip.Deflate
       }

       writer, err := archive.CreateHeader(header)
       if err != nil {
           return err
       }

       if !info.IsDir() {
           file, err := os.Open(path)
           if err != nil {
               return err
           }
           defer file.Close()
           _, err = io.Copy(writer, file)
       }
       return err
   })

   return err
}

//zip解压
func Unzip(zipFile string, destDir string) error {
   zipReader, err := zip.OpenReader(zipFile)
   if err != nil {
       return err
   }
   defer zipReader.Close()

   for _, f := range zipReader.File {
       filePath := filepath.Join(destDir, f.Name)
       filenameList := strings.Split(f.Name, "_")
       filename := "./static/https_tcl/"   filenameList[0]   "_https_tcl"   filepath.Ext(f.Name)
       if f.FileInfo().IsDir() {
           os.MkdirAll(filePath, os.ModePerm)
       } else {
           if err = os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
               return err
           }

           inFile, err := f.Open()
           if err != nil {
               return err
           }
           defer inFile.Close()

           outFile, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
           if err != nil {
               return err
           }
           defer outFile.Close()

           _, err = io.Copy(outFile, inFile)
           if err != nil {
               return err
           }
       }
   }
   return nil
}

golang发送静态文件(golang实现zip的压缩和解压)

golang发送静态文件

package main

import (
   "archive/zip"
   "io"
   "os"
   "path/Filepath"
   "strings"
)

//zip压缩
func Zip(srcFile string, destZip string) error {
   zipfile, err := os.Create(destZip)
   if err != nil {
       return err
   }
   defer zipfile.Close()

   archive := zip.NewWriter(zipfile)
   defer archive.Close()

   filepath.Walk(srcFile, func(path string, info os.FileInfo, err error) error {
       if err != nil {
           return err
       }

       Header, err := zip.FileInfoHeader(info)
       if err != nil {
           return err
       }

       header.Name = path
       if info.IsDir() {
           header.Name  = "/"
       } else {
           header.Method = zip.Deflate
       }

       writer, err := archive.CreateHeader(header)
       if err != nil {
           return err
       }

       if !info.IsDir() {
           file, err := os.Open(path)
           if err != nil {
               return err
           }
           defer file.Close()
           _, err = io.Copy(writer, file)
       }
       return err
   })

   return err
}

//zip解压
func Unzip(zipFile string, destDir string) error {
   zipReader, err := zip.OpenReader(zipFile)
   if err != nil {
       return err
   }
   defer zipReader.Close()

   for _, f := range zipReader.File {
       filePath := filepath.Join(destDir, f.Name)
       filenameList := strings.Split(f.Name, "_")
       filename := "./static/https_tcl/"   filenameList[0]   "_https_tcl"   filepath.Ext(f.Name)
       if f.FileInfo().IsDir() {
           os.MkdirAll(filePath, os.ModePerm)
       } else {
           if err = os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
               return err
           }

           inFile, err := f.Open()
           if err != nil {
               return err
           }
           defer inFile.Close()

           outFile, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
           if err != nil {
               return err
           }
           defer outFile.Close()

           _, err = io.Copy(outFile, inFile)
           if err != nil {
               return err
           }
       }
   }
   return nil
}
,