Skip to content

Commit

Permalink
add BrCompressFile and DirIterFilesRecursively
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Oct 13, 2023
1 parent 7760ee9 commit 683d307
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions u/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,15 @@ func BrCompressData(d []byte) ([]byte, error) {
}
return dst.Bytes(), nil
}

func BrCompressFile(path string) error {
d, err := os.ReadFile(path)
if err != nil {
return err
}
d2, err := BrCompressData(d)
if err != nil {
return err
}
return os.WriteFile(path+".br", d2, 0644)
}
29 changes: 29 additions & 0 deletions u/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha1"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -151,3 +152,31 @@ func ReadZipFile(path string) (map[string][]byte, error) {
}
return res, nil
}

func DirIterFilesRecursively(dir string, cb func(path string, de fs.DirEntry) (bool, error)) error {
dirsToVisit := []string{dir}
for len(dirsToVisit) > 0 {
dir = dirsToVisit[0]
dirsToVisit = dirsToVisit[1:]
entries, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, de := range entries {
name := de.Name()
if de.IsDir() {
newDir := filepath.Join(dir, name)
dirsToVisit = append(dirsToVisit, newDir)
continue
}
shouldStop, err := cb(filepath.Join(dir, name), de)
if err != nil {
return err
}
if shouldStop {
return nil
}
}
}
return nil
}

0 comments on commit 683d307

Please sign in to comment.