github.com/goreleaser/goreleaser@v1.25.1/pkg/archive/gzip/gzip.go (about) 1 // Package gzip implements the Archive interface providing gz archiving 2 // and compression. 3 package gzip 4 5 import ( 6 "fmt" 7 "io" 8 "os" 9 10 "github.com/goreleaser/goreleaser/pkg/config" 11 gzip "github.com/klauspost/pgzip" 12 ) 13 14 // Archive as gz. 15 type Archive struct { 16 gw *gzip.Writer 17 } 18 19 // New gz archive. 20 func New(target io.Writer) Archive { 21 // the error will be nil since the compression level is valid 22 gw, _ := gzip.NewWriterLevel(target, gzip.BestCompression) 23 return Archive{ 24 gw: gw, 25 } 26 } 27 28 // Close all closeables. 29 func (a Archive) Close() error { 30 return a.gw.Close() 31 } 32 33 // Add file to the archive. 34 func (a Archive) Add(f config.File) error { 35 if a.gw.Header.Name != "" { 36 return fmt.Errorf("gzip: failed to add %s, only one file can be archived in gz format", f.Destination) 37 } 38 file, err := os.Open(f.Source) // #nosec 39 if err != nil { 40 return err 41 } 42 defer file.Close() 43 info, err := file.Stat() 44 if err != nil { 45 return err 46 } 47 if info.IsDir() { 48 return nil 49 } 50 a.gw.Header.Name = f.Destination 51 if f.Info.ParsedMTime.IsZero() { 52 a.gw.Header.ModTime = info.ModTime() 53 } else { 54 a.gw.Header.ModTime = f.Info.ParsedMTime 55 } 56 _, err = io.Copy(a.gw, file) 57 return err 58 }