github.com/hashicorp/go-getter/v2@v2.2.2/decompress_tgz.go (about)

     1  package getter
     2  
     3  import (
     4  	"compress/gzip"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  )
     9  
    10  // TarGzipDecompressor is an implementation of Decompressor that can
    11  // decompress tar.gzip files.
    12  type TarGzipDecompressor struct {
    13  	// FileSizeLimit limits the total size of all
    14  	// decompressed files.
    15  	//
    16  	// The zero value means no limit.
    17  	FileSizeLimit int64
    18  
    19  	// FilesLimit limits the number of files that are
    20  	// allowed to be decompressed.
    21  	//
    22  	// The zero value means no limit.
    23  	FilesLimit int
    24  }
    25  
    26  func (d *TarGzipDecompressor) Decompress(dst, src string, dir bool, umask os.FileMode) error {
    27  	// If we're going into a directory we should make that first
    28  	mkdir := dst
    29  	if !dir {
    30  		mkdir = filepath.Dir(dst)
    31  	}
    32  	if err := os.MkdirAll(mkdir, mode(0755, umask)); err != nil {
    33  		return err
    34  	}
    35  
    36  	// File first
    37  	f, err := os.Open(src)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	defer f.Close()
    42  
    43  	// Gzip compression is second
    44  	gzipR, err := gzip.NewReader(f)
    45  	if err != nil {
    46  		return fmt.Errorf("Error opening a gzip reader for %s: %s", src, err)
    47  	}
    48  	defer gzipR.Close()
    49  
    50  	return untar(gzipR, dst, src, dir, umask, d.FileSizeLimit, d.FilesLimit)
    51  }