Skip to content

Commit 7bd1d82

Browse files
authored
fix: the zip decompress failed (#454)
Signed-off-by: rick <LinuxSuRen@users.noreply.github.com> Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
1 parent 9bcb3e8 commit 7bd1d82

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

pkg/compress/testdata/simple.zip

739 Bytes
Binary file not shown.

pkg/compress/types_test.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,33 @@ func Test_extraFile(t *testing.T) {
194194
}
195195

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

200-
err := compressor.ExtractFiles("testdata/simple.tar.gz", "aa")
201-
assert.NoError(t, err)
201+
err := compressor.ExtractFiles("testdata/simple.tar.gz", "aa")
202+
assert.NoError(t, err)
202203

203-
assertFileContentEqual(t, "testdata/aa", "aa\n")
204-
assertFileContentEqual(t, "testdata/bb", "bb\n")
205-
assertFileContentEqual(t, "testdata/cc", "cc\n")
204+
assertFileContentEqual(t, "testdata/aa", "aa\n")
205+
assertFileContentEqual(t, "testdata/bb", "bb\n")
206+
assertFileContentEqual(t, "testdata/cc", "cc\n")
207+
})
208+
209+
t.Run("test .zip", func(t *testing.T) {
210+
compressor := GetCompressor(".zip", []string{"bb", "cc"})
211+
assert.NotNil(t, compressor)
212+
213+
err := compressor.ExtractFiles("testdata/simple.zip", "aa")
214+
assert.NoError(t, err)
215+
216+
assertFileContentEqual(t, "testdata/aa", "aa\n")
217+
assertFileContentEqual(t, "testdata/bb", "bb\n")
218+
assertFileContentEqual(t, "testdata/cc", "cc\n")
219+
220+
// invalid parameters
221+
err = compressor.ExtractFiles("", "")
222+
assert.Error(t, err)
223+
})
206224
}
207225

208226
func assertFileContentEqual(t *testing.T, filePath string, expectedContent string) {

pkg/compress/zip.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package compress
33
import (
44
"archive/zip"
55
"errors"
6-
"fmt"
76
"io"
87
"os"
98
"path/filepath"
10-
"strings"
119
)
1210

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

39+
z.additionBinaries = append(z.additionBinaries, targetName)
4140
for _, f := range archive.File {
4241
if f.FileInfo().IsDir() {
4342
continue
4443
}
4544

46-
if strings.HasPrefix(f.Name, targetName) {
45+
for _, ff := range z.additionBinaries {
46+
if filepath.Base(f.Name) != ff {
47+
continue
48+
}
49+
4750
var targetFile *os.File
48-
if targetFile, err = os.OpenFile(fmt.Sprintf("%s/%s", filepath.Dir(sourceFile), targetName),
51+
if targetFile, err = os.OpenFile(filepath.Join(filepath.Dir(sourceFile), ff),
4952
os.O_CREATE|os.O_RDWR, f.Mode()); err != nil {
5053
return
5154
}
@@ -59,7 +62,7 @@ func (z *Zip) ExtractFiles(sourceFile, targetName string) (err error) {
5962
return
6063
}
6164
_ = targetFile.Close()
62-
return
65+
break
6366
}
6467
}
6568
return

0 commit comments

Comments
 (0)