Skip to content

Commit

Permalink
Adds StripComponents to vacation.ZipArchive
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Moran authored and ForestEckhardt committed Jul 23, 2021
1 parent bd5243a commit e64ce7b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
2 changes: 1 addition & 1 deletion vacation/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (a Archive) Decompress(destination string) error {
case "application/x-bzip2":
decompressor = NewTarBzip2Archive(bufferedReader).StripComponents(a.components)
case "application/zip":
decompressor = NewZipArchive(bufferedReader)
decompressor = NewZipArchive(bufferedReader).StripComponents(a.components)
case "text/plain; charset=utf-8", "application/jar":
destination = filepath.Join(destination, a.name)
decompressor = NewNopArchive(bufferedReader)
Expand Down
24 changes: 24 additions & 0 deletions vacation/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,18 @@ func testArchive(t *testing.T, context spec.G, it spec.S) {
_, err = f.Write([]byte("some-file"))
Expect(err).NotTo(HaveOccurred())

_, err = zw.Create("some-dir/")
Expect(err).NotTo(HaveOccurred())

header = &zip.FileHeader{Name: filepath.Join("some-dir", "some-nested-file")}
header.SetMode(0644)

f, err = zw.CreateHeader(header)
Expect(err).NotTo(HaveOccurred())

_, err = f.Write([]byte("nested file"))
Expect(err).NotTo(HaveOccurred())

Expect(zw.Close()).To(Succeed())

archive = vacation.NewArchive(buffer)
Expand All @@ -318,6 +330,18 @@ func testArchive(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(tempDir, "some-file"),
filepath.Join(tempDir, "some-dir"),
}))
})

it("unpackages the archive into the path but also strips the first component", func() {
err := archive.StripComponents(1).Decompress(tempDir)
Expect(err).NotTo(HaveOccurred())

files, err := filepath.Glob(filepath.Join(tempDir, "*"))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(tempDir, "some-nested-file"),
}))
})
})
Expand Down
3 changes: 1 addition & 2 deletions vacation/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ func ExampleArchive_StripComponents() {

// Output:
// some-tar-file
// some-zip-dir/some-zip-file
// zip-file
// some-zip-file
}

func ExampleTarArchive() {
Expand Down
21 changes: 19 additions & 2 deletions vacation/zip_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"os"
"path/filepath"
"sort"
"strings"
)

// A ZipArchive decompresses zip files from an input stream.
type ZipArchive struct {
reader io.Reader
reader io.Reader
components int
}

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

path := filepath.Join(destination, name)
fileNames := strings.Split(name, "/")

// Checks to see if file should be written when stripping components
if len(fileNames) <= z.components {
continue
}

// Constructs the path that conforms to the stripped components.
path := filepath.Join(append([]string{destination}, fileNames[z.components:]...)...)

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

return nil
}

// StripComponents removes the first n levels from the final decompression
// destination.
func (z ZipArchive) StripComponents(components int) ZipArchive {
z.components = components
return z
}
15 changes: 15 additions & 0 deletions vacation/zip_archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ func testZipArchive(t *testing.T, context spec.G, it spec.S) {
Expect(data).To(Equal([]byte("nested file")))
})

it("unpackages the archive into the path but also strips the first component", func() {
var err error
err = zipArchive.StripComponents(1).Decompress(tempDir)
Expect(err).ToNot(HaveOccurred())

files, err := filepath.Glob(fmt.Sprintf("%s/*", tempDir))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(tempDir, "some-other-dir"),
}))

Expect(filepath.Join(tempDir, "some-other-dir")).To(BeADirectory())
Expect(filepath.Join(tempDir, "some-other-dir", "some-file")).To(BeARegularFile())
})

context("failure cases", func() {
context("when it fails to create a zip reader", func() {
it("returns an error", func() {
Expand Down

0 comments on commit e64ce7b

Please sign in to comment.