Replace the combination of os. ReadFile() and os. WriteFile() with io. Copy()

Use io. Copy() instead of the combination of os. ReadFile() and os. WriteFile() to avoid reading the entire file into memory and writing it back to disk. This can reduce memory usage and improve efficiency
pull/223/head
zengwei2000 2 years ago committed by GitHub
parent 3eaa2dd2bf
commit a877b44caf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      utils/fileutil/filetutil.go

@ -96,15 +96,26 @@ func CopyDirHasSuffix(src, dst, suffix string) error {
// CopyFile copies the file from the source to the destination
func CopyFile(src, dst string) error {
s, err := os.ReadFile(src)
if err != nil {
return err
}
err = os.WriteFile(dst, s, 0o600)
if err != nil {
return err
}
return nil
fsrc, err := os.Open(src)
if err != nil {
return err
}
defer fsrc.Close()
fdst, err := os.Create(dst)
if err != nil {
return err
}
defer fdst.Close()
if _, err := io.Copy(fdst, fsrc); err != nil {
return err
}
if err := fdst.Sync(); err != nil {
return err
}
return nil
}
// ItemName returns the filename from the provided path

Loading…
Cancel
Save