Browse Source

Fixed Reader.OnBlockDone not processing uncompressed blocks
Fixed uncompress cli command not displaying the progress bar

Pierre Curto 6 years ago
parent
commit
fea8a15e10
2 changed files with 21 additions and 3 deletions
  1. 18 3
      internal/cmds/uncompress.go
  2. 3 0
      reader.go

+ 18 - 3
internal/cmds/uncompress.go

@@ -2,6 +2,7 @@ package cmds
 
 import (
 	"flag"
+	"fmt"
 	"github.com/schollz/progressbar/v2"
 	"io"
 	"os"
@@ -47,15 +48,24 @@ func Uncompress(_ *flag.FlagSet) cmdflag.Handler {
 			if err != nil {
 				return err
 			}
-			var out io.Writer = file
-			if size := zfinfo.Size(); size > 0 {
-				out = progressbar.NewOptions(0,
+			var (
+				size  int
+				out   io.Writer = file
+				zsize           = zfinfo.Size()
+				bar   *progressbar.ProgressBar
+			)
+			if zsize > 0 {
+				bar = progressbar.NewOptions64(zsize,
 					// File transfers are usually slow, make sure we display the bar at 0%.
 					progressbar.OptionSetRenderBlankState(true),
 					// Display the filename.
 					progressbar.OptionSetDescription(filename),
 					progressbar.OptionClearOnFinish(),
 				)
+				out = io.MultiWriter(out, bar)
+				zr.OnBlockDone = func(n int) {
+					size += n
+				}
 			}
 
 			// Uncompress.
@@ -69,6 +79,11 @@ func Uncompress(_ *flag.FlagSet) cmdflag.Handler {
 					return err
 				}
 			}
+
+			if bar != nil {
+				_ = bar.Clear()
+				fmt.Printf("%s %d\n", zfilename, size)
+			}
 		}
 
 		return nil

+ 3 - 0
reader.go

@@ -214,6 +214,9 @@ func (z *Reader) Read(buf []byte) (int, error) {
 				return 0, err
 			}
 			z.pos += int64(bLen)
+			if z.OnBlockDone != nil {
+				z.OnBlockDone(int(bLen))
+			}
 
 			if z.BlockChecksum {
 				checksum, err := z.readUint32()