From 9a8e2b2e3f2f0b3edd9684b544c26e356d226069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Mon, 28 Oct 2024 01:02:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=81=BF=E5=85=8D=E5=8E=8B=E7=BC=A9?= =?UTF-8?q?=E5=8C=85=E5=A4=9A=E4=B8=80=E5=B1=82=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/io/compress.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/pkg/io/compress.go b/pkg/io/compress.go index b05425408f..098cedc104 100644 --- a/pkg/io/compress.go +++ b/pkg/io/compress.go @@ -4,6 +4,7 @@ import ( "errors" "os/exec" "path/filepath" + "strings" ) type FormatArchive string @@ -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: @@ -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: @@ -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") }