2018-08-05 10:23:39 -03:00
|
|
|
// Package zip implements the Archive interface providing zip archiving
|
|
|
|
// and compression.
|
|
|
|
package zip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
2020-04-01 15:05:20 -04:00
|
|
|
"compress/flate"
|
2022-11-29 19:05:18 -06:00
|
|
|
"fmt"
|
2018-08-05 10:23:39 -03:00
|
|
|
"io"
|
2023-04-07 22:53:15 -03:00
|
|
|
"io/fs"
|
2018-08-05 10:23:39 -03:00
|
|
|
"os"
|
2022-11-29 19:05:18 -06:00
|
|
|
"path/filepath"
|
2021-07-21 22:09:02 -03:00
|
|
|
|
2024-05-26 15:02:57 -03:00
|
|
|
"github.com/goreleaser/goreleaser/v2/pkg/config"
|
2018-08-05 10:23:39 -03:00
|
|
|
)
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Archive zip struct.
|
2018-08-05 10:23:39 -03:00
|
|
|
type Archive struct {
|
2023-04-07 22:53:15 -03:00
|
|
|
z *zip.Writer
|
|
|
|
files map[string]bool
|
2018-08-05 10:23:39 -03:00
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// New zip archive.
|
2018-08-05 10:23:39 -03:00
|
|
|
func New(target io.Writer) Archive {
|
2020-04-01 15:05:20 -04:00
|
|
|
compressor := zip.NewWriter(target)
|
|
|
|
compressor.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
|
|
|
|
return flate.NewWriter(out, flate.BestCompression)
|
|
|
|
})
|
2018-08-05 10:23:39 -03:00
|
|
|
return Archive{
|
2023-04-07 22:53:15 -03:00
|
|
|
z: compressor,
|
|
|
|
files: map[string]bool{},
|
2018-08-05 10:23:39 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-31 13:53:27 +02:00
|
|
|
func Copy(source *os.File, target io.Writer) (Archive, error) {
|
2023-04-07 22:53:15 -03:00
|
|
|
info, err := source.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return Archive{}, err
|
|
|
|
}
|
|
|
|
r, err := zip.NewReader(source, info.Size())
|
|
|
|
if err != nil {
|
|
|
|
return Archive{}, err
|
|
|
|
}
|
|
|
|
w := New(target)
|
|
|
|
for _, zf := range r.File {
|
|
|
|
w.files[zf.Name] = true
|
|
|
|
hdr := zip.FileHeader{
|
|
|
|
Name: zf.Name,
|
|
|
|
UncompressedSize64: zf.UncompressedSize64,
|
|
|
|
UncompressedSize: zf.UncompressedSize,
|
|
|
|
CreatorVersion: zf.CreatorVersion,
|
|
|
|
ExternalAttrs: zf.ExternalAttrs,
|
|
|
|
}
|
|
|
|
ww, err := w.z.CreateHeader(&hdr)
|
|
|
|
if err != nil {
|
|
|
|
return Archive{}, fmt.Errorf("creating %q header in target: %w", zf.Name, err)
|
|
|
|
}
|
2023-04-13 11:44:02 -03:00
|
|
|
if zf.Mode().IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
2023-04-07 22:53:15 -03:00
|
|
|
rr, err := zf.Open()
|
|
|
|
if err != nil {
|
|
|
|
return Archive{}, fmt.Errorf("opening %q from source: %w", zf.Name, err)
|
|
|
|
}
|
|
|
|
defer rr.Close()
|
|
|
|
if _, err = io.Copy(ww, rr); err != nil {
|
|
|
|
return Archive{}, fmt.Errorf("copy from %q source to target: %w", zf.Name, err)
|
|
|
|
}
|
|
|
|
_ = rr.Close()
|
|
|
|
}
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2021-08-31 23:19:10 -03:00
|
|
|
// Close all closeables.
|
|
|
|
func (a Archive) Close() error {
|
|
|
|
return a.z.Close()
|
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Add a file to the zip archive.
|
2021-07-21 22:09:02 -03:00
|
|
|
func (a Archive) Add(f config.File) error {
|
2023-04-07 22:53:15 -03:00
|
|
|
if _, ok := a.files[f.Destination]; ok {
|
|
|
|
return &fs.PathError{Err: fs.ErrExist, Path: f.Destination, Op: "add"}
|
|
|
|
}
|
|
|
|
a.files[f.Destination] = true
|
2021-08-31 23:08:07 -03:00
|
|
|
info, err := os.Lstat(f.Source) // #nosec
|
2018-08-05 10:23:39 -03:00
|
|
|
if err != nil {
|
2021-07-21 22:09:02 -03:00
|
|
|
return err
|
2018-08-05 10:23:39 -03:00
|
|
|
}
|
|
|
|
if info.IsDir() {
|
2021-07-21 22:09:02 -03:00
|
|
|
return err
|
2018-08-05 10:23:39 -03:00
|
|
|
}
|
|
|
|
header, err := zip.FileInfoHeader(info)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-21 22:09:02 -03:00
|
|
|
header.Name = f.Destination
|
2018-08-06 14:37:37 -04:00
|
|
|
header.Method = zip.Deflate
|
2022-12-14 12:16:43 -03:00
|
|
|
if !f.Info.ParsedMTime.IsZero() {
|
|
|
|
header.Modified = f.Info.ParsedMTime
|
2021-07-21 22:09:02 -03:00
|
|
|
}
|
|
|
|
if f.Info.Mode != 0 {
|
|
|
|
header.SetMode(f.Info.Mode)
|
|
|
|
}
|
2018-08-05 10:23:39 -03:00
|
|
|
w, err := a.z.CreateHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-29 19:05:18 -06:00
|
|
|
if info.IsDir() {
|
2022-05-13 13:55:01 -03:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-29 19:05:18 -06:00
|
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
|
|
link, err := os.Readlink(f.Source) // #nosec
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", f.Source, err)
|
|
|
|
}
|
|
|
|
_, err = io.WriteString(w, filepath.ToSlash(link))
|
|
|
|
return err
|
|
|
|
}
|
2022-05-13 13:55:01 -03:00
|
|
|
file, err := os.Open(f.Source) // #nosec
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2018-08-05 10:23:39 -03:00
|
|
|
_, err = io.Copy(w, file)
|
|
|
|
return err
|
|
|
|
}
|
2021-07-21 22:09:02 -03:00
|
|
|
|
|
|
|
// TODO: test fileinfo stuff
|