UnZip do unzip
(zipArchive *zip.ReadCloser, destDir string)
| 31 | |
| 32 | // UnZip do unzip |
| 33 | func UnZip(zipArchive *zip.ReadCloser, destDir string) error { |
| 34 | return WalkZip(zipArchive, func(f *zip.File) error { |
| 35 | fpath := filepath.Join(destDir, f.Name) |
| 36 | if f.FileInfo().IsDir() { |
| 37 | _ = os.MkdirAll(fpath, os.ModePerm) |
| 38 | } else { |
| 39 | if err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil { |
| 40 | return err |
| 41 | } |
| 42 | |
| 43 | inFile, err := f.Open() |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | defer inFile.Close() |
| 48 | |
| 49 | outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | defer outFile.Close() |
| 54 | |
| 55 | _, err = io.Copy(outFile, inFile) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | } |
| 60 | return nil |
| 61 | }) |
| 62 | } |
| 63 | |
| 64 | // WalkZip walk in zip |
| 65 | func WalkZip(zipArchive *zip.ReadCloser, walkFunc func(file *zip.File) error) error { |
no test coverage detected