2021-10-25 11:45:21 +08:00
|
|
|
|
# Gloang 文件操作
|
|
|
|
|
|
|
|
|
|
## 文件删除
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
package main
|
|
|
|
|
import "os"
|
|
|
|
|
func main () {
|
|
|
|
|
err := os.Remove(logFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// 删除失败
|
|
|
|
|
} else {
|
|
|
|
|
// 删除成功
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 检查指定路径是否为文件夹
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func IsDir(name string) bool {
|
|
|
|
|
if info, err := os.Stat(name); err == nil {
|
|
|
|
|
return info.IsDir()
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 检查文件是否存在
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func IsPathExist(path string) bool {
|
|
|
|
|
_, e := os.Stat(path)
|
|
|
|
|
if e != nil {
|
|
|
|
|
if os.IsExist(e) {
|
|
|
|
|
return true
|
|
|
|
|
} else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 创建文件夹(如果文件夹不存在则创建)
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func MakeDir(dir string) error {
|
|
|
|
|
if !IsPathExist(dir) {
|
|
|
|
|
if err := os.MkdirAll(dir, 0755); err != nil { //os.ModePerm
|
|
|
|
|
fmt.Println("MakeDir failed:", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 复制文件
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
// 使用 io.Copy
|
|
|
|
|
func CopyFile(src, des string) (written int64, err error) {
|
|
|
|
|
srcFile, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
defer srcFile.Close()
|
|
|
|
|
|
|
|
|
|
// 获取源文件的权限
|
|
|
|
|
fi, _ := srcFile.Stat()
|
|
|
|
|
perm := fi.Mode()
|
|
|
|
|
|
|
|
|
|
// desFile, err := os.Create(des) // 无法复制源文件的所有权限
|
|
|
|
|
desFile, err := os.OpenFile(des, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm) // 复制源文件的所有权限
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
defer desFile.Close()
|
|
|
|
|
|
|
|
|
|
return io.Copy(desFile, srcFile)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 18:11:46 +08:00
|
|
|
|
// 使用 os.WriteFile() 和 os.ReadFile()
|
2021-10-25 11:45:21 +08:00
|
|
|
|
func CopyFile2(src, des string) (written int64, err error) {
|
|
|
|
|
// 获取源文件的权限
|
|
|
|
|
srcFile, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
fi, _ := srcFile.Stat()
|
|
|
|
|
perm := fi.Mode()
|
|
|
|
|
srcFile.Close()
|
|
|
|
|
|
2022-01-13 18:11:46 +08:00
|
|
|
|
input, err := os.ReadFile(src)
|
2021-10-25 11:45:21 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 18:11:46 +08:00
|
|
|
|
err = os.WriteFile(des, input, perm)
|
2021-10-25 11:45:21 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int64(len(input)), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用 os.Read() 和 os.Write()
|
|
|
|
|
func CopyFile3(src, des string, bufSize int) (written int64, err error) {
|
|
|
|
|
if bufSize <= 0 {
|
|
|
|
|
bufSize = 1 * 1024 * 1024 // 1M
|
|
|
|
|
}
|
|
|
|
|
buf := make([]byte, bufSize)
|
|
|
|
|
|
|
|
|
|
srcFile, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
defer srcFile.Close()
|
|
|
|
|
|
|
|
|
|
// 获取源文件的权限
|
|
|
|
|
fi, _ := srcFile.Stat()
|
|
|
|
|
perm := fi.Mode()
|
|
|
|
|
|
|
|
|
|
desFile, err := os.OpenFile(des, os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
defer desFile.Close()
|
|
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
|
for {
|
|
|
|
|
n, err := srcFile.Read(buf)
|
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n == 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if wn, err := desFile.Write(buf[:n]); err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
} else {
|
|
|
|
|
count += wn
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int64(count), nil
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 复制整个文件夹
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func CopyDir(srcPath, desPath string) error {
|
|
|
|
|
// 检查目录是否正确
|
|
|
|
|
if srcInfo, err := os.Stat(srcPath); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
if !srcInfo.IsDir() {
|
|
|
|
|
return errors.New("源路径不是一个正确的目录!")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if desInfo, err := os.Stat(desPath); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
if !desInfo.IsDir() {
|
|
|
|
|
return errors.New("目标路径不是一个正确的目录!")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.TrimSpace(srcPath) == strings.TrimSpace(desPath) {
|
|
|
|
|
return errors.New("源路径与目标路径不能相同!")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
|
|
|
|
|
if f == nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 复制目录是将源目录中的子目录复制到目标路径中,不包含源目录本身
|
|
|
|
|
if path == srcPath {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 生成新路径
|
|
|
|
|
destNewPath := strings.Replace(path, srcPath, desPath, -1)
|
|
|
|
|
|
|
|
|
|
if !f.IsDir() {
|
|
|
|
|
CopyFile(path, destNewPath)
|
|
|
|
|
} else {
|
|
|
|
|
if !FileIsExisted(destNewPath) {
|
|
|
|
|
return MakeDir(destNewPath)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 遍历指定文件夹中的所有文件(不进入下一级子目录)
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
/* 获取指定路径下的所有文件,只搜索当前路径,不进入下一级目录,可匹配后缀过滤(suffix为空则不过滤)*/
|
|
|
|
|
func ListDir(dir, suffix string) (files []string, err error) {
|
|
|
|
|
files = []string{}
|
|
|
|
|
|
2022-01-13 18:11:46 +08:00
|
|
|
|
_dir, err := os.ReadDir(dir)
|
2021-10-25 11:45:21 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
suffix = strings.ToLower(suffix) //匹配后缀
|
|
|
|
|
|
|
|
|
|
for _, _file := range _dir {
|
|
|
|
|
if _file.IsDir() {
|
|
|
|
|
continue //忽略目录
|
|
|
|
|
}
|
|
|
|
|
if len(suffix) == 0 || strings.HasSuffix(strings.ToLower(_file.Name()), suffix) {
|
|
|
|
|
//文件后缀匹配
|
|
|
|
|
files = append(files, path.Join(dir, _file.Name()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 遍历指定路径及其子目录中的所有文件
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
/* 获取指定路径下以及所有子目录下的所有文件,可匹配后缀过滤(suffix为空则不过滤)*/
|
|
|
|
|
func WalkDir(dir, suffix string) (files []string, err error) {
|
|
|
|
|
files = []string{}
|
|
|
|
|
err = filepath.Walk(dir, func(fname string, fi os.FileInfo, err error) error {
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
|
// 忽略目录
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(suffix) == 0 || strings.HasSuffix(strings.ToLower(fi.Name()), suffix) {
|
|
|
|
|
// 文件后缀匹配
|
|
|
|
|
files = append(files, fname)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return files, err
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 删除文件夹及其包含的所有子目录和所有文件
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
|
```
|