From 84cdbe88b3b6472d7f2dfef498ef73108b3f38f2 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 20 Nov 2015 09:38:29 +0800 Subject: [PATCH] Fix unzip empty dir bug --- util/zip.go | 7 ++++++- util/zip_test.go | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/util/zip.go b/util/zip.go index e972f0e..5b32359 100644 --- a/util/zip.go +++ b/util/zip.go @@ -176,6 +176,11 @@ func cloneZipItem(f *zip.File, dest string) error { } if f.FileInfo().IsDir() { + err = os.Mkdir(path, os.ModeDir|os.ModePerm) + if nil != err { + return err + } + return nil } @@ -188,7 +193,7 @@ func cloneZipItem(f *zip.File, dest string) error { defer rc.Close() - // use os.Create() since Zip don't store file permissions. + // use os.Create() since Zip don't store file permissions fileCopy, err := os.Create(path) if nil != err { return err diff --git a/util/zip_test.go b/util/zip_test.go index 5da22d4..2acf25b 100644 --- a/util/zip_test.go +++ b/util/zip_test.go @@ -56,21 +56,24 @@ func TestUnzip(t *testing.T) { } func TestEmptyDir(t *testing.T) { - err := os.MkdirAll(packageName+"/dir/subDir1", 644) + dir1 := "/dir/subDir1" + dir2 := "/dir/subDir2" + + err := os.MkdirAll(packageName+dir1, 644) if nil != err { t.Error(err) return } - err = os.MkdirAll(packageName+"/dir/subDir2", 644) + err = os.MkdirAll(packageName+dir2, 644) if nil != err { t.Error(err) return } - f, err := os.Create(packageName + "/dir/subDir2/file") + f, err := os.Create(packageName + dir2 + "/file") if nil != err { t.Error(err) @@ -98,6 +101,37 @@ func TestEmptyDir(t *testing.T) { return } + + err = Zip.Unzip(packageName+"/dir.zip", packageName+"/unzipDir") + if nil != err { + t.Error(err) + + return + } + + if !File.IsExist(packageName+"/unzipDir") || !File.IsDir(packageName+"/unzipDir") { + t.Error("Unzip failed") + + return + } + + if !File.IsExist(packageName+"/unzipDir"+dir1) || !File.IsDir(packageName+"/unzipDir"+dir1) { + t.Error("Unzip failed") + + return + } + + if !File.IsExist(packageName+"/unzipDir"+dir2) || !File.IsDir(packageName+"/unzipDir"+dir2) { + t.Error("Unzip failed") + + return + } + + if !File.IsExist(packageName+"/unzipDir"+dir2+"/file") || File.IsDir(packageName+"/unzipDir"+dir2+"/file") { + t.Error("Unzip failed") + + return + } } func TestMain(m *testing.M) {