forked from mholt/archiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tarsz.go
92 lines (74 loc) · 2.36 KB
/
tarsz.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
package archiver
import (
"fmt"
"io"
"os"
"strings"
"github.com/golang/snappy"
)
// TarSz is for TarSz format
var TarSz tarSzFormat
func init() {
RegisterFormat("TarSz", TarSz)
}
type tarSzFormat struct{}
func (tarSzFormat) Match(filename string) bool {
return strings.HasSuffix(strings.ToLower(filename), ".tar.sz") || strings.HasSuffix(strings.ToLower(filename), ".tsz") || isTarSz(filename)
}
// isTarSz checks the file has the sz compressed Tar format header by
// reading its beginning block.
func isTarSz(tarszPath string) bool {
f, err := os.Open(tarszPath)
if err != nil {
return false
}
defer f.Close()
szr := snappy.NewReader(f)
buf := make([]byte, tarBlockSize)
n, err := szr.Read(buf)
if err != nil || n < tarBlockSize {
return false
}
return hasTarHeader(buf)
}
// Write outputs a .tar.sz file to a Writer containing
// the contents of files listed in filePaths. File paths
// can be those of regular files or directories. Regular
// files are stored at the 'root' of the archive, and
// directories are recursively added.
func (tarSzFormat) Write(output io.Writer, filePaths []string) error {
return writeTarSz(filePaths, output, "")
}
// Make creates a .tar.sz file at tarszPath containing
// the contents of files listed in filePaths. File paths
// can be those of regular files or directories. Regular
// files are stored at the 'root' of the archive, and
// directories are recursively added.
func (tarSzFormat) Make(tarszPath string, filePaths []string) error {
out, err := os.Create(tarszPath)
if err != nil {
return fmt.Errorf("error creating %s: %v", tarszPath, err)
}
defer out.Close()
return writeTarSz(filePaths, out, tarszPath)
}
func writeTarSz(filePaths []string, output io.Writer, dest string) error {
szw := snappy.NewBufferedWriter(output)
defer szw.Close()
return writeTar(filePaths, szw, dest)
}
// Read untars a .tar.sz file read from a Reader and decompresses
// the contents into destination.
func (tarSzFormat) Read(input io.Reader, destination string) error {
szr := snappy.NewReader(input)
return Tar.Read(szr, destination)
}
// Open untars source and decompresses the contents into destination.
func (tarSzFormat) Open(source, destination string) error {
f, err := os.Open(source)
if err != nil {
return fmt.Errorf("%s: failed to open archive: %v", source, err)
}
defer f.Close()
return TarSz.Read(f, destination)
}