Skip to content

Instantly share code, notes, and snippets.

@hnaohiro
Created January 19, 2013 13:01
Show Gist options
  • Save hnaohiro/4572580 to your computer and use it in GitHub Desktop.
Save hnaohiro/4572580 to your computer and use it in GitHub Desktop.
Golangでzipファイルを解凍するサンプル
package main
import (
"archive/zip"
"io"
"log"
"os"
"path/filepath"
)
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
}
func main() {
err := Unzip("./sample.zip", "./out")
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment