package main import ( "archive/zip" "fmt" "io" "os" "path/filepath" "strings" ) func main() { var ( Path = os.Args[1] Name = os.Args[2] ) File, _ := os.Create(Name) PS := strings.Split(Path, "\\") PathName := strings.Join(PS[:len(PS)-1], "\\") os.Chdir(PathName) Path = PS[len(PS)-1] defer File.Close() Zip := zip.NewWriter(File) defer Zip.Close() walk := func(Path string, info os.FileInfo, err error) error { if err != nil { fmt.Println(err) return err } if info.IsDir() { return nil } Src, _ := os.Open(Path) defer Src.Close() //FileName, _ := Zip.Create(Path) h := &zip.FileHeader{Name: Path, Method: zip.Deflate, Flags: 0x800} FileName, _ := Zip.CreateHeader(h) io.Copy(FileName, Src) Zip.Flush() return nil } if err := filepath.Walk(Path, walk); err != nil { fmt.Println(err) } }
有疑问加站长微信联系(非本文作者)