]>
Commit | Line | Data |
---|---|---|
1 | package getter | |
2 | ||
3 | import ( | |
4 | "compress/bzip2" | |
5 | "fmt" | |
6 | "io" | |
7 | "os" | |
8 | "path/filepath" | |
9 | ) | |
10 | ||
11 | // Bzip2Decompressor is an implementation of Decompressor that can | |
12 | // decompress bz2 files. | |
13 | type Bzip2Decompressor struct{} | |
14 | ||
15 | func (d *Bzip2Decompressor) Decompress(dst, src string, dir bool) error { | |
16 | // Directory isn't supported at all | |
17 | if dir { | |
18 | return fmt.Errorf("bzip2-compressed files can only unarchive to a single file") | |
19 | } | |
20 | ||
21 | // If we're going into a directory we should make that first | |
22 | if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil { | |
23 | return err | |
24 | } | |
25 | ||
26 | // File first | |
27 | f, err := os.Open(src) | |
28 | if err != nil { | |
29 | return err | |
30 | } | |
31 | defer f.Close() | |
32 | ||
33 | // Bzip2 compression is second | |
34 | bzipR := bzip2.NewReader(f) | |
35 | ||
36 | // Copy it out | |
37 | dstF, err := os.Create(dst) | |
38 | if err != nil { | |
39 | return err | |
40 | } | |
41 | defer dstF.Close() | |
42 | ||
43 | _, err = io.Copy(dstF, bzipR) | |
44 | return err | |
45 | } |