33 lines
539 B
Go
33 lines
539 B
Go
package archive
|
|
|
|
import (
|
|
"archive/zip"
|
|
"errors"
|
|
"io"
|
|
)
|
|
|
|
func WalkZipFile(filePath string, fn func(name string, r io.Reader) error) (err error) {
|
|
zf, err := zip.OpenReader(filePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer func() {
|
|
err = errors.Join(err, zf.Close())
|
|
}()
|
|
|
|
for _, f := range zf.File {
|
|
if fr, err := f.Open(); err != nil {
|
|
return err
|
|
} else {
|
|
if err := fn(f.Name, fr); err != nil {
|
|
return errors.Join(err, fr.Close())
|
|
}
|
|
|
|
if err := fr.Close(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|