From e6984b4206ed7da9d7d386074f7628cb16f92541 Mon Sep 17 00:00:00 2001 From: Gergely Brautigam <182850+Skarlso@users.noreply.github.com> Date: Wed, 19 Jun 2024 08:58:36 +0200 Subject: [PATCH] feat: add un-taring plain, unzipped tar files Signed-off-by: Gergely Brautigam <182850+Skarlso@users.noreply.github.com> --- tar/tar.go | 18 ++++++++++++++---- tar/tar_opts.go | 7 +++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/tar/tar.go b/tar/tar.go index d9f8028f..3adc3860 100644 --- a/tar/tar.go +++ b/tar/tar.go @@ -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. @@ -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() diff --git a/tar/tar_opts.go b/tar/tar_opts.go index 127dfdfe..1d433c28 100644 --- a/tar/tar_opts.go +++ b/tar/tar_opts.go @@ -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)