diff --git a/backup4abap_go/main.go b/backup4abap_go/main.go index 494f6de..0b469fb 100644 --- a/backup4abap_go/main.go +++ b/backup4abap_go/main.go @@ -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 diff --git a/backup4abap_go/util/probar.go b/backup4abap_go/util/probar.go new file mode 100644 index 0000000..c15ff9f --- /dev/null +++ b/backup4abap_go/util/probar.go @@ -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() +} diff --git a/backup4abap_go/util/zip.go b/backup4abap_go/util/zip.go index fcfd404..010ada2 100644 --- a/backup4abap_go/util/zip.go +++ b/backup4abap_go/util/zip.go @@ -8,7 +8,9 @@ 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 @@ -16,16 +18,29 @@ func DeCompressed(src string) error { 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)