doom910730 2018-10-08 14:07
浏览 185
已采纳

如何将2个文件合并或合并为单个文件

I need to take tmp1.zip and append it's tmp1.signed file to the end of it; creating a new tmp1.zip.signed file using Go. It's essentially same as cat | sc I could call cmd line from Go, but that seems super inefficient (and cheesy).

So far

Google-ing the words "go combine files" et. al. yields minimal help.

But I have come across a couple of options that I have tried such as ..

    f, err := os.OpenFile("tmp1.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
    log.Fatal(err)
}
if _, err := f.Write([]byte("appended some data
")); err != nil {
    log.Fatal(err)
}
if err := f.Close(); err != nil {
    log.Fatal(err)
}

But that is just getting strings added to the end of the file, not really merging the two files, or appending the signature to the original file.

Question

Assuming I am asking the right questions to get one file appended to another, Is there a better sample of how exactly to merge two files into one using Go?

  • 写回答

1条回答 默认 最新

  • dongtan7418 2018-10-08 14:23
    关注

    Based on your question, you want to create a new file with the content of both files.

    You can use io.Copy to achieve that.

    Here is a simple command-line tool implementing it.

    package main
    
    import (
        "io"
        "log"
        "os"
    )
    
    func main() {
        if len(os.Args) != 4 {
            log.Fatalln("Usage: %s <zip> <signed> <output>
    ", os.Args[0])
        }
        zipName, signedName, output := os.Args[1], os.Args[2], os.Args[3]
    
        zipIn, err := os.Open(zipName)
        if err != nil {
            log.Fatalln("failed to open zip for reading:", err)
        }
        defer zipIn.Close()
    
        signedIn, err := os.Open(signedName)
        if err != nil {
            log.Fatalln("failed to open signed for reading:", err)
        }
        defer signedIn.Close()
    
        out, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY, 0644)
        if err != nil {
            log.Fatalln("failed to open outpout file:", err)
        }
        defer out.Close()
    
        n, err := io.Copy(out, zipIn)
        if err != nil {
            log.Fatalln("failed to append zip file to output:", err)
        }
        log.Printf("wrote %d bytes of %s to %s
    ", n, zipName, output)
    
        n, err = io.Copy(out, signedIn)
        if err != nil {
            log.Fatalln("failed to append signed file to output:", err)
        }
        log.Printf("wrote %d bytes of %s to %s
    ", n, signedName, output)
    }
    

    Basically, it open both files you want to merge, create a new one and copy the content of each file to the new file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题