-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
76 lines (62 loc) · 1.44 KB
/
test.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
// @author: Brian Wojtczak
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"github.com/dustin/go-humanize"
"github.com/klauspost/compress/gzip"
"github.com/pkg/errors"
"io"
"log"
"os"
"time"
)
// TestFile takes a filename and tests it for gzip integrity.
func TestFile(filename string, verbose bool) (err error) {
var (
inputInfo os.FileInfo
inputFile *os.File
zr *gzip.Reader
)
started := time.Now().UTC()
if verbose {
log.Printf(
"Testing %s",
filename,
)
}
inputInfo, err = os.Stat(filename)
if err != nil {
return errors.Wrap(err, "error stating input file")
}
inputFile, err = os.Open(filename)
if err != nil {
return errors.Wrap(err, "error opening input file")
}
//goland:noinspection GoUnhandledErrorResult
defer inputFile.Close()
zr, err = gzip.NewReader(bufio.NewReader(inputFile))
if err != nil {
return errors.Wrap(err, "error creating gzip reader")
}
_, err = io.Copy(io.Discard, zr)
if err != nil {
return errors.Wrap(err, "error decompressing file")
}
if err := zr.Close(); err != nil {
return errors.Wrap(err, "error closing gzip reader")
}
if err := inputFile.Close(); err != nil {
return errors.Wrap(err, "error closing input file")
}
if verbose {
log.Printf(
"Tested %s (%s) in %v",
filename,
humanize.Bytes(uint64(inputInfo.Size())),
time.Since(started),
)
}
return nil
}