Documentation ¶
Overview ¶
Package fsutil Filesystem util functions, quick create, read and write file. eg: file and dir check, operate
Index ¶
- Constants
- Variables
- func CopyFile(srcPath, dstPath string) error
- func CreateFile(fpath string, filePerm, dirPerm os.FileMode, fileFlag ...int) (*os.File, error)
- func DeleteIfExist(fPath string) error
- func DeleteIfFileExist(fPath string) error
- func Dir(fpath string) string
- func DiscardReader(src io.Reader)
- func Expand(pathStr string) string
- func ExpandPath(pathStr string) string
- func FileExists(path string) bool
- func FileExt(fpath string) string
- func FindInDir(dir string, handleFn HandleFunc, filters ...FilterFunc) (e error)
- func GetContents(in any) []byte
- func GlobWithFunc(pattern string, fn func(filePath string) error) (err error)
- func IsAbsPath(aPath string) bool
- func IsDir(path string) bool
- func IsFile(path string) bool
- func IsImageFile(path string) bool
- func IsZipFile(filepath string) bool
- func JoinPaths(elem ...string) string
- func LineScanner(in any) *bufio.Scanner
- func MimeType(path string) (mime string)
- func MkDirs(perm os.FileMode, dirPaths ...string) error
- func MkParentDir(fpath string) error
- func MkSubDirs(perm os.FileMode, parentDir string, subDirs ...string) error
- func Mkdir(dirPath string, perm os.FileMode) error
- func MustCopyFile(srcPath, dstPath string)
- func MustCreateFile(filePath string, filePerm, dirPerm os.FileMode) *os.File
- func MustReadFile(filePath string) []byte
- func MustReadReader(r io.Reader) []byte
- func MustRemove(fPath string)
- func Name(fpath string) string
- func NewIOReader(in any) (r io.Reader, err error)
- func OSTempDir(pattern string) (string, error)
- func OSTempFile(pattern string) (*os.File, error)
- func OpenFile(filepath string, flag int, perm os.FileMode) (*os.File, error)
- func OpenReadFile(filepath string) (*os.File, error)
- func PathExists(path string) bool
- func PathName(fpath string) string
- func PutContents(filePath string, data any, fileFlag ...int) (int, error)
- func QuickOpenFile(filepath string, fileFlag ...int) (*os.File, error)
- func QuietRemove(fPath string)
- func ReadExistFile(filePath string) []byte
- func ReadString(in any) string
- func ReaderMimeType(r io.Reader) (mime string)
- func Realpath(pathStr string) string
- func Remove(fPath string) error
- func RmFileIfExist(fPath string) error
- func RmIfExist(fPath string) error
- func SplitPath(pathStr string) (dir, name string)
- func Suffix(fpath string) string
- func TempDir(dir, pattern string) (string, error)
- func TempFile(dir, pattern string) (*os.File, error)
- func TextScanner(in any) *scanner.Scanner
- func Unzip(archive, targetDir string) (err error)
- func WriteFile(filePath string, data any, perm os.FileMode, fileFlag ...int) error
- func WriteOSFile(f *os.File, data any) (n int, err error)
- type FilterFunc
- type HandleFunc
Constants ¶
const ( FsCWAFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND // create, append write-only FsCWTFlags = os.O_CREATE | os.O_WRONLY | os.O_TRUNC // create, override write-only FsCWFlags = os.O_CREATE | os.O_WRONLY // create, write-only FsRFlags = os.O_RDONLY // read-only )
some flag consts for open file
const (
// MimeSniffLen sniff Length, use for detect file mime type
MimeSniffLen = 512
)
Variables ¶
var ( DefaultDirPerm os.FileMode = 0775 DefaultFilePerm os.FileMode = 0665 OnlyReadFilePerm os.FileMode = 0444 )
perm for create dir or file
var ( // DefaultFileFlags for create and write DefaultFileFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND // OnlyReadFileFlags open file for read OnlyReadFileFlags = os.O_RDONLY )
var ( DirExist = IsDir FileExist = IsFile PathExist = PathExists )
alias methods
var ( // MustRm removes the named file or (empty) directory. MustRm = MustRemove // QuietRm removes the named file or (empty) directory. QuietRm = QuietRemove )
alias methods
var ImageMimeTypes = map[string]string{
"bmp": "image/bmp",
"gif": "image/gif",
"ief": "image/ief",
"jpg": "image/jpeg",
"jpeg": "image/jpeg",
"png": "image/png",
"svg": "image/svg+xml",
"ico": "image/x-icon",
"webp": "image/webp",
}
ImageMimeTypes refer net/http package
Functions ¶
func CreateFile ¶
CreateFile create file if not exists
Usage:
CreateFile("path/to/file.txt", 0664, 0666)
func DeleteIfExist ¶
DeleteIfExist removes the named file or (empty) directory on exists.
func DeleteIfFileExist ¶
DeleteIfFileExist removes the named file on exists.
func ExpandPath ¶
ExpandPath will parse `~` as user home dir path.
func FileExists ¶
FileExists reports whether the named file or directory exists.
func FindInDir ¶
func FindInDir(dir string, handleFn HandleFunc, filters ...FilterFunc) (e error)
FindInDir code refer the go pkg: path/filepath.glob()
filters: return false will skip the file.
func GetContents ¶
GetContents read contents from path or io.Reader, will panic on in type error
func GlobWithFunc ¶
GlobWithFunc handle matched file
func IsZipFile ¶
IsZipFile check is zip file. from https://blog.csdn.net/wangshubo1989/article/details/71743374
func LineScanner ¶
LineScanner create from filepath or io.Reader
s := fsutil.LineScanner("/path/to/file") for s.Scan() { fmt.Println(s.Text()) }
func MustCopyFile ¶
func MustCopyFile(srcPath, dstPath string)
MustCopyFile copy file to another path.
func MustCreateFile ¶
MustCreateFile create file, will panic on error
func MustReadFile ¶
MustReadFile read file contents, will panic on error
func MustReadReader ¶
MustReadReader read contents from io.Reader, will panic on error
func MustRemove ¶
func MustRemove(fPath string)
MustRemove removes the named file or (empty) directory. NOTICE: will panic on error
func NewIOReader ¶
NewIOReader instance by input file path or io.Reader
func OSTempDir ¶
OSTempDir creates a new temp dir on os.TempDir and return the temp dir path
Usage:
fsutil.OSTempDir("example.*")
func OSTempFile ¶
OSTempFile create a temp file on os.TempDir()
Usage:
fsutil.OSTempFile("example.*.txt")
func OpenReadFile ¶
OpenReadFile like os.OpenFile, open file for read contents
func PathExists ¶
PathExists reports whether the named file or directory exists.
func PutContents ¶
PutContents create file and write contents to file at once.
data type allow: string, []byte, io.Reader
Tip: file flag default is FsCWAFlags
Usage:
fsutil.PutContents(filePath, contents, fsutil.FsCWTFlags)
func QuickOpenFile ¶
QuickOpenFile like os.OpenFile, open for write, if not exists, will create it.
Tip: file flag default is FsCWAFlags
func QuietRemove ¶
func QuietRemove(fPath string)
QuietRemove removes the named file or (empty) directory.
NOTICE: will ignore error
func ReadExistFile ¶
ReadExistFile read file contents if existed, will panic on error
func ReadString ¶
ReadString read contents from path or io.Reader, will panic on in type error
func ReaderMimeType ¶
ReaderMimeType get the io.Reader mimeType
Usage:
file, err := os.Open(filepath) if err != nil { return } mime := ReaderMimeType(file)
func Realpath ¶
Realpath returns the shortest path name equivalent to path by purely lexical processing.
func RmFileIfExist ¶
RmFileIfExist removes the named file on exists.
func SplitPath ¶
SplitPath splits path immediately following the final Separator, separating it into a directory and file name component
func TempDir ¶
TempDir creates a new temp dir and return the temp dir path
Usage:
fsutil.TempDir("", "example.*") fsutil.TempDir("testdata", "example.*")
func TempFile ¶
TempFile is like os.CreateTemp, but can custom temp dir.
Usage:
fsutil.TempFile("", "example.*.txt")
func TextScanner ¶
TextScanner from filepath or io.Reader, will panic on in type error
Usage:
s := fsutil.TextScanner("/path/to/file") for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() { fmt.Printf("%s: %s\n", s.Position, s.TokenText()) }
func Unzip ¶
Unzip a zip archive from https://blog.csdn.net/wangshubo1989/article/details/71743374
Types ¶
type FilterFunc ¶
FilterFunc type for FindInDir