Skip to content

Commit

Permalink
🎨 Unzip not change the file mod time
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Jun 15, 2023
1 parent 4409e11 commit b519d68
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
11 changes: 7 additions & 4 deletions zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"archive/zip"
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"unicode/utf8"
Expand Down Expand Up @@ -57,7 +56,7 @@ func (z *ZipFile) AddEntryN(path string, names ...string) error {
return nil
}

// AddEntry adds a entry.
// AddEntry adds an entry.
func (z *ZipFile) AddEntry(path, name string) error {
fi, err := os.Stat(name)
if nil != err {
Expand Down Expand Up @@ -108,7 +107,7 @@ func (z *ZipFile) AddDirectoryN(path string, names ...string) error {

// AddDirectory adds a directory.
func (z *ZipFile) AddDirectory(path, dirName string) error {
files, err := ioutil.ReadDir(dirName)
files, err := os.ReadDir(dirName)
if nil != err {
return err
}
Expand Down Expand Up @@ -144,7 +143,7 @@ func cloneZipItem(f *zip.File, dest string) error {
fileName := f.Name

if !utf8.ValidString(fileName) {
data, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader([]byte(fileName)), simplifiedchinese.GB18030.NewDecoder()))
data, err := io.ReadAll(transform.NewReader(bytes.NewReader([]byte(fileName)), simplifiedchinese.GB18030.NewDecoder()))
if nil == err {
fileName = string(data)
} else {
Expand Down Expand Up @@ -186,6 +185,10 @@ func cloneZipItem(f *zip.File, dest string) error {
if nil != err {
return err
}

if err = os.Chtimes(path, f.Modified, f.Modified); nil != err {
return err
}
return nil
}

Expand Down
27 changes: 14 additions & 13 deletions zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,33 @@ func TestCreate(t *testing.T) {
zipFile, err := Zip.Create(zipDirPath + ".zip")
if nil != err {
t.Error(err)

return
}

zipFile.AddDirectoryN(".", testdataDir)
if nil != err {
t.Error(err)

return
}

err = zipFile.Close()
if nil != err {
t.Error(err)
return
}

err = Zip.Unzip(zipDirPath+".zip", zipDirPath)
if nil != err {
t.Error(err)
return
}

f1, _ := os.Stat(filepath.Join(testdataDir, "README.md"))
f1ModTime := f1.ModTime()
f2, _ := os.Stat(filepath.Join(zipDirPath, "README.md"))
f2ModTime := f2.ModTime()
if f1ModTime.Unix() != f2ModTime.Unix() {
t.Error("ModTime error")
return
}
}
Expand All @@ -46,7 +58,6 @@ func TestUnzip(t *testing.T) {
err := Zip.Unzip(zipDirPath+".zip", zipDirPath)
if nil != err {
t.Error(err)

return
}
}
Expand All @@ -65,67 +76,57 @@ func _TestEmptyDir(t *testing.T) {
err = os.MkdirAll(zipDirPath+dir2, os.ModeDir)
if nil != err {
t.Error(err)

return
}

f, err := os.Create(zipDirPath + dir2 + "/file")
if nil != err {
t.Error(err)

return
}
f.Close()

zipFile, err := Zip.Create(zipDirPath + "/dir.zip")
if nil != err {
t.Error(err)

return
}

zipFile.AddDirectoryN("dir", zipDirPath+"/dir")
if nil != err {
t.Error(err)

return
}

err = zipFile.Close()
if nil != err {
t.Error(err)

return
}

err = Zip.Unzip(zipDirPath+"/dir.zip", zipDirPath+"/unzipDir")
if nil != err {
t.Error(err)

return
}

if !File.IsExist(zipDirPath+"/unzipDir") || !File.IsDir(zipDirPath+"/unzipDir") {
t.Error("Unzip failed")

return
}

if !File.IsExist(zipDirPath+"/unzipDir"+dir1) || !File.IsDir(zipDirPath+"/unzipDir"+dir1) {
t.Error("Unzip failed")

return
}

if !File.IsExist(zipDirPath+"/unzipDir"+dir2) || !File.IsDir(zipDirPath+"/unzipDir"+dir2) {
t.Error("Unzip failed")

return
}

if !File.IsExist(zipDirPath+"/unzipDir"+dir2+"/file") || File.IsDir(zipDirPath+"/unzipDir"+dir2+"/file") {
t.Error("Unzip failed")

return
}
}
Expand Down

0 comments on commit b519d68

Please sign in to comment.