Skip to content

Commit e64ce7b

Browse files
Ryan MoranForestEckhardt
Ryan Moran
authored andcommitted
Adds StripComponents to vacation.ZipArchive
1 parent bd5243a commit e64ce7b

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

vacation/archive.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (a Archive) Decompress(destination string) error {
6464
case "application/x-bzip2":
6565
decompressor = NewTarBzip2Archive(bufferedReader).StripComponents(a.components)
6666
case "application/zip":
67-
decompressor = NewZipArchive(bufferedReader)
67+
decompressor = NewZipArchive(bufferedReader).StripComponents(a.components)
6868
case "text/plain; charset=utf-8", "application/jar":
6969
destination = filepath.Join(destination, a.name)
7070
decompressor = NewNopArchive(bufferedReader)

vacation/archive_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,18 @@ func testArchive(t *testing.T, context spec.G, it spec.S) {
301301
_, err = f.Write([]byte("some-file"))
302302
Expect(err).NotTo(HaveOccurred())
303303

304+
_, err = zw.Create("some-dir/")
305+
Expect(err).NotTo(HaveOccurred())
306+
307+
header = &zip.FileHeader{Name: filepath.Join("some-dir", "some-nested-file")}
308+
header.SetMode(0644)
309+
310+
f, err = zw.CreateHeader(header)
311+
Expect(err).NotTo(HaveOccurred())
312+
313+
_, err = f.Write([]byte("nested file"))
314+
Expect(err).NotTo(HaveOccurred())
315+
304316
Expect(zw.Close()).To(Succeed())
305317

306318
archive = vacation.NewArchive(buffer)
@@ -318,6 +330,18 @@ func testArchive(t *testing.T, context spec.G, it spec.S) {
318330
Expect(err).NotTo(HaveOccurred())
319331
Expect(files).To(ConsistOf([]string{
320332
filepath.Join(tempDir, "some-file"),
333+
filepath.Join(tempDir, "some-dir"),
334+
}))
335+
})
336+
337+
it("unpackages the archive into the path but also strips the first component", func() {
338+
err := archive.StripComponents(1).Decompress(tempDir)
339+
Expect(err).NotTo(HaveOccurred())
340+
341+
files, err := filepath.Glob(filepath.Join(tempDir, "*"))
342+
Expect(err).NotTo(HaveOccurred())
343+
Expect(files).To(ConsistOf([]string{
344+
filepath.Join(tempDir, "some-nested-file"),
321345
}))
322346
})
323347
})

vacation/example_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ func ExampleArchive_StripComponents() {
191191

192192
// Output:
193193
// some-tar-file
194-
// some-zip-dir/some-zip-file
195-
// zip-file
194+
// some-zip-file
196195
}
197196

198197
func ExampleTarArchive() {

vacation/zip_archive.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77
"os"
88
"path/filepath"
99
"sort"
10+
"strings"
1011
)
1112

1213
// A ZipArchive decompresses zip files from an input stream.
1314
type ZipArchive struct {
14-
reader io.Reader
15+
reader io.Reader
16+
components int
1517
}
1618

1719
// NewZipArchive returns a new ZipArchive that reads from inputReader.
@@ -65,7 +67,15 @@ func (z ZipArchive) Decompress(destination string) error {
6567
return err
6668
}
6769

68-
path := filepath.Join(destination, name)
70+
fileNames := strings.Split(name, "/")
71+
72+
// Checks to see if file should be written when stripping components
73+
if len(fileNames) <= z.components {
74+
continue
75+
}
76+
77+
// Constructs the path that conforms to the stripped components.
78+
path := filepath.Join(append([]string{destination}, fileNames[z.components:]...)...)
6979

7080
switch {
7181
case f.FileInfo().IsDir():
@@ -158,3 +168,10 @@ func (z ZipArchive) Decompress(destination string) error {
158168

159169
return nil
160170
}
171+
172+
// StripComponents removes the first n levels from the final decompression
173+
// destination.
174+
func (z ZipArchive) StripComponents(components int) ZipArchive {
175+
z.components = components
176+
return z
177+
}

vacation/zip_archive_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ func testZipArchive(t *testing.T, context spec.G, it spec.S) {
113113
Expect(data).To(Equal([]byte("nested file")))
114114
})
115115

116+
it("unpackages the archive into the path but also strips the first component", func() {
117+
var err error
118+
err = zipArchive.StripComponents(1).Decompress(tempDir)
119+
Expect(err).ToNot(HaveOccurred())
120+
121+
files, err := filepath.Glob(fmt.Sprintf("%s/*", tempDir))
122+
Expect(err).NotTo(HaveOccurred())
123+
Expect(files).To(ConsistOf([]string{
124+
filepath.Join(tempDir, "some-other-dir"),
125+
}))
126+
127+
Expect(filepath.Join(tempDir, "some-other-dir")).To(BeADirectory())
128+
Expect(filepath.Join(tempDir, "some-other-dir", "some-file")).To(BeARegularFile())
129+
})
130+
116131
context("failure cases", func() {
117132
context("when it fails to create a zip reader", func() {
118133
it("returns an error", func() {

0 commit comments

Comments
 (0)