Created
May 17, 2016 08:20
-
-
Save johnsonz/ce42542ddf4bc696094e88090d84cf47 to your computer and use it in GitHub Desktop.
go使用archive/zip原生包解压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" | |
"fmt" | |
"io" | |
"os" | |
) | |
func main() { | |
if err := unzip(); err != nil { | |
fmt.Println(err) | |
} | |
} | |
func unzip() error { | |
or, err := zip.OpenReader(`unpack.zip`) | |
if err != nil { | |
return err | |
} | |
for _, file := range or.Reader.File { | |
if file.FileInfo().IsDir() { | |
if err := os.MkdirAll(file.Name, 0644); err != nil { | |
return err | |
} | |
continue | |
} | |
f, err := file.Open() | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
cfile, err := os.Create(file.Name) | |
if err != nil { | |
return err | |
} | |
if _, err := io.Copy(cfile, f); err != nil { | |
return err | |
} | |
cfile.Close() | |
} | |
or.Close() | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment