searcherside/internal/archive/zip.go

34 lines
539 B
Go
Raw Normal View History

2024-06-06 20:08:51 +00:00
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
}