创建+服务(通过HTTP).ZIP文件,而不写入磁盘?

I am wanting to serve a .ZIP file that is created on the fly, without having to write it to disk (I/O will slow down performance) and serve it to the client over HTTP.

Here is how I first attempted this:

func ZipServe(W http.ResponseWriter, R *http.Request) {

buf := new(bytes.Buffer)
writer := zip.NewWriter(buf)

// for the sake of this demonstration, this is the data I will zip
data := ioutil.ReadFile("randomfile.jpg")

f, err := writer.Create("randomfile.jpg")
if err != nil { 
    fmt.Println(err)
}

_, err = f.Write(data)
if err != nil {
    fmt.Println(err)
}

io.Copy(W, buf)

err := writer.Close()
if err != nil {
    fmt.Println(err)
}

}

This is no good, as the .ZIP ends up being corrupt after downloading. I suppose the issue has to do with io.Copy; shall I be using a different method?

I found interesting this and just for testing came up with this:

http://play.golang.org/p/JKAde2jbR3

package main

import (
    "archive/zip"
    "bytes"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

func zipHandler(w http.ResponseWriter, r *http.Request) {
    filename := "randomfile.jpg"
    buf := new(bytes.Buffer)
    writer := zip.NewWriter(buf)
    data, err := ioutil.ReadFile(filename)
    if err != nil {
        log.Fatal(err)
    }
    f, err := writer.Create(filename)
    if err != nil {
        log.Fatal(err)
    }
    _, err = f.Write([]byte(data))
    if err != nil {
        log.Fatal(err)
    }
    err = writer.Close()
    if err != nil {
        log.Fatal(err)
    }
    w.Header().Set("Content-Type", "application/zip")
    w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.zip\"", filename))
    //io.Copy(w, buf)
    w.Write(buf.Bytes())
}

func main() {
    http.HandleFunc("/zip", zipHandler)
    http.ListenAndServe(":8080", nil)
}

I just add some headers like the Content-Type and Content-Disposition.

Also instead of using io.Copy(w, buf) I am writing directly w.Write(buf.Bytes()) wondering if this is better? probably a more experienced user could clarify this.

Here's a slightly simpler method using io.Copy. It's probably not as performant as using a buffers for larger file sizes, but it works for me:

func handleZip(w http.ResponseWriter, r *http.Request) {
    f, err := os.Open("main.go")
    if err != nil {
        log.Fatal(err)
    }
    defer func() {
        if err := f.Close(); err != nil {
            log.Fatal(err)
        }
    }()

    // write straight to the http.ResponseWriter
    zw := zip.NewWriter(w)
    cf, err := zw.Create(f.Name())
    if err != nil {
        log.Fatal(err)
    }

    w.Header().Set("Content-Type", "application/zip")
    w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.zip\"", f.Name()))


    // copy the file contents to the zip Writer
    _, err = io.Copy(cf, f)
    if err != nil {
        log.Fatal(err)
    }

    // close the zip Writer to flush the contents to the ResponseWriter
    err = zw.Close()
    if err != nil {
        log.Fatal(err)
    }
}