1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-19 20:57:53 +02:00
John Taylor e2ef52032b
feat: use maximum compression for gzip and zip archives (#1416)
* feat: use maximum compression for gzip and zip archives

* fix: add comment for skipping error check

Co-authored-by: John Taylor <ec2-user@ip-172-31-29-117.ap-south-1.compute.internal>
2020-04-01 19:05:20 +00:00

53 lines
1020 B
Go

// Package gzip implements the Archive interface providing gz archiving
// and compression.
package gzip
import (
"compress/gzip"
"fmt"
"io"
"os"
)
// Archive as gz
type Archive struct {
gw *gzip.Writer
}
// Close all closeables
func (a Archive) Close() error {
return a.gw.Close()
}
// New gz archive
func New(target io.Writer) Archive {
// the error will be nil since the compression level is valid
gw, _ := gzip.NewWriterLevel(target, gzip.BestCompression)
return Archive{
gw: gw,
}
}
// Add file to the archive
func (a Archive) Add(name, path string) error {
if a.gw.Header.Name != "" {
return fmt.Errorf("gzip: failed to add %s, only one file can be archived in gz format", name)
}
file, err := os.Open(path) // #nosec
if err != nil {
return err
}
defer file.Close() // nolint: errcheck
info, err := file.Stat()
if err != nil {
return err
}
if info.IsDir() {
return nil
}
a.gw.Header.Name = name
a.gw.Header.ModTime = info.ModTime()
_, err = io.Copy(a.gw, file)
return err
}