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 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误