github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zfile/handle.go (about) 1 package zfile 2 3 import ( 4 "bufio" 5 "io" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 ) 10 11 // CopyDir copies the source directory to the dest directory. 12 func CopyDir(source string, dest string, filterFn ...func(srcFilePath, destFilePath string) bool) (err error) { 13 info, err := os.Stat(source) 14 if err != nil { 15 return err 16 } 17 err = os.MkdirAll(dest, info.Mode()) 18 if err != nil { 19 return err 20 } 21 directory, err := os.Open(source) 22 if err != nil { 23 return err 24 } 25 defer directory.Close() 26 objects, err := directory.Readdir(-1) 27 if err != nil { 28 return err 29 } 30 var filter func(srcFilePath, destFilePath string) bool 31 if len(filterFn) > 0 { 32 filter = filterFn[0] 33 } 34 copySum := len(objects) 35 for _, obj := range objects { 36 srcFilePath := filepath.Join(source, obj.Name()) 37 destFilePath := filepath.Join(dest, obj.Name()) 38 if obj.IsDir() { 39 _ = CopyDir(srcFilePath, destFilePath, filterFn...) 40 } else if filter == nil || filter(srcFilePath, destFilePath) { 41 _ = CopyFile(srcFilePath, destFilePath) 42 } else { 43 copySum-- 44 } 45 } 46 if copySum < 1 { 47 Rmdir(dest) 48 } 49 return nil 50 } 51 52 // ReadFile ReadFile 53 func ReadFile(path string) ([]byte, error) { 54 path = RealPath(path) 55 return ioutil.ReadFile(path) 56 } 57 58 // ReadLineFile ReadLineFile 59 func ReadLineFile(path string, handle func(line int, data []byte) error) (err error) { 60 var f *os.File 61 f, err = os.Open(RealPath(path)) 62 if err != nil { 63 return 64 } 65 defer f.Close() 66 67 rd := bufio.NewReader(f) 68 i := 0 69 for { 70 i++ 71 line, lerr := rd.ReadBytes('\n') 72 if err = handle(i, line); err != nil { 73 break 74 } 75 if lerr != nil || io.EOF == lerr { 76 break 77 } 78 } 79 return 80 } 81 82 // WriteFile WriteFile 83 func WriteFile(path string, b []byte, isAppend ...bool) (err error) { 84 var file *os.File 85 path = RealPath(path) 86 if FileExist(path) { 87 if len(isAppend) > 0 && isAppend[0] { 88 file, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, os.ModeAppend) 89 } else { 90 file, err = os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, os.ModeExclusive) 91 } 92 } else { 93 _ = RealPathMkdir(filepath.Dir(path)) 94 file, err = os.Create(path) 95 } 96 if err != nil { 97 return err 98 } 99 _, err = file.Write(b) 100 file.Close() 101 return err 102 } 103 104 // PutOffset open the specified file and write data from the specified location 105 func PutOffset(path string, b []byte, offset int64) (err error) { 106 var file *os.File 107 path = RealPath(path) 108 if FileExist(path) { 109 file, err = os.OpenFile(path, os.O_WRONLY, os.ModeAppend) 110 } else { 111 file, err = os.Create(path) 112 } 113 if err != nil { 114 return err 115 } 116 defer file.Close() 117 _, err = file.WriteAt(b, offset) 118 return err 119 } 120 121 // PutAppend open the specified file and write data at the end of the file 122 func PutAppend(path string, b []byte) (err error) { 123 var file *os.File 124 path = RealPath(path) 125 if FileExist(path) { 126 file, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, os.ModeAppend) 127 } else { 128 _ = RealPathMkdir(filepath.Dir(path)) 129 file, err = os.Create(path) 130 } 131 if err != nil { 132 return err 133 } 134 defer file.Close() 135 _, err = file.Write(b) 136 return err 137 }