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

     1  package getter
     2  
     3  import (
     4  	"compress/gzip"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"path/filepath"
     9  )
    10  
    11  // GzipDecompressor is an implementation of Decompressor that can
    12  // decompress gzip files.
    13  type GzipDecompressor struct{}
    14  
    15  func (d *GzipDecompressor) Decompress(dst, src string, dir bool) error {
    16  	// Directory isn't supported at all
    17  	if dir {
    18  		return fmt.Errorf("gzip-compressed files can only unarchive to a single file")
    19  	}
    20  
    21  	// If we're going into a directory we should make that first
    22  	if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
    23  		return err
    24  	}
    25  
    26  	// File first
    27  	f, err := os.Open(src)
    28  	if err != nil {
    29  		return err
    30  	}
    31  	defer f.Close()
    32  
    33  	// gzip compression is second
    34  	gzipR, err := gzip.NewReader(f)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	defer gzipR.Close()
    39  
    40  	// Copy it out
    41  	dstF, err := os.Create(dst)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	defer dstF.Close()
    46  
    47  	_, err = io.Copy(dstF, gzipR)
    48  	return err
    49  }