forked from mholt/archiver
/
tarbz2.go
50 lines (41 loc) · 1.25 KB
/
tarbz2.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
package archiver
import (
"archive/tar"
"fmt"
"os"
"github.com/dsnet/compress/bzip2"
)
// TarBz2 creates a .tar.bz2 file at tarbz2Path 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 TarBz2(tarbz2Path string, filePaths []string) error {
out, err := os.Create(tarbz2Path)
if err != nil {
return fmt.Errorf("error creating %s: %v", tarbz2Path, err)
}
defer out.Close()
bz2Writer, err := bzip2.NewWriter(out, nil)
if err != nil {
return fmt.Errorf("error compressing %s: %v", tarbz2Path, err)
}
defer bz2Writer.Close()
tarWriter := tar.NewWriter(bz2Writer)
defer tarWriter.Close()
return tarball(filePaths, tarWriter, tarbz2Path)
}
// UntarBz2 untars source and decompresses the contents into destination.
func UntarBz2(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()
bz2r, err := bzip2.NewReader(f, nil)
if err != nil {
return fmt.Errorf("error decompressing %s: %v", source, err)
}
defer bz2r.Close()
return untar(tar.NewReader(bz2r), destination)
}