is there a easy way to unzip file with golang ?
right now my code is:
func Unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer r.Close()
for _, f := range r.File {
rc, err := f.Open()
if err != nil {
return err
}
defer rc.Close()
path := filepath.Join(dest, f.Name)
if f.FileInfo().IsDir() {
os.MkdirAll(path, f.Mode())
} else {
f, err := os.OpenFile(
path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
}
return nil
}
forloop untilExtractexits. This will work fine for small numbers of files but if you had 1000s in the zip then you'll run out of file descriptors. Factor the contents of theforloop to a function or closure to fix. – Nick Craig-Wood Dec 4 '13 at 8:05os.MkdirAll(dest, 0755)right before your for loop. – Astockwell Jul 16 '14 at 23:28