Skip to content

Commit

Permalink
ADD: ioutil.CopyFile() & ioutil.CreateFileWithData()
Browse files Browse the repository at this point in the history
  • Loading branch information
whoisnian committed Apr 19, 2023
1 parent ce974cc commit b0675ad
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions util/ioutil/ioutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,32 @@ func SeekAndReadAll(fi *os.File) ([]byte, error) {
}
return io.ReadAll(fi)
}

// CopyFile reads file content and writes to another file.
func CopyFile(fromPath, toPath string) (int64, error) {
from, err := os.Open(fromPath)
if err != nil {
return 0, err
}
defer from.Close()

to, err := os.Create(toPath)
if err != nil {
return 0, err
}
defer to.Close()

// TODO: speedtest with different buf sizes
return io.Copy(to, from)
}

// CreateFileWithData
func CreateFileWithData(filePath string, data []byte) (int, error) {
fi, err := os.Create(filePath)
if err != nil {
return 0, err
}
defer fi.Close()

return fi.Write(data)
}

0 comments on commit b0675ad

Please sign in to comment.