Created
July 20, 2015 16:31
-
-
Save sathishvj/81fc88297df63a8beb54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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