github.com/weiwenhao/getter@v1.30.1/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  
    14  func (d *Bzip2Decompressor) Decompress(dst, src string, dir bool, umask os.FileMode) error {
    15  	// Directory isn't supported at all
    16  	if dir {
    17  		return fmt.Errorf("bzip2-compressed files can only unarchive to a single file")
    18  	}
    19  
    20  	// If we're going into a directory we should make that first
    21  	if err := os.MkdirAll(filepath.Dir(dst), mode(0755, umask)); 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  	// Bzip2 compression is second
    33  	bzipR := bzip2.NewReader(f)
    34  
    35  	// Copy it out
    36  	return copyReader(dst, bzipR, 0622, umask)
    37  }