Skip to content

Commit

Permalink
✨ Add callback func for zip
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Jun 12, 2024
1 parent bdeafab commit b3a7670
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
27 changes: 20 additions & 7 deletions zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (z *ZipFile) Close() error {
}

// AddEntry adds an entry.
func (z *ZipFile) AddEntry(path, name string) error {
func (z *ZipFile) AddEntry(path, name string, callback ...func(filename string)) error {
fi, err := os.Stat(name)
if nil != err {
return err
Expand Down Expand Up @@ -80,18 +80,22 @@ func (z *ZipFile) AddEntry(path, name string) error {
defer file.Close()

_, err = io.Copy(entry, file)

if 0 < len(callback) {
callback[0](name)
}
return err
}

// AddDirectory adds a directory.
func (z *ZipFile) AddDirectory(path, dirName string) error {
func (z *ZipFile) AddDirectory(path, dirName string, callback ...func(string)) error {
files, err := os.ReadDir(dirName)
if nil != err {
return err
}

if 0 == len(files) {
err = z.AddEntry(path, dirName)
err = z.AddEntry(path, dirName, callback...)
if nil != err {
return err
}
Expand All @@ -106,7 +110,7 @@ func (z *ZipFile) AddDirectory(path, dirName string) error {
if file.IsDir() {
err = z.AddDirectory(zipPath, localPath)
} else {
err = z.AddEntry(zipPath, localPath)
err = z.AddEntry(zipPath, localPath, callback...)
}

if nil != err {
Expand All @@ -117,24 +121,29 @@ func (z *ZipFile) AddDirectory(path, dirName string) error {
}

// Unzip extracts a zip file specified by the zipFilePath to the destination.
func (*GuluZip) Unzip(zipFilePath, destination string) error {
func (*GuluZip) Unzip(zipFilePath, destination string, callback ...func(filename string)) error {
r, err := zip.OpenReader(zipFilePath)

if nil != err {
return err
}
defer r.Close()

var cb func(string)
if 0 < len(callback) {
cb = callback[0]
}

for _, f := range r.File {
err = cloneZipItem(f, destination)
err = cloneZipItem(f, destination, cb)
if nil != err {
return err
}
}
return nil
}

func cloneZipItem(f *zip.File, dest string) error {
func cloneZipItem(f *zip.File, dest string, callback func(filename string)) error {
// create full directory path
fileName := f.Name

Expand Down Expand Up @@ -185,5 +194,9 @@ func cloneZipItem(f *zip.File, dest string) error {
if err = os.Chtimes(path, f.Modified, f.Modified); nil != err {
return err
}

if nil != callback {
callback(path)
}
return nil
}
8 changes: 6 additions & 2 deletions zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ func TestCreate(t *testing.T) {
return
}

zipFile.AddDirectory(".", testdataDir)
zipFile.AddDirectory(".", testdataDir, func(filename string) {
t.Logf("zipped %s", filename)
})
if nil != err {
t.Error(err)
return
Expand All @@ -38,7 +40,9 @@ func TestCreate(t *testing.T) {
return
}

err = Zip.Unzip(zipDirPath+".zip", zipDirPath)
err = Zip.Unzip(zipDirPath+".zip", zipDirPath, func(filename string) {
t.Logf("unzipped %s", filename)
})
if nil != err {
t.Error(err)
return
Expand Down

0 comments on commit b3a7670

Please sign in to comment.