Skip to content

Commit

Permalink
fix: the zip decompress failed (#454)
Browse files Browse the repository at this point in the history
Signed-off-by: rick <LinuxSuRen@users.noreply.github.com>
Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Mar 26, 2024
1 parent 9bcb3e8 commit 7bd1d82
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
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

0 comments on commit 7bd1d82

Please sign in to comment.