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

fix: the zip decompress failed #454

Merged
merged 1 commit into from
Mar 26, 2024
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
Binary file added pkg/compress/testdata/simple.zip
Binary file not shown.
32 changes: 25 additions & 7 deletions pkg/compress/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,33 @@ func Test_extraFile(t *testing.T) {
}

func TestExtractFiles(t *testing.T) {
compressor := GetCompressor(".tar.gz", []string{"bb", "cc"})
assert.NotNil(t, compressor)
t.Run("test .tar.gz", func(t *testing.T) {
compressor := GetCompressor(".tar.gz", []string{"bb", "cc"})
assert.NotNil(t, compressor)

err := compressor.ExtractFiles("testdata/simple.tar.gz", "aa")
assert.NoError(t, err)
err := compressor.ExtractFiles("testdata/simple.tar.gz", "aa")
assert.NoError(t, err)

assertFileContentEqual(t, "testdata/aa", "aa\n")
assertFileContentEqual(t, "testdata/bb", "bb\n")
assertFileContentEqual(t, "testdata/cc", "cc\n")
assertFileContentEqual(t, "testdata/aa", "aa\n")
assertFileContentEqual(t, "testdata/bb", "bb\n")
assertFileContentEqual(t, "testdata/cc", "cc\n")
})

t.Run("test .zip", func(t *testing.T) {
compressor := GetCompressor(".zip", []string{"bb", "cc"})
assert.NotNil(t, compressor)

err := compressor.ExtractFiles("testdata/simple.zip", "aa")
assert.NoError(t, err)

assertFileContentEqual(t, "testdata/aa", "aa\n")
assertFileContentEqual(t, "testdata/bb", "bb\n")
assertFileContentEqual(t, "testdata/cc", "cc\n")

// invalid parameters
err = compressor.ExtractFiles("", "")
assert.Error(t, err)
})
}

func assertFileContentEqual(t *testing.T, filePath string, expectedContent string) {
Expand Down
13 changes: 8 additions & 5 deletions pkg/compress/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package compress
import (
"archive/zip"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)

// Zip implements a compress which is base on zip file
Expand Down Expand Up @@ -38,14 +36,19 @@ func (z *Zip) ExtractFiles(sourceFile, targetName string) (err error) {
_ = archive.Close()
}()

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

if strings.HasPrefix(f.Name, targetName) {
for _, ff := range z.additionBinaries {
if filepath.Base(f.Name) != ff {
continue
}

var targetFile *os.File
if targetFile, err = os.OpenFile(fmt.Sprintf("%s/%s", filepath.Dir(sourceFile), targetName),
if targetFile, err = os.OpenFile(filepath.Join(filepath.Dir(sourceFile), ff),
os.O_CREATE|os.O_RDWR, f.Mode()); err != nil {
return
}
Expand All @@ -59,7 +62,7 @@ func (z *Zip) ExtractFiles(sourceFile, targetName string) (err error) {
return
}
_ = targetFile.Close()
return
break
}
}
return
Expand Down
Loading