mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-03 13:11:48 +02:00
69c8a502db
* chore(deps): bump github.com/golangci/golangci-lint from 1.23.7 to 1.27.0 Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: tests Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
// Package zip implements the Archive interface providing zip archiving
|
|
// and compression.
|
|
package zip
|
|
|
|
import (
|
|
"archive/zip"
|
|
"compress/flate"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
// Archive zip struct.
|
|
type Archive struct {
|
|
z *zip.Writer
|
|
}
|
|
|
|
// Close all closeables.
|
|
func (a Archive) Close() error {
|
|
return a.z.Close()
|
|
}
|
|
|
|
// New zip archive.
|
|
func New(target io.Writer) Archive {
|
|
compressor := zip.NewWriter(target)
|
|
compressor.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
|
|
return flate.NewWriter(out, flate.BestCompression)
|
|
})
|
|
return Archive{
|
|
z: compressor,
|
|
}
|
|
}
|
|
|
|
// Add a file to the zip archive.
|
|
func (a Archive) Add(name, path string) (err error) {
|
|
file, err := os.Open(path) // #nosec
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer file.Close()
|
|
info, err := file.Stat()
|
|
if err != nil {
|
|
return
|
|
}
|
|
if info.IsDir() {
|
|
return
|
|
}
|
|
header, err := zip.FileInfoHeader(info)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
header.Name = name
|
|
header.Method = zip.Deflate
|
|
w, err := a.z.CreateHeader(header)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = io.Copy(w, file)
|
|
return err
|
|
}
|