1 Star 0 Fork 1

技术狼/go-fun

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dir.go 3.35 KB
一键复制 编辑 原始数据 按行查看 历史
技术狼 提交于 1年前 . no message
package fun
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
type FileInfo struct {
Name string `json:"name"` // 文件名
Suffix string `json:"suffix"` // 后缀
Path string `json:"path"` // 文件路径
Size int64 `json:"size"` // 文件大小
ModTime time.Time `json:"mod_time"` // 修改时间
}
// @title: 目录是否存在
// @param: 路径(string)
// @return: 布尔, error
// @description:
// @date: 2024/6/11 22:32
func DirIsExist(dirPath string) (bool, error) {
info, err := os.Stat(dirPath)
if err != nil {
return false, err
}
if !info.IsDir() {
return false, errors.New(dirPath + "Already exists, but not a directory")
}
return true, nil
}
// @title: 创建目录
// @param: 目录路径(string),权限(os.FileMode)
// @return: error
// @description:
// @date: 2024/6/11 22:32
func DirCreate(dirPath string, perm os.FileMode) error {
return os.MkdirAll(dirPath, perm)
}
// @title: 目录复制
// @param: 拷贝路径(string),目标路径
// @return: error
// @description:
// @date: 2024/6/11 22:32
func DirCopy(srcPath string, destPath string) error {
var err error
_, err = DirIsExist(srcPath)
if err != nil {
return err
}
is, _ := DirIsExist(destPath)
if !is { // 不存在,就创建目录
err = DirCreate(destPath, 0766)
if err != nil {
return err // 目录创建失败
}
}
//
err = filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if !f.IsDir() {
path := strings.Replace(path, "\\", "/", -1)
destNewPath := DirEndsWithSlash(destPath)
destNewPath += f.Name()
err = FileCopy(path, destNewPath)
if err != nil {
return err
}
}
return nil
})
return err
}
// @title: 目录路径斜杠校正并以斜杠结尾
// @param: 路径(string)
// @return: 路径(string)
// @description:
// @date: 2024/6/11 22:32
func DirEndsWithSlash(dirPath string) string {
dirPath = strings.Replace(dirPath, "\\", "/", -1)
if !strings.HasSuffix(dirPath, "/") {
dirPath += "/"
}
return dirPath
}
// @title: 获取某目录指定后缀的所有文件
// @param: 目录路径(string),后缀(string)
// @return: 文件信息数组,错误信息
// @description:
// @date: 2024/6/11 22:32
func DirFilesBySuffix(dir string, suffix string) ([]FileInfo, error) {
files := []FileInfo{}
fis, err := ioutil.ReadDir(filepath.Clean(filepath.ToSlash(dir)))
if err != nil {
return files, err
}
for _, f := range fis {
_path := filepath.Join(dir, f.Name())
if f.IsDir() {
fs, _ := DirFilesBySuffix(_path, suffix)
files = append(files, fs...)
continue
}
// 指定格式
fileName := f.Name()
switch filepath.Ext(fileName) {
case suffix:
name := Rtrim(f.Name(), suffix)
item := FileInfo{
Name: name,
Path: _path,
Suffix: suffix,
Size: f.Size(),
ModTime: f.ModTime(),
}
files = append(files, item)
}
}
return files, nil
}
// @title: 列出指定目录的所有文件及目录
// @param: dirPath(目录路径)
// @return: []string
// @return: error
// @description:
// @date: 2024/7/7 17:00
func ListDirFiles(dirPath string) ([]string, error) {
f, err := os.Open(dirPath)
if err != nil {
return nil, err
}
defer f.Close()
names, err := f.Readdirnames(-1)
if err != nil {
return nil, err
}
sort.Strings(names)
return names, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jishulangcom/go-fun.git
git@gitee.com:jishulangcom/go-fun.git
jishulangcom
go-fun
go-fun
v0.0.4

搜索帮助