Skip to content

Commit

Permalink
feat: 避免压缩包多一层目录
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 27, 2024
1 parent 86bac09 commit 9a8e2b2
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions pkg/io/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"os/exec"
"path/filepath"
"strings"
)

type FormatArchive string
Expand All @@ -20,11 +21,24 @@ const (

// Compress 压缩文件
func Compress(src []string, dst string, format FormatArchive) error {
var cmd *exec.Cmd
if len(src) == 0 {
return errors.New("source is empty")
}

cmd := new(exec.Cmd)
cmd.Dir = filepath.Dir(src[0])

// 取相对路径,避免压缩包内多一层目录
for i, item := range src {
if !strings.HasPrefix(item, cmd.Dir) {
continue
}
src[i] = filepath.Base(item)
}

switch format {
case Zip:
cmd = exec.Command("zip", append([]string{"-r", "-o", dst}, src...)...)
cmd = exec.Command("zip", append([]string{"-qr", "-o", dst}, src...)...)
case Gz:
cmd = exec.Command("tar", append([]string{"-czf", dst}, src...)...)
case Bz2:
Expand All @@ -50,7 +64,7 @@ func UnCompress(src string, dst string, format FormatArchive) error {

switch format {
case Zip:
cmd = exec.Command("unzip", "-o", src, "-d", dst)
cmd = exec.Command("unzip", "-qo", src, "-d", dst)
case Gz:
cmd = exec.Command("tar", "-xzf", src, "-C", dst)
case Bz2:
Expand All @@ -62,7 +76,7 @@ func UnCompress(src string, dst string, format FormatArchive) error {
case Xz:
cmd = exec.Command("tar", "-xJf", src, "-C", dst)
case SevenZip:
cmd = exec.Command("7z", "x", "-y", src, "-o"+dst)
cmd = exec.Command("7z", "x", "-y", src, "-o", dst)
default:
return errors.New("unsupported format")
}
Expand Down

0 comments on commit 9a8e2b2

Please sign in to comment.