|
|
|
|
package starainrt
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"archive/zip"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Unzip 读取位于src的zip文件,并解压到dst文件夹中
|
|
|
|
|
// shell传入当前解压的文件名称
|
|
|
|
|
func Unzip(src, dst string, shell func(string)) error {
|
|
|
|
|
dst, err := filepath.Abs(dst)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !IsFile(src) {
|
|
|
|
|
return errors.New(src + " Not Exists")
|
|
|
|
|
}
|
|
|
|
|
if Exists(dst) && !IsFolder(dst) {
|
|
|
|
|
return errors.New(dst + " Exists And Not A Folder")
|
|
|
|
|
}
|
|
|
|
|
if !Exists(dst) {
|
|
|
|
|
err := os.MkdirAll(dst, 0755)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
zipreader, err := zip.OpenReader(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer zipreader.Close()
|
|
|
|
|
for _, v := range zipreader.File {
|
|
|
|
|
name := strings.ReplaceAll(v.Name, "\\", string(os.PathSeparator))
|
|
|
|
|
if v.FileInfo().IsDir() {
|
|
|
|
|
err := os.MkdirAll(dst+string(os.PathSeparator)+name, 0755)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
fp, err := v.Open()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
go shell(name)
|
|
|
|
|
dir := filepath.Dir(dst + string(os.PathSeparator) + name)
|
|
|
|
|
if !Exists(dir) {
|
|
|
|
|
os.MkdirAll(dir, 0755)
|
|
|
|
|
}
|
|
|
|
|
fpdst, err := os.Create(dst + string(os.PathSeparator) + name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
_, err = io.Copy(fpdst, fp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
fp.Close()
|
|
|
|
|
fpdst.Close()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|