github.com/remind101/go-getter@v0.0.0-20180809191950-4bda8fa99001/decompress_txz.go (about)

     1  package getter
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/ulikunitz/xz"
     9  )
    10  
    11  // TarXzDecompressor is an implementation of Decompressor that can
    12  // decompress tar.xz files.
    13  type TarXzDecompressor struct{}
    14  
    15  func (d *TarXzDecompressor) Decompress(dst, src string, dir bool) error {
    16  	// If we're going into a directory we should make that first
    17  	mkdir := dst
    18  	if !dir {
    19  		mkdir = filepath.Dir(dst)
    20  	}
    21  	if err := os.MkdirAll(mkdir, 0755); err != nil {
    22  		return err
    23  	}
    24  
    25  	// File first
    26  	f, err := os.Open(src)
    27  	if err != nil {
    28  		return err
    29  	}
    30  	defer f.Close()
    31  
    32  	// xz compression is second
    33  	txzR, err := xz.NewReader(f)
    34  	if err != nil {
    35  		return fmt.Errorf("Error opening an xz reader for %s: %s", src, err)
    36  	}
    37  
    38  	return untar(txzR, dst, src, dir)
    39  }