mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-03 12:20:28 +08:00
升级时如果unzip命令不存在,则使用自定义解压程序
This commit is contained in:
95
internal/utils/unzip.go
Normal file
95
internal/utils/unzip.go
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/zip"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Unzip struct {
|
||||||
|
zipFile string
|
||||||
|
targetDir string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUnzip(zipFile string, targetDir string) *Unzip {
|
||||||
|
return &Unzip{
|
||||||
|
zipFile: zipFile,
|
||||||
|
targetDir: targetDir,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Unzip) Run() error {
|
||||||
|
if len(this.zipFile) == 0 {
|
||||||
|
return errors.New("zip file should not be empty")
|
||||||
|
}
|
||||||
|
if len(this.targetDir) == 0 {
|
||||||
|
return errors.New("target dir should not be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
reader, err := zip.OpenReader(this.zipFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
_ = reader.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
for _, file := range reader.File {
|
||||||
|
var info = file.FileInfo()
|
||||||
|
var target = this.targetDir + "/" + file.Name
|
||||||
|
|
||||||
|
// 目录
|
||||||
|
if info.IsDir() {
|
||||||
|
stat, err := os.Stat(target)
|
||||||
|
if err != nil {
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
err = os.MkdirAll(target, info.Mode())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if !stat.IsDir() {
|
||||||
|
err = os.MkdirAll(target, info.Mode())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 文件
|
||||||
|
err = func(file *zip.File, target string) error {
|
||||||
|
fileReader, err := file.Open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = fileReader.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
// remove old
|
||||||
|
_ = os.Remove(target)
|
||||||
|
|
||||||
|
// create new
|
||||||
|
fileWriter, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, file.FileInfo().Mode())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = fileWriter.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
_, err = io.Copy(fileWriter, fileReader)
|
||||||
|
return err
|
||||||
|
}(file, target)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -88,10 +88,6 @@ func (this *UpgradeManager) Start() error {
|
|||||||
|
|
||||||
// 检查unzip
|
// 检查unzip
|
||||||
unzipExe, _ := exec.LookPath("unzip")
|
unzipExe, _ := exec.LookPath("unzip")
|
||||||
if len(unzipExe) == 0 {
|
|
||||||
// TODO install unzip automatically or pack with a static 'unzip' file
|
|
||||||
return errors.New("can not find 'unzip' command")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查cp
|
// 检查cp
|
||||||
cpExe, _ := exec.LookPath("cp")
|
cpExe, _ := exec.LookPath("cp")
|
||||||
@@ -232,12 +228,24 @@ func (this *UpgradeManager) Start() error {
|
|||||||
return fmt.Errorf("remove old dir '%s' failed: %w", unzipDir, err)
|
return fmt.Errorf("remove old dir '%s' failed: %w", unzipDir, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var unzipCmd = exec.Command(unzipExe, "-q", "-o", destFile, "-d", unzipDir)
|
|
||||||
var unzipStderr = &bytes.Buffer{}
|
if len(unzipExe) > 0 {
|
||||||
unzipCmd.Stderr = unzipStderr
|
var unzipCmd = exec.Command(unzipExe, "-q", "-o", destFile, "-d", unzipDir)
|
||||||
err = unzipCmd.Run()
|
var unzipStderr = &bytes.Buffer{}
|
||||||
if err != nil {
|
unzipCmd.Stderr = unzipStderr
|
||||||
return fmt.Errorf("unzip installation file failed: %w: %s", err, unzipStderr.String())
|
err = unzipCmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unzip installation file failed: %w: %s", err, unzipStderr.String())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var unzipCmd = &Unzip{
|
||||||
|
zipFile: destFile,
|
||||||
|
targetDir: unzipDir,
|
||||||
|
}
|
||||||
|
err = unzipCmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unzip installation file failed: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
installationFiles, err := filepath.Glob(unzipDir + "/edge-" + this.component + "/*")
|
installationFiles, err := filepath.Glob(unzipDir + "/edge-" + this.component + "/*")
|
||||||
|
|||||||
Reference in New Issue
Block a user