doubiao1734 2014-01-11 10:02
浏览 356
已采纳

在Golang中复制文件的简单方法

Is there any simple/fast way to copy a file in Go?

I couldn't find a fast way in the Doc's and searching the internet doesn't help as well.

  • 写回答

9条回答 默认 最新

  • douhuan7862 2014-01-11 20:59
    关注

    Warning: This answer is mainly about adding a second link to a file, not about copying the contents.

    A robust and efficient copy is conceptually simple, but not simple to implement due to the need to handle a number of edge cases and system limitations that are imposed by the target operating system and it's configuration.

    If you simply want to make a duplicate of the existing file you can use os.Link(srcName, dstName). This avoids having to move bytes around in the application and saves disk space. For large files, this is a significant time and space saving.

    But various operating systems have different restrictions on how hard links work. Depending on your application and your target system configuration, Link() calls may not work in all cases.

    If you want a single generic, robust and efficient copy function, update Copy() to:

    1. Perform checks to ensure that at least some form of copy will succeed (access permissions, directories exist, etc.)
    2. Check to see if both files already exist and are the same using os.SameFile, return success if they are the same
    3. Attempt a Link, return if success
    4. Copy the bytes (all efficient means failed), return result

    An optimization would be to copy the bytes in a go routine so the caller doesn't block on the byte copy. Doing so imposes additional complexity on the caller to handle the success/error case properly.

    If I wanted both, I would have two different copy functions: CopyFile(src, dst string) (error) for a blocking copy and CopyFileAsync(src, dst string) (chan c, error) which passes a signaling channel back to the caller for the asynchronous case.

    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
    ", os.Args[1], os.Args[2])
        err := CopyFile(os.Args[1], os.Args[2])
        if err != nil {
            fmt.Printf("CopyFile failed %q
    ", err)
        } else {
            fmt.Printf("CopyFile succeeded
    ")
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错