Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .zip file support #168

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/compress/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,16 @@ func extraFile(name, targetName, tarFile string, header *tar.Header, tarReader *
_ = targetFile.Close()
return
}

// GetCompressor gets the compressor base on file extension
func GetCompressor(extension string, additionBinaries []string) Compress {
// Select the right decompressor based on file type
switch extension {
case ".xz":
return NewXz(additionBinaries)
case ".zip":
return NewZip(additionBinaries)
default:
return NewGZip(additionBinaries)
}
}
37 changes: 37 additions & 0 deletions pkg/compress/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package compress

import (
"reflect"
"testing"
)

func TestGetCompressor(t *testing.T) {
type args struct {
extension string
additionBinaries []string
}
tests := []struct {
name string
args args
want Compress
}{{
name: "unknown type",
args: args{extension: ".xdf"},
want: NewGZip(nil),
}, {
name: ".zip",
args: args{extension: ".zip"},
want: NewZip(nil),
}, {
name: ".xz",
args: args{extension: ".xz"},
want: NewXz(nil),
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetCompressor(tt.args.extension, tt.args.additionBinaries); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetCompressor() = %v, want %v", got, tt.want)
}
})
}
}
3 changes: 3 additions & 0 deletions pkg/compress/xz.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func NewXz(additionBinaries []string) *Xz {
return &Xz{additionBinaries: additionBinaries}
}

// make sure Xz implements the interface Compress
var _ Compress = &Xz{}

// ExtractFiles extracts files from a target compress file
func (x *Xz) ExtractFiles(sourceFile, targetName string) (err error) {
if targetName == "" {
Expand Down
58 changes: 58 additions & 0 deletions pkg/compress/zip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package compress

import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)

// Zip implements a compress which is base on zip file
type Zip struct {
additionBinaries []string
}

// NewZip creates an instance of zip
func NewZip(additionBinaries []string) *Zip {
return &Zip{additionBinaries: additionBinaries}
}

// make sure Zip implements the interface Compress
var _ Compress = &Zip{}

// ExtractFiles extracts files from a target compress file
func (z *Zip) ExtractFiles(sourceFile, targetName string) (err error) {
var archive *zip.ReadCloser
archive, err = zip.OpenReader(sourceFile)
defer func() {
_ = archive.Close()
}()

for _, f := range archive.File {
if f.FileInfo().IsDir() {
continue
}

if strings.HasPrefix(f.Name, "/"+targetName) {
var targetFile *os.File
if targetFile, err = os.OpenFile(fmt.Sprintf("%s/%s", filepath.Dir(sourceFile), targetName),
os.O_CREATE|os.O_RDWR, f.Mode()); err != nil {
return
}

var fileInArchive io.ReadCloser
fileInArchive, err = f.Open()
if err != nil {
return
}
if _, err = io.Copy(targetFile, fileInArchive); err != nil {
return
}
_ = targetFile.Close()
return
}
}
return
}
10 changes: 1 addition & 9 deletions pkg/installer/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,6 @@ func (o *Installer) OverWriteBinary(sourceFile, targetPath string) (err error) {
}

func (o *Installer) extractFiles(tarFile, targetName string) (err error) {
// Select the right decompressor based on file type
extension := path.Ext(tarFile)
var compressor compress.Compress
if extension == ".xz" {
compressor = compress.NewXz(o.AdditionBinaries)
} else {
compressor = compress.NewGZip(o.AdditionBinaries)
}
err = compressor.ExtractFiles(tarFile, targetName)
err = compress.GetCompressor(path.Ext(tarFile), o.AdditionBinaries).ExtractFiles(tarFile, targetName)
return
}