func WalkDirZip(fsys fs.FS, root string, fn WalkDirFunc) error { return fs.WalkDir(fsys, root, func(path string, d fs.DirEntry, err error) error { if d.IsDir() || !strings.EqualFold(filepath.Ext(path), ".zip") { return fn(fsys, path, d, err) } else if f, err := fsys.Open(path); err != nil { return err } else if s, err := f.Stat(); err != nil { return err } else if zfs, err := zip.NewReader(f.(io.ReaderAt), s.Size()); err != nil { return err } else { return fs.WalkDir(zfs, ".", func(path string, d fs.DirEntry, err error) error { return fn(zfs, path, d, err) }) } }) } type WalkDirFunc func(fsys fs.FS, path string, d fs.DirEntry, err error) error
Wednesday, February 16, 2022
Traverse into zip files when walking the file system
Subscribe to:
Posts (Atom)