Skip to content

Commit

Permalink
feat: add un-taring plain, unzipped tar files
Browse files Browse the repository at this point in the history
Signed-off-by: Gergely Brautigam <182850+Skarlso@users.noreply.github.com>
  • Loading branch information
Skarlso committed Jun 19, 2024
1 parent 328e8e9 commit e6984b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
18 changes: 14 additions & 4 deletions tar/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type tarOpts struct {

// skipSymlinks ignores symlinks instead of failing the decompression.
skipSymlinks bool

// skipGzip skip gzip reader an un-tar a plain tar file.
skipGzip bool
}

// Untar reads the gzip-compressed tar file from r and writes it into dir.
Expand Down Expand Up @@ -78,11 +81,18 @@ func Untar(r io.Reader, dir string, inOpts ...TarOption) (err error) {
}

madeDir := map[string]bool{}
zr, err := gzip.NewReader(r)
if err != nil {
return fmt.Errorf("requires gzip-compressed body: %w", err)
var tr *tar.Reader
if opts.skipGzip {
tr = tar.NewReader(r)
} else {
zr, err := gzip.NewReader(r)
if err != nil {
return fmt.Errorf("requires gzip-compressed body: %w", err)
}

tr = tar.NewReader(zr)
}
tr := tar.NewReader(zr)

processedBytes := 0
t0 := time.Now()

Expand Down
7 changes: 7 additions & 0 deletions tar/tar_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ func WithSkipSymlinks() TarOption {
}
}

// WithSkipGzip allows for un-taring plain tar files too, that aren't gzipped.
func WithSkipGzip() TarOption {
return func(t *tarOpts) {
t.skipGzip = true
}
}

func (t *tarOpts) applyOpts(tarOpts ...TarOption) {
for _, clientOpt := range tarOpts {
clientOpt(t)
Expand Down

0 comments on commit e6984b4

Please sign in to comment.