You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
starainrt/archive.go

59 lines
1.1 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package starainrt
import (
"archive/zip"
"errors"
"fmt"
"io"
"os"
)
// Unzip 读取位于src的zip文件并解压到dst文件夹中
// shell传入当前解压的文件名称
func Unzip(src, dst string, shell func(string)) error {
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, 0644)
if err != nil {
return nil
}
}
zipreader, err := zip.OpenReader(src)
if err != nil {
return err
}
defer zipreader.Close()
for _, v := range zipreader.File {
if v.FileInfo().IsDir() {
err := os.MkdirAll(dst+string(os.PathSeparator)+v.Name, 0644)
if err != nil {
fmt.Println(err)
}
continue
}
fp, err := v.Open()
if err != nil {
fmt.Println(err)
continue
}
go shell(v.Name)
fpdst, err := os.Create(dst + string(os.PathSeparator) + v.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
}