diff --git a/archive/zip_archiver.go b/archive/zip_archiver.go index 5f1adbfd..4a022748 100644 --- a/archive/zip_archiver.go +++ b/archive/zip_archiver.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "sort" + "time" ) type ZipArchiver struct { @@ -58,6 +59,8 @@ func (a *ZipArchiver) ArchiveFile(infilename string) error { } fh.Name = fi.Name() fh.Method = zip.Deflate + // fh.Modified alone isn't enough when using a zero value + fh.SetModTime(time.Time{}) f, err := a.writer.CreateHeader(fh) if err != nil { @@ -96,6 +99,9 @@ func (a *ZipArchiver) ArchiveDir(indirname string) error { } fh.Name = relname fh.Method = zip.Deflate + // fh.Modified alone isn't enough when using a zero value + fh.SetModTime(time.Time{}) + f, err := a.writer.CreateHeader(fh) if err != nil { return fmt.Errorf("error creating file inside archive: %s", err) diff --git a/archive/zip_archiver_test.go b/archive/zip_archiver_test.go index 34a27272..bd40aa1d 100644 --- a/archive/zip_archiver_test.go +++ b/archive/zip_archiver_test.go @@ -2,8 +2,12 @@ package archive import ( "archive/zip" + "bytes" "io/ioutil" + "os" + "path/filepath" "testing" + "time" ) func TestZipArchiver_Content(t *testing.T) { @@ -29,6 +33,43 @@ func TestZipArchiver_File(t *testing.T) { "test-file.txt": []byte("This is test content"), }) } +func TestZipArchiver_FileModified(t *testing.T) { + var ( + zipFilePath = filepath.FromSlash("archive-file.zip") + toZipPath = filepath.FromSlash("./test-fixtures/test-file.txt") + ) + + var zip = func() { + archiver := NewZipArchiver(zipFilePath) + if err := archiver.ArchiveFile(toZipPath); err != nil { + t.Fatalf("unexpected error: %s", err) + } + } + + zip() + + expectedContents, err := ioutil.ReadFile(zipFilePath) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + + //touch file modified, in the future just in case of weird race issues + newTime := time.Now().Add(1 * time.Hour) + if err := os.Chtimes(toZipPath, newTime, newTime); err != nil { + t.Fatalf("unexpected error: %s", err) + } + + zip() + + actualContents, err := ioutil.ReadFile(zipFilePath) + if err != nil { + t.Fatalf("unexpecte error: %s", err) + } + + if !bytes.Equal(expectedContents, actualContents) { + t.Fatalf("zip contents do not match, potentially a modified time issue") + } +} func TestZipArchiver_Dir(t *testing.T) { zipfilepath := "archive-dir.zip"