Created
January 19, 2013 13:01
-
-
Save hnaohiro/4572580 to your computer and use it in GitHub Desktop.
Golangでzipファイルを解凍するサンプル
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
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