Skip to content

Commit

Permalink
Merge pull request #8 from MCSManager/build/test
Browse files Browse the repository at this point in the history
Fix: linux os zip prefix
  • Loading branch information
zijiren233 authored Nov 27, 2022
2 parents 924f436 + c892cae commit d939cda
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
12 changes: 5 additions & 7 deletions utils/unzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,27 @@ func decode(files []*zip.File, targetPath string, types string) error {
}

func handleFile(f *zip.File, targetPath, decodeName string) error {
var err error
fpath := filepath.Join(targetPath, decodeName)
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, os.ModePerm)
return os.MkdirAll(fpath, os.ModePerm)
} else {
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
if err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return err
}
inFile, err := f.Open()
if err != nil {
return err
}
defer inFile.Close()
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer outFile.Close()
buf := bufio.NewWriter(outFile)
if _, err = io.Copy(buf, inFile); err != nil {
return err
}
buf.Flush()
inFile.Close()
outFile.Close()
return buf.Flush()
}
return err
}
30 changes: 11 additions & 19 deletions utils/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

// 示例 zip.Zip("MCSManager 9.4.5_win64_x86", "./test.zip") 可使用相对路径和绝对路径
func Zip(filePath []string, zipPath string) error {
var err error
if zipPath, err = filepath.Abs(zipPath); err != nil {
zipPath, err := filepath.Abs(zipPath)
if err != nil {
return err
}
if strings.ToLower(filepath.Ext(zipPath)) != ".zip" {
Expand All @@ -24,6 +24,7 @@ func Zip(filePath []string, zipPath string) error {
}
defer zipfile.Close()
buf := bufio.NewWriter(zipfile)
defer buf.Flush()
zw := zip.NewWriter(buf)
defer zw.Close()
for _, fPath := range filePath {
Expand All @@ -35,34 +36,25 @@ func Zip(filePath []string, zipPath string) error {
if err != nil {
return err
}
var zipfile io.Writer
if !strings.HasSuffix(filepath.Dir(fPath), `\`) {
fPath = filepath.Dir(fPath) + `\`
}
if info.IsDir() {
if !strings.HasSuffix(path, `\`) && !strings.HasSuffix(path, `/`) {
path = path + `/`
}
_, err = zw.Create(strings.TrimPrefix(path, fPath))
_, err = zw.Create(strings.TrimPrefix(strings.TrimPrefix(path, filepath.Dir(fPath)), string(os.PathSeparator)) + `/`)
return err
}
zipfile, err := zw.Create(strings.TrimPrefix(strings.TrimPrefix(path, filepath.Dir(fPath)), string(os.PathSeparator)))
if err != nil {
return err
} else {
zipfile, err = zw.Create(strings.TrimPrefix(path, fPath))
if err != nil {
return err
}
}
f1, err := os.Open(path)
if err != nil {
return err
}
io.Copy(zipfile, f1)
f1.Close()
return nil
defer f1.Close()
_, err = io.Copy(zipfile, f1)
return err
})
if err != nil {
return err
}
}
buf.Flush()
return err
}

0 comments on commit d939cda

Please sign in to comment.