github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/file/file.go (about)

     1  package file
     2  
     3  import (
     4  	"github.com/isyscore/isc-gobase/isc"
     5  	"io"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  )
    10  
    11  func FileExists(filePath string) bool {
    12  	if _, err := os.Stat(filePath); err != nil {
    13  		return os.IsExist(err)
    14  	}
    15  	return true
    16  }
    17  
    18  func DirectoryExists(dirPath string) bool {
    19  	if s, err := os.Stat(dirPath); err != nil {
    20  		return false
    21  	} else {
    22  		return s.IsDir()
    23  	}
    24  }
    25  
    26  func ExtractFilePath(filePath string) string {
    27  	idx := strings.LastIndex(filePath, string(os.PathSeparator))
    28  	return filePath[:idx]
    29  }
    30  
    31  func ExtractFileName(filePath string) string {
    32  	idx := strings.LastIndex(filePath, string(os.PathSeparator))
    33  	return filePath[idx+1:]
    34  }
    35  
    36  func ExtractFileExt(filePath string) string {
    37  	idx := strings.LastIndex(filePath, ".")
    38  	if idx != -1 {
    39  		return filePath[idx+1:]
    40  	}
    41  	return ""
    42  }
    43  
    44  func ChangeFileExt(filePath string, ext string) string {
    45  	mext := ExtractFileExt(filePath)
    46  	if mext == "" {
    47  		return filePath + "." + ext
    48  	} else {
    49  		return filePath[:len(filePath)-len(mext)] + ext
    50  	}
    51  }
    52  
    53  func MkDirs(path string) bool {
    54  	if !DirectoryExists(path) {
    55  		return os.MkdirAll(path, os.ModePerm) == nil
    56  	} else {
    57  		return false
    58  	}
    59  }
    60  
    61  func DeleteDirs(path string) bool {
    62  	return os.RemoveAll(path) == nil
    63  }
    64  
    65  func DeleteFile(filePath string) bool {
    66  	return os.Remove(filePath) == nil
    67  }
    68  
    69  func ReadFile(filePath string) string {
    70  	var ret = ""
    71  	if b, err := os.ReadFile(filePath); err == nil {
    72  		ret = string(b)
    73  	}
    74  	return ret
    75  }
    76  
    77  func ReadFileBytes(filePath string) []byte {
    78  	var ret []byte
    79  	if b, err := os.ReadFile(filePath); err == nil {
    80  		ret = b
    81  	}
    82  	return ret
    83  }
    84  
    85  func ReadFileLines(filePath string) []string {
    86  	var ret []string
    87  	if b, err := os.ReadFile(filePath); err == nil {
    88  		ret = strings.Split(string(b), "\n")
    89  	}
    90  	return ret
    91  }
    92  
    93  func WriteFile(filePath string, text string) bool {
    94  	return WriteFileBytes(filePath, []byte(text))
    95  }
    96  
    97  func WriteFileBytes(filePath string, data []byte) bool {
    98  	p0 := ExtractFilePath(filePath)
    99  	if !DirectoryExists(p0) {
   100  		MkDirs(p0)
   101  	}
   102  	if FileExists(filePath) {
   103  		DeleteFile(filePath)
   104  	}
   105  	if fl, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0644); err != nil {
   106  		return false
   107  	} else {
   108  		_, err := fl.Write(data)
   109  		_ = fl.Close()
   110  		return err == nil
   111  	}
   112  }
   113  
   114  func AppendFile(filePath string, text string) bool {
   115  	return AppendFileBytes(filePath, []byte(text))
   116  }
   117  
   118  func AppendFileBytes(filePath string, data []byte) bool {
   119  	p0 := ExtractFilePath(filePath)
   120  	if !DirectoryExists(p0) {
   121  		MkDirs(p0)
   122  	}
   123  	if fl, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil {
   124  		return false
   125  	} else {
   126  		_, err := fl.Write(data)
   127  		_ = fl.Close()
   128  		return err == nil
   129  	}
   130  }
   131  
   132  // Deprecated:this function is replaced by CopyFileWithError, which will return the reason for copy failure.
   133  func CopyFile(srcFilePath string, destFilePath string) bool {
   134  	p0 := ExtractFilePath(destFilePath)
   135  	if !DirectoryExists(p0) {
   136  		MkDirs(p0)
   137  	}
   138  	src, _ := os.Open(srcFilePath)
   139  	defer func(src *os.File) { _ = src.Close() }(src)
   140  	dst, _ := os.OpenFile(destFilePath, os.O_WRONLY|os.O_CREATE, 0644)
   141  	defer func(dst *os.File) { _ = dst.Close() }(dst)
   142  	_, err := io.Copy(dst, src)
   143  	return err == nil
   144  }
   145  
   146  func RenameFile(srcFilePath string, destFilePath string) bool {
   147  	p0 := ExtractFilePath(destFilePath)
   148  	if !DirectoryExists(p0) {
   149  		MkDirs(p0)
   150  	}
   151  	return os.Rename(srcFilePath, destFilePath) == nil
   152  }
   153  
   154  func CreateFile(filePath string) bool {
   155  	if FileExists(filePath) {
   156  		return true
   157  	}
   158  
   159  	p0 := ExtractFilePath(filePath)
   160  	if !DirectoryExists(p0) {
   161  		MkDirs(p0)
   162  	}
   163  
   164  	if _, err := os.OpenFile(filePath, os.O_CREATE, 0644); err != nil {
   165  		return false
   166  	} else {
   167  		return true
   168  	}
   169  }
   170  
   171  func Child(filePath string) ([]os.DirEntry, error) {
   172  	return os.ReadDir(filePath)
   173  }
   174  
   175  // Size 返回文件/目录的大小
   176  func Size(filePath string) int64 {
   177  	if !DirectoryExists(filePath) {
   178  		fi, err := os.Stat(filePath)
   179  		if err == nil {
   180  			return fi.Size()
   181  		}
   182  		return 0
   183  	} else {
   184  		var size int64
   185  		err := filepath.Walk(filePath, func(_ string, info os.FileInfo, err error) error {
   186  			if !info.IsDir() {
   187  				size += info.Size()
   188  			}
   189  			return err
   190  		})
   191  		if err != nil {
   192  			return 0
   193  		}
   194  		return size
   195  	}
   196  }
   197  
   198  func Sizes(filePaths ...string) int64 {
   199  	var count int64
   200  	for _, filePath := range filePaths {
   201  		count += Size(filePath)
   202  	}
   203  	return count
   204  }
   205  
   206  func SizeList(filePaths []string) int64 {
   207  	var count int64
   208  	for _, filePath := range filePaths {
   209  		count += Size(filePath)
   210  	}
   211  	return count
   212  }
   213  
   214  // SizeFormat 返回文件/目录的可读大小
   215  func SizeFormat(filePath string) string {
   216  	return isc.FormatSize(Size(filePath))
   217  }
   218  
   219  func CopyFileWithError(srcFilePath string, destFilePath string) error {
   220  	dirPath := ExtractFilePath(destFilePath)
   221  	var err error
   222  	if !DirectoryExists(dirPath) {
   223  		err = os.MkdirAll(dirPath, os.ModePerm)
   224  	}
   225  	if err != nil {
   226  		return err
   227  	}
   228  	src, err := os.Open(srcFilePath)
   229  	if err != nil {
   230  		return err
   231  	}
   232  	defer func(src *os.File) { _ = src.Close() }(src)
   233  	dst, err := os.OpenFile(destFilePath, os.O_WRONLY|os.O_CREATE, 0644)
   234  	if err != nil {
   235  		return err
   236  	}
   237  	defer func(dst *os.File) { _ = dst.Close() }(dst)
   238  	if _, err = io.Copy(dst, src); err != nil {
   239  		return err
   240  	}
   241  	return nil
   242  }