Skip to content

Commit 017a33d

Browse files
authored
Add .zip file support (#168)
1 parent 38d64ee commit 017a33d

File tree

5 files changed

+112
-9
lines changed

5 files changed

+112
-9
lines changed

pkg/compress/types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,16 @@ func extraFile(name, targetName, tarFile string, header *tar.Header, tarReader *
2929
_ = targetFile.Close()
3030
return
3131
}
32+
33+
// GetCompressor gets the compressor base on file extension
34+
func GetCompressor(extension string, additionBinaries []string) Compress {
35+
// Select the right decompressor based on file type
36+
switch extension {
37+
case ".xz":
38+
return NewXz(additionBinaries)
39+
case ".zip":
40+
return NewZip(additionBinaries)
41+
default:
42+
return NewGZip(additionBinaries)
43+
}
44+
}

pkg/compress/types_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package compress
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestGetCompressor(t *testing.T) {
9+
type args struct {
10+
extension string
11+
additionBinaries []string
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want Compress
17+
}{{
18+
name: "unknown type",
19+
args: args{extension: ".xdf"},
20+
want: NewGZip(nil),
21+
}, {
22+
name: ".zip",
23+
args: args{extension: ".zip"},
24+
want: NewZip(nil),
25+
}, {
26+
name: ".xz",
27+
args: args{extension: ".xz"},
28+
want: NewXz(nil),
29+
}}
30+
for _, tt := range tests {
31+
t.Run(tt.name, func(t *testing.T) {
32+
if got := GetCompressor(tt.args.extension, tt.args.additionBinaries); !reflect.DeepEqual(got, tt.want) {
33+
t.Errorf("GetCompressor() = %v, want %v", got, tt.want)
34+
}
35+
})
36+
}
37+
}

pkg/compress/xz.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ func NewXz(additionBinaries []string) *Xz {
2020
return &Xz{additionBinaries: additionBinaries}
2121
}
2222

23+
// make sure Xz implements the interface Compress
24+
var _ Compress = &Xz{}
25+
2326
// ExtractFiles extracts files from a target compress file
2427
func (x *Xz) ExtractFiles(sourceFile, targetName string) (err error) {
2528
if targetName == "" {

pkg/compress/zip.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package compress
2+
3+
import (
4+
"archive/zip"
5+
"fmt"
6+
"io"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
)
11+
12+
// Zip implements a compress which is base on zip file
13+
type Zip struct {
14+
additionBinaries []string
15+
}
16+
17+
// NewZip creates an instance of zip
18+
func NewZip(additionBinaries []string) *Zip {
19+
return &Zip{additionBinaries: additionBinaries}
20+
}
21+
22+
// make sure Zip implements the interface Compress
23+
var _ Compress = &Zip{}
24+
25+
// ExtractFiles extracts files from a target compress file
26+
func (z *Zip) ExtractFiles(sourceFile, targetName string) (err error) {
27+
var archive *zip.ReadCloser
28+
archive, err = zip.OpenReader(sourceFile)
29+
defer func() {
30+
_ = archive.Close()
31+
}()
32+
33+
for _, f := range archive.File {
34+
if f.FileInfo().IsDir() {
35+
continue
36+
}
37+
38+
if strings.HasPrefix(f.Name, "/"+targetName) {
39+
var targetFile *os.File
40+
if targetFile, err = os.OpenFile(fmt.Sprintf("%s/%s", filepath.Dir(sourceFile), targetName),
41+
os.O_CREATE|os.O_RDWR, f.Mode()); err != nil {
42+
return
43+
}
44+
45+
var fileInArchive io.ReadCloser
46+
fileInArchive, err = f.Open()
47+
if err != nil {
48+
return
49+
}
50+
if _, err = io.Copy(targetFile, fileInArchive); err != nil {
51+
return
52+
}
53+
_ = targetFile.Close()
54+
return
55+
}
56+
}
57+
return
58+
}

pkg/installer/process.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,6 @@ func (o *Installer) OverWriteBinary(sourceFile, targetPath string) (err error) {
113113
}
114114

115115
func (o *Installer) extractFiles(tarFile, targetName string) (err error) {
116-
// Select the right decompressor based on file type
117-
extension := path.Ext(tarFile)
118-
var compressor compress.Compress
119-
if extension == ".xz" {
120-
compressor = compress.NewXz(o.AdditionBinaries)
121-
} else {
122-
compressor = compress.NewGZip(o.AdditionBinaries)
123-
}
124-
err = compressor.ExtractFiles(tarFile, targetName)
116+
err = compress.GetCompressor(path.Ext(tarFile), o.AdditionBinaries).ExtractFiles(tarFile, targetName)
125117
return
126118
}

0 commit comments

Comments
 (0)