-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
47 lines (37 loc) · 1004 Bytes
/
main.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
package main
import (
"os"
"github.com/alecthomas/kong"
"github.com/rs/zerolog/log"
"github.com/martinlindhe/compressionid"
)
var args struct {
Filename string `kong:"arg" name:"filename" type:"existingfile" help:"Input file."`
Offset int64 `help:"Starting offset (default 0)."`
OutFile string `help:"Write extracted data to file." short:"o"`
}
func main() {
compressionid.InitLogging()
_ = kong.Parse(&args,
kong.Name("compressionid"),
kong.Description("A compression identifier."))
f, err := os.Open(args.Filename)
if err != nil {
panic(err)
}
if args.Offset != 0 {
f.Seek(args.Offset, os.SEEK_SET)
}
kind, v, err := compressionid.TryExtract(f)
if err == nil {
log.Info().Msgf("%s: %s compression detected", args.Filename, kind.String())
if args.OutFile != "" {
err = os.WriteFile(args.OutFile, v, 0644)
if err != nil {
log.Error().Err(err).Msgf("Failed to write to %s", args.OutFile)
}
}
} else {
log.Error().Err(err).Msgf("Giving up")
}
}