1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main

import (
	"fmt"
	"github.com/coreos/pkg/progressutil"
	"io"
	"os"
	"path/filepath"
	"time"
)

func combineMultipleFiles(outPutFile string, files ...string) {
	abs, err := filepath.Abs(outPutFile)
	if err != nil {
		panic("can not get outPutFile absolute path")
	}
	dir := filepath.Dir(abs)
	if _, err := os.Stat(dir); err != nil {
		if os.IsNotExist(err) {
			err := os.MkdirAll(dir, os.ModePerm)
			if err != nil {
				panic(fmt.Sprintf("can not mkdirs, path: %s, err: %s", dir, err))
			}
		}
	}
	file, err := os.Create(outPutFile)
	var fileSize int64
	for _, f := range files {
		time.Sleep(time.Second * 2)
		fileReader, err := os.Open(f)
		if err != nil {
			fmt.Printf("open file %s error, err: %s\n", f, err)
			continue
		}
		count, err := io.Copy(file, fileReader)
		if err != nil {
			panic("copy file error")
		}
		fileSize += count
	}
	size := progressutil.ByteUnitStr(fileSize)
	fmt.Printf("combine end, size %s\n", size)
}

func main() {
	combineMultipleFiles("out_put_file", "file1", "file2")
}