Skip to content

Commit

Permalink
🚧 新增进度条显示逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
chunrichi committed Mar 3, 2023
1 parent fe66ed7 commit f6202df
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion backup4abap_go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func main() {
UzipTime := time.Now()

// 解压
err = util.DeCompressed(filename)
err = util.DeCompressed(filename, set.ProcessBar)
if err != nil {
fmt.Println(err)
return
Expand Down
48 changes: 48 additions & 0 deletions backup4abap_go/util/probar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package util

import (
"fmt"
"strings"
)

type Bar struct {
percent int64 // 百分比
curr int64
total int64
grap string
show string
IsInit bool
}

func (bar *Bar) Init(total int64, grap string) {
bar.IsInit = true
bar.total = total
if grap == "" {
bar.grap = "#"
}
bar.show = strings.Repeat(bar.grap, 100)
}

func (bar *Bar) percents() {
bar.percent = int64(float32(bar.curr) / float32(bar.total) * 100)
}

func (bar *Bar) Add(desc string) {
if !bar.IsInit {
return
}

bar.curr++

bar.percents()

fmt.Printf("\r[%-50s]%3d%% %8d/%d %-10s", bar.show[:bar.percent/2], bar.percent, bar.curr, bar.total, desc)
}

func (bar *Bar) End() {
if !bar.IsInit {
return
}

fmt.Println()
}
19 changes: 17 additions & 2 deletions backup4abap_go/util/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,39 @@ import (
"strings"
)

func DeCompressed(src string) error {
var probar Bar

func DeCompressed(src string, procBar bool) error {
ZipReader, err := zip.OpenReader(src)
if err != nil {
return err
}

defer ZipReader.Close()

fmt.Printf("解压数量:%6v\n", len(ZipReader.File))

if procBar {
probar.Init(int64(len(ZipReader.File)), "")
}

for _, f := range ZipReader.File {
if err := deCompressed(f); err != nil {
return err
}
}

probar.End()

return nil
}

func deCompressed(f *zip.File) error {
fmt.Println(f.Name)
if probar.IsInit {
probar.Add(f.Name)
} else {
fmt.Println(f.Name)
}

if f.FileInfo().IsDir() {
err := os.MkdirAll(f.Name, 0755)
Expand Down

0 comments on commit f6202df

Please sign in to comment.