Skip to content

Instantly share code, notes, and snippets.

@sathishvj
Created July 20, 2015 16:31
Show Gist options
  • Save sathishvj/81fc88297df63a8beb54 to your computer and use it in GitHub Desktop.
Save sathishvj/81fc88297df63a8beb54 to your computer and use it in GitHub Desktop.
import (
"archive/zip"
"bytes"
"fmt"
"io/ioutil"
"path"
)
func zipfiles(zippath string, files []string) error {
var allB bytes.Buffer
w := zip.NewWriter(&allB)
for _, f := range files {
zipfile, err := w.Create(path.Base(f)) // add only the basename of the input file
b, err := ioutil.ReadFile(f)
if err != nil {
fmt.Printf("zipfiles(): could not read file %s: %+v\n", f, err)
continue
}
zipfile.Write(b)
}
w.Close() // You must close this first to flush the bytes to the buffer.
err := ioutil.WriteFile(zippath, allB.Bytes(), 0666)
if err != nil {
fmt.Printf("zipfiles(): could not write zip file %s: %+v\n", zippath, err)
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment