mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-22 04:08:49 +02:00
e2ef52032b
* 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>
62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
// Package targz implements the Archive interface providing tar.gz archiving
|
|
// and compression.
|
|
package targz
|
|
|
|
import (
|
|
"archive/tar"
|
|
"compress/gzip"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
// Archive as tar.gz
|
|
type Archive struct {
|
|
gw *gzip.Writer
|
|
tw *tar.Writer
|
|
}
|
|
|
|
// Close all closeables
|
|
func (a Archive) Close() error {
|
|
if err := a.tw.Close(); err != nil {
|
|
return err
|
|
}
|
|
return a.gw.Close()
|
|
}
|
|
|
|
// New tar.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)
|
|
tw := tar.NewWriter(gw)
|
|
return Archive{
|
|
gw: gw,
|
|
tw: tw,
|
|
}
|
|
}
|
|
|
|
// Add file to the archive
|
|
func (a Archive) Add(name, path string) error {
|
|
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
|
|
}
|
|
header, err := tar.FileInfoHeader(info, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
header.Name = name
|
|
if err = a.tw.WriteHeader(header); err != nil {
|
|
return err
|
|
}
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
_, err = io.Copy(a.tw, file)
|
|
return err
|
|
}
|