package main

import ( "bytes" "encoding/binary" "io/ioutil" "os" )

// fixFiles2 合并文件2 func fixFiles2(fileList []string, fileOutPath string) { // 先进行获取文件长度 和音频文件长度 写入到Head byte数组中! length := make([]int64, len(fileList)) var totalLength int64 for i, file := range fileList { info, err := os.Stat(file) if err != nil { panic(err) } length[i] = info.Size() - 44 // 每个文件长度-44 totalLength += length[i] }

// 设置总长度
fileLength := make([]byte, 4)
binary.BigEndian.PutUint32(fileLength, uint32(totalLength+36))

dataLength := make([]byte, 4)
binary.BigEndian.PutUint32(dataLength, uint32(totalLength))

// 数组反转
fileLength = reverse(fileLength)
dataLength = reverse(dataLength)

head := make([]byte, 44)
var byteArrayOutputStream bytes.Buffer
buffer := make([]byte, 1024)

// 进行获取头部信息
for i, file := range fileList {
	fileBytes, err := ioutil.ReadFile(file)
	if err != nil {
		panic(err)
	}

	if i == 0 {
		copy(head, fileBytes[:44])
		copy(head[4:8], fileLength)
		copy(head[40:44], dataLength)

		// 先将头文件写进去!
		byteArrayOutputStream.Write(head)
		byteArrayOutputStream.Write(fileBytes[44:])
	} else {
		byteArrayOutputStream.Write(fileBytes[44:])
	}
}

// 进行写出文件
err := ioutil.WriteFile(fileOutPath, byteArrayOutputStream.Bytes(), 0644)
if err != nil {
	panic(err)
}

}

// reverse 数组反转 func reverse(arr []byte) []byte { for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 { arr[i], arr[j] = arr[j], arr[i] } return arr

使用golang 重写下面代码 : 合并文件2 param fileList public static void fixFils2ListFile fileListString fileOutPath 先进行获取文件长度 和音频文件长度 写入到Head byte数组中! long length=new long4; long totalLength=0; for in

原文地址: https://www.cveoy.top/t/topic/h28H 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录