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

     1  package getter
     2  
     3  import (
     4  	"compress/bzip2"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  )
     9  
    10  // Bzip2Decompressor is an implementation of Decompressor that can
    11  // decompress bz2 files.
    12  type Bzip2Decompressor struct {
    13  	// FileSizeLimit limits the size of a decompressed file.
    14  	//
    15  	// The zero value means no limit.
    16  	FileSizeLimit int64
    17  }
    18  
    19  func (d *Bzip2Decompressor) Decompress(dst, src string, dir bool, umask os.FileMode) error {
    20  	// Directory isn't supported at all
    21  	if dir {
    22  		return fmt.Errorf("bzip2-compressed files can only unarchive to a single file")
    23  	}
    24  
    25  	// If we're going into a directory we should make that first
    26  	if err := os.MkdirAll(filepath.Dir(dst), mode(0755, umask)); err != nil {
    27  		return err
    28  	}
    29  
    30  	// File first
    31  	f, err := os.Open(src)
    32  	if err != nil {
    33  		return err
    34  	}
    35  	defer f.Close()
    36  
    37  	// Bzip2 compression is second
    38  	bzipR := bzip2.NewReader(f)
    39  
    40  	// Copy it out
    41  	return copyReader(dst, bzipR, 0622, umask, d.FileSizeLimit)
    42  }