From 2fbddf3f3044a66560222fb2ac1dff8f3c260fd5 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Mon, 13 Mar 2023 13:38:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3zip=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=8E=8B=E7=BC=A9=E6=8A=A5=E9=94=99=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/utils/files/file_op.go | 65 +++++++++++++++++-- .../src/views/host/file-management/index.vue | 2 +- go.mod | 2 +- go.sum | 6 +- 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/backend/utils/files/file_op.go b/backend/utils/files/file_op.go index 94af994ce..ae450f8d9 100644 --- a/backend/utils/files/file_op.go +++ b/backend/utils/files/file_op.go @@ -1,6 +1,7 @@ package files import ( + "archive/zip" "bufio" "context" "encoding/json" @@ -12,6 +13,7 @@ import ( "path" "path/filepath" "strconv" + "strings" "sync" "time" @@ -379,7 +381,9 @@ func getFormat(cType CompressType) archiver.CompressedArchive { format.Compression = archiver.Gz{} format.Archival = archiver.Tar{} case Zip: - format.Archival = archiver.Zip{} + format.Archival = archiver.Zip{ + Compression: zip.Deflate, + } case Bz2: format.Compression = archiver.Bz2{} format.Archival = archiver.Tar{} @@ -413,9 +417,19 @@ func (f FileOp) Compress(srcRiles []string, dst string, name string, cType Compr return err } - err = format.Archive(context.Background(), out, files) - if err != nil { - return err + switch cType { + case Zip: + if err := ZipFile(files, out); err != nil { + _ = f.DeleteFile(dstFile) + return err + } + default: + err = format.Archive(context.Background(), out, files) + if err != nil { + _ = f.DeleteFile(dstFile) + return err + } + break } return nil } @@ -488,3 +502,46 @@ func (f FileOp) CopyAndBackup(src string) (string, error) { } return backupPath, nil } + +func ZipFile(files []archiver.File, dst afero.File) error { + zw := zip.NewWriter(dst) + defer zw.Close() + + for _, file := range files { + hdr, err := zip.FileInfoHeader(file) + if err != nil { + return err + } + hdr.Name = file.NameInArchive + if file.IsDir() { + if !strings.HasSuffix(hdr.Name, "/") { + hdr.Name += "/" + } + hdr.Method = zip.Store + } + w, err := zw.CreateHeader(hdr) + if err != nil { + return err + } + if file.IsDir() { + continue + } + + if file.LinkTarget != "" { + _, err = w.Write([]byte(filepath.ToSlash(file.LinkTarget))) + if err != nil { + return err + } + } else { + fileReader, err := file.Open() + if err != nil { + return err + } + _, err = io.Copy(w, fileReader) + if err != nil { + return err + } + } + } + return nil +} diff --git a/frontend/src/views/host/file-management/index.vue b/frontend/src/views/host/file-management/index.vue index fd92a42a8..c4003a505 100644 --- a/frontend/src/views/host/file-management/index.vue +++ b/frontend/src/views/host/file-management/index.vue @@ -183,7 +183,7 @@ let selects = ref([]); let req = reactive({ path: '/', expand: true, - showHidden: false, + showHidden: true, page: 1, pageSize: 100, search: '', diff --git a/go.mod b/go.mod index 5b950ba9b..993a4e97d 100644 --- a/go.mod +++ b/go.mod @@ -70,7 +70,7 @@ require ( github.com/distribution/distribution/v3 v3.0.0-20220725133111-4bf3547399eb // indirect github.com/docker/distribution v2.8.1+incompatible // indirect github.com/docker/go-units v0.5.0 // indirect - github.com/dsnet/compress v0.0.1 // indirect + github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/fatih/color v1.13.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect diff --git a/go.sum b/go.sum index 516e55ffb..81bf6db75 100644 --- a/go.sum +++ b/go.sum @@ -313,8 +313,8 @@ github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDD github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE= github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM= github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= -github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q= -github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo= +github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 h1:iFaUwBSo5Svw6L7HYpRu/0lE3e0BaElwnNO1qkNQxBY= +github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s= github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY= github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= @@ -955,7 +955,7 @@ github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljT github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= -github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8= +github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8= github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=