Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes the issue with modified time affecting zip contents #16

Merged
merged 2 commits into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions archive/zip_archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"sort"
"time"
)

type ZipArchiver struct {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions archive/zip_archiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package archive

import (
"archive/zip"
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)

func TestZipArchiver_Content(t *testing.T) {
Expand All @@ -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"
Expand Down