fix OS X unzipping

pull/57/head
Henrique Dias 2016-02-25 19:22:40 +00:00
parent b873605bad
commit 6140e6ca1f
1 changed files with 48 additions and 23 deletions

View File

@ -195,38 +195,63 @@ func checkSHA256() {
fmt.Println("checked!")
}
func unzip(archive, target string) error {
reader, err := zip.OpenReader(archive)
func unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()
if err := os.MkdirAll(target, 0755); err != nil {
return err
os.MkdirAll(dest, 0755)
// Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()
path := filepath.Join(dest, f.Name)
if f.FileInfo().IsDir() {
os.MkdirAll(path, f.Mode())
} else {
if _, err := os.Stat(filepath.Dir(path)); os.IsNotExist(err) {
os.MkdirAll(filepath.Dir(path), 0755)
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
return nil
}
for _, file := range reader.File {
path := filepath.Join(target, file.Name)
if file.FileInfo().IsDir() {
os.MkdirAll(path, file.Mode())
continue
}
fileReader, err := file.Open()
for _, f := range r.File {
err := extractAndWriteFile(f)
if err != nil {
return err
}
defer fileReader.Close()
targetFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
if err != nil {
return err
}
defer targetFile.Close()
if _, err := io.Copy(targetFile, fileReader); err != nil {
return err
}
}
return nil