From 7c618f46df6d64dd9e6ebc15f53253cf093a442d Mon Sep 17 00:00:00 2001 From: Thibault Richard Date: Wed, 28 Feb 2024 11:44:25 +0100 Subject: [PATCH] Create file headers with modified time set to now in zip archive (#221) This adds a `Create` method to the `ZipFile` struct, which overrides https://pkg.go.dev/archive/zip#Writer.Create in order to set the file's modification time to now. --- internal/archive/archive.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/archive/archive.go b/internal/archive/archive.go index 8e5bc45..167d652 100644 --- a/internal/archive/archive.go +++ b/internal/archive/archive.go @@ -11,6 +11,7 @@ import ( "log" "os" "strings" + "time" "k8s.io/apimachinery/pkg/util/errors" ) @@ -64,6 +65,17 @@ func NewZipFile(fileName string, version string, log *log.Logger) (*ZipFile, err }, nil } +// Create adds a file to the zip file using the provided name similarly to https://pkg.go.dev/archive/zip#Writer.Create, +// with the addition of the file modification time set to now. +func (z *ZipFile) Create(filename string) (io.Writer, error) { + header := &zip.FileHeader{ + Name: filename, + Method: zip.Deflate, + Modified: time.Now(), + } + return z.CreateHeader(header) +} + // Close closes the zip.Writer and the underlying file. func (z *ZipFile) Close() error { errs := []error{