github.com/hashicorp/go-getter/v2@v2.2.2/decompress_gzip.go (about) 1 package getter 2 3 import ( 4 "compress/gzip" 5 "fmt" 6 "os" 7 "path/filepath" 8 ) 9 10 // GzipDecompressor is an implementation of Decompressor that can 11 // decompress gzip files. 12 type GzipDecompressor 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 *GzipDecompressor) Decompress(dst, src string, dir bool, umask os.FileMode) error { 20 // Directory isn't supported at all 21 if dir { 22 return fmt.Errorf("gzip-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 // gzip compression is second 38 gzipR, err := gzip.NewReader(f) 39 if err != nil { 40 return err 41 } 42 defer gzipR.Close() 43 44 // Copy it out 45 return copyReader(dst, gzipR, 0622, umask, d.FileSizeLimit) 46 }