-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathunsz.go
93 lines (71 loc) · 1.91 KB
/
unsz.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package unsz
import (
"time"
"github.com/itchio/savior"
"github.com/itchio/butler/archive/szextractor"
humanize "github.com/dustin/go-humanize"
"github.com/itchio/butler/comm"
"github.com/itchio/butler/mansion"
"github.com/itchio/wharf/eos"
"github.com/itchio/wharf/state"
"github.com/pkg/errors"
)
var args = struct {
file *string
dir *string
}{}
func Register(ctx *mansion.Context) {
cmd := ctx.App.Command("unsz", "Extract any archive file supported by 7-zip").Hidden()
args.file = cmd.Arg("file", "Path of the archive to extract").Required().String()
args.dir = cmd.Flag("dir", "An optional directory to which to extract files (defaults to CWD)").Default(".").Short('d').String()
ctx.Register(cmd, do)
}
func do(ctx *mansion.Context) {
ctx.Must(Do(ctx, &UnszParams{
File: *args.file,
Dir: *args.dir,
Consumer: comm.NewStateConsumer(),
}))
}
type UnszParams struct {
File string
Dir string
Consumer *state.Consumer
}
func Do(ctx *mansion.Context, params *UnszParams) error {
if params.File == "" {
return errors.New("unsz: File must be specified")
}
if params.Dir == "" {
return errors.New("unsz: Dir must be specified")
}
consumer := params.Consumer
file, err := eos.Open(params.File)
if err != nil {
return errors.WithStack(err)
}
defer file.Close()
stats, err := file.Stat()
if err != nil {
return errors.WithStack(err)
}
consumer.Opf("Extracting %s to %s", stats.Name(), params.Dir)
ex, err := szextractor.New(file, consumer)
if err != nil {
return errors.WithStack(err)
}
startTime := time.Now()
sink := &savior.FolderSink{
Directory: params.Dir,
}
comm.StartProgress()
res, err := ex.Resume(nil, sink)
comm.EndProgress()
if err != nil {
return errors.WithStack(err)
}
duration := time.Since(startTime)
bytesPerSec := float64(res.Size()) / duration.Seconds()
consumer.Statf("Overall extraction speed: %s/s", humanize.IBytes(uint64(bytesPerSec)))
return nil
}