github.com/drone/drone-cache-lib@v0.0.0-20200806063744-981868645a25/archive/tgz/tgz.go (about)

     1  package tgz
     2  
     3  // special thanks to this medium article:
     4  // https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07
     5  
     6  import (
     7  	"compress/gzip"
     8  	"io"
     9  
    10  	"github.com/drone/drone-cache-lib/archive"
    11  	"github.com/drone/drone-cache-lib/archive/tar"
    12  )
    13  
    14  type tgzArchive struct{}
    15  
    16  // New creates an archive that uses the .tar.gz file format.
    17  func New() archive.Archive {
    18  	return &tgzArchive{}
    19  }
    20  
    21  func (a *tgzArchive) Pack(srcs []string, w io.Writer) error {
    22  	gw := gzip.NewWriter(w)
    23  	defer gw.Close()
    24  
    25  	taP := tar.New()
    26  
    27  	err := taP.Pack(srcs, gw)
    28  
    29  	return err
    30  }
    31  
    32  func (a *tgzArchive) Unpack(dst string, r io.Reader) error {
    33  	gr, err := gzip.NewReader(r)
    34  
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	taU := tar.New()
    40  
    41  	fwErr := taU.Unpack(dst, gr)
    42  
    43  	return fwErr
    44  }