小编典典

复制文件的简单方法

go

有什么简单/快速的方法可以在 Go 中复制文件?

我在 Doc 中找不到快速的方法,在互联网上搜索也无济于事。


阅读 261

收藏
2021-12-12

共2个答案

小编典典

警告:这个答案主要是关于向文件添加硬链接,而不是复制内容。

一个强大的高效率副本的概念很简单,但并不容易实现,因为需要处理一些边缘情况和系统限制由目标操作系统强加的,它的配置。

如果您只想复制现有文件,您可以使用os.Link(srcName, dstName). 这避免了必须在应用程序中移动字节并节省磁盘空间。对于大文件,这可以节省大量时间和空间。

但是各种操作系统对硬链接的工作方式有不同的限制。根据您的应用程序和目标系统配置,Link()调用可能并非在所有情况下都有效。

如果您想要一个通用、健壮且高效的复制功能,请更新Copy()为:

  1. 执行检查以确保至少某种形式的复制会成功(访问权限、目录存在等)
  2. 检查两个文件是否已经存在并且使用相同 os.SameFile,如果它们相同则返回成功
  3. 尝试链接,成功则返回
  4. 复制字节(所有有效手段失败),返回结果

优化是在 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")
    }
}
2021-12-12
小编典典

您已经在标准库中获得了编写此类函数所需的所有内容。这是执行此操作的明显代码。

// 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()
}
2021-12-12