package main import( "io/ioutil" "os" "bytes" "archive/zip" ) func main(){ createZip(); } func createZip(){ content, err := ioutil.ReadFile("zzsxgm.xml") if err != nil{ panic(err) } buf := new(bytes.Buffer) w := zip.NewWriter(buf) f,err := w.Create("zzsxgm.xml") if err != nil{ panic(err) } _,err = f.Write(content) if err != nil{ panic(err) } err = w.Close() if err != nil{ panic(err) } f, err = os.OpenFile("test.zip", os.O_CREATE|os.O_WRONLY, 0666) if err != nil{ panic(err) } buf.WriteTo(f) }
参考:http://www.01happy.com/golang-zip/