github.com/remind101/go-getter@v0.0.0-20180809191950-4bda8fa99001/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  
    14  func (d *TarGzipDecompressor) Decompress(dst, src string, dir bool) error {
    15  	// If we're going into a directory we should make that first
    16  	mkdir := dst
    17  	if !dir {
    18  		mkdir = filepath.Dir(dst)
    19  	}
    20  	if err := os.MkdirAll(mkdir, 0755); err != nil {
    21  		return err
    22  	}
    23  
    24  	// File first
    25  	f, err := os.Open(src)
    26  	if err != nil {
    27  		return err
    28  	}
    29  	defer f.Close()
    30  
    31  	// Gzip compression is second
    32  	gzipR, err := gzip.NewReader(f)
    33  	if err != nil {
    34  		return fmt.Errorf("Error opening a gzip reader for %s: %s", src, err)
    35  	}
    36  	defer gzipR.Close()
    37  
    38  	return untar(gzipR, dst, src, dir)
    39  }