Fix zip empty dir bug

This commit is contained in:
Liang Ding 2015-11-20 09:13:34 +08:00
parent 25b628c844
commit bf7ebfd618
2 changed files with 64 additions and 11 deletions

View File

@ -41,7 +41,7 @@ type ZipFile struct {
// Create creates a zip file with the specified filename.
func (*myzip) Create(filename string) (*ZipFile, error) {
file, err := os.Create(filename)
if err != nil {
if nil != err {
return nil, err
}
@ -63,7 +63,7 @@ func (z *ZipFile) AddEntryN(path string, names ...string) error {
for _, name := range names {
zipPath := filepath.Join(path, name)
err := z.AddEntry(zipPath, name)
if err != nil {
if nil != err {
return err
}
}
@ -73,12 +73,12 @@ func (z *ZipFile) AddEntryN(path string, names ...string) error {
// AddEntry adds a entry.
func (z *ZipFile) AddEntry(path, name string) error {
fi, err := os.Stat(name)
if err != nil {
if nil != err {
return err
}
fh, err := zip.FileInfoHeader(fi)
if err != nil {
if nil != err {
return err
}
@ -90,7 +90,7 @@ func (z *ZipFile) AddEntry(path, name string) error {
}
entry, err := z.writer.CreateHeader(fh)
if err != nil {
if nil != err {
return err
}
@ -99,7 +99,7 @@ func (z *ZipFile) AddEntry(path, name string) error {
}
file, err := os.Open(name)
if err != nil {
if nil != err {
return err
}
defer file.Close()
@ -113,7 +113,7 @@ func (z *ZipFile) AddEntry(path, name string) error {
func (z *ZipFile) AddDirectoryN(path string, names ...string) error {
for _, name := range names {
err := z.AddDirectory(path, name)
if err != nil {
if nil != err {
return err
}
}
@ -123,23 +123,31 @@ 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)
if err != nil {
if nil != err {
return err
}
if 0 == len(files) {
err := z.AddEntry(path, dirName)
if nil != err {
return err
}
return nil
}
for _, file := range files {
localPath := filepath.Join(dirName, file.Name())
zipPath := filepath.Join(path, file.Name())
err = nil
if file.IsDir() {
z.AddEntry(path, dirName)
err = z.AddDirectory(zipPath, localPath)
} else {
err = z.AddEntry(zipPath, localPath)
}
if err != nil {
if nil != err {
return err
}
}

View File

@ -55,6 +55,51 @@ func TestUnzip(t *testing.T) {
}
}
func TestEmptyDir(t *testing.T) {
err := os.MkdirAll(packageName+"/dir/subDir1", 644)
if nil != err {
t.Error(err)
return
}
err = os.MkdirAll(packageName+"/dir/subDir2", 644)
if nil != err {
t.Error(err)
return
}
f, err := os.Create(packageName + "/dir/subDir2/file")
if nil != err {
t.Error(err)
return
}
f.Close()
zipFile, err := Zip.Create(packageName + "/dir.zip")
if nil != err {
t.Error(err)
return
}
zipFile.AddDirectoryN("dir", packageName+"/dir")
if nil != err {
t.Error(err)
return
}
err = zipFile.Close()
if nil != err {
t.Error(err)
return
}
}
func TestMain(m *testing.M) {
logger.Info(testDir)