mar*_*rkc 66
一个强大的和高效率副本的概念很简单,但并不容易实现,因为需要处理一些边缘情况和系统限制由目标操作系统强加的,它的配置.
如果您只想复制现有文件,则可以使用os.Link(srcName, dstName).这样可以避免在应用程序中移动字节并节省磁盘空间.对于大文件,这是一个显着的节省时间和空间.
但是各种操作系统对硬链接的工作方式有不同的限制.根据您的应用程序和目标系统配置,Link()呼叫可能无法在所有情况下都有效.
如果您想要一个通用,强大且高效的复制功能,请更新Copy()到:
os.SameFile,如果它们相同则返回成功优化是复制go例程中的字节,这样调用者就不会阻塞字节副本.这样做会给调用者带来额外的复杂性,以便正确处理成功/错误情况.
如果我想要两者,我将有两个不同的复制功能:CopyFile(src, dst string) (error)用于阻塞副本CopyFileAsync(src, dst string) (chan c, error),并将信令通道传递回调用程序以用于异步情况.
package main
import (
"fmt"
"io"
"os"
)
// CopyFile copies a file from src to dst. If src and dst files exist, and are
// the same, then return success. Otherise, attempt to create a hard link
// between the two files. If that fail, copy the file contents from src to dst.
func CopyFile(src, dst string) (err error) {
sfi, err := os.Stat(src)
if err != nil {
return
}
if !sfi.Mode().IsRegular() {
// cannot copy non-regular files (e.g., directories,
// symlinks, devices, etc.)
return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
}
dfi, err := os.Stat(dst)
if err != nil {
if !os.IsNotExist(err) {
return
}
} else {
if !(dfi.Mode().IsRegular()) {
return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
}
if os.SameFile(sfi, dfi) {
return
}
}
if err = os.Link(src, dst); err == nil {
return
}
err = copyFileContents(src, dst)
return
}
// copyFileContents copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file.
func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
err = out.Sync()
return
}
func main() {
fmt.Printf("Copying %s to %s\n", os.Args[1], os.Args[2])
err := CopyFile(os.Args[1], os.Args[2])
if err != nil {
fmt.Printf("CopyFile failed %q\n", err)
} else {
fmt.Printf("CopyFile succeeded\n")
}
}
Run Code Online (Sandbox Code Playgroud)
Pau*_*kin 45
你已经拥有了在标准库中编写这样一个函数所需的所有功能.这是明显的代码.
// Copy the src file to dst. Any existing file will be overwritten and will not
// copy file attributes.
func Copy(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*alf 11
如果你在linux/mac中运行代码,你可以只执行system的cp命令.
srcFolder := "copy/from/path"
destFolder := "copy/to/path"
cpCmd := exec.Command("cp", "-rf", srcFolder, destFolder)
err := cpCmd.Run()
Run Code Online (Sandbox Code Playgroud)
它的处理方式有点像脚本,但它完成了工作.此外,您需要导入"os/exec"
import (
"io/ioutil"
"log"
)
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func copy(src string, dst string) {
// Read all content of src to data
data, err := ioutil.ReadFile(src)
checkErr(err)
// Write data to dst
err = ioutil.WriteFile(dst, data, 0644)
checkErr(err)
}
Run Code Online (Sandbox Code Playgroud)
io.Copy。(*os.File).Close调用中的错误。io.Copy错误(*os.File).Close。Close对同一文件调用两次但忽略其中一次调用的错误。stat检查是否存在或文件类型。这些检查不是必需的:如果它不是该文件类型的有效操作,则 futureopen和read操作无论如何都会返回错误。其次,此类检查很容易出现竞争(例如,文件可能会在stat和之间的时间内被删除open)。// Copy copies the contents of the file at srcpath to a regular file
// at dstpath. If the file named by dstpath already exists, it is
// truncated. The function does not copy the file mode, file
// permission bits, or file attributes.
func Copy(srcpath, dstpath string) (err error) {
r, err := os.Open(srcpath)
if err != nil {
return err
}
defer r.Close() // ignore error: file was opened read-only.
w, err := os.Create(dstpath)
if err != nil {
return err
}
defer func() {
// Report the error, if any, from Close, but do so
// only if there isn't already an outgoing error.
if c := w.Close(); err == nil {
err = c
}
}()
_, err = io.Copy(w, r)
return err
}
Run Code Online (Sandbox Code Playgroud)
小智 6
从 Go 1.15(2020 年 8 月)开始,您可以使用File.ReadFrom:
package main
import "os"
func main() {
r, err := os.Open("in.txt")
if err != nil {
panic(err)
}
defer r.Close()
w, err := os.Create("out.txt")
if err != nil {
panic(err)
}
defer w.Close()
w.ReadFrom(r)
}
Run Code Online (Sandbox Code Playgroud)