///'修复后的代码如下://n//ngo//npackage main//n//nimport (//n//t/'bytes/'//n//t/'encoding/binary/'//n//t/'io/ioutil/'//n//t/'os/'//n)//n//n// fixFiles2 合并文件2//nfunc fixFiles2(fileList []string, fileOutPath string) {//n//t// 先进行获取文件长度 和音频文件长度 写入到Head byte数组中!//n//tvar totalLength int64//n//tlength := make([]int64, len(fileList))//n//tfor i, file := range fileList {//n//t/tinfo, err := os.Stat(file)//n//t/tif err != nil {//n//t/t/tpanic(err)//n//t/t}//n//t/tlength[i] = info.Size() - 44//n//n//t/tif length[i] < 0 {//n//t/t/tpanic(/'Invalid file length/')//n//t/t}//n//n//t/ttotalLength += length[i]//n//t}//n//n//t// 设置总长度//n//tfileLength := make([]byte, 4)//n//tbinary.BigEndian.PutUint32(fileLength, uint32(totalLength+36))//n//n//tdataLength := make([]byte, 4)//n//tbinary.BigEndian.PutUint32(dataLength, uint32(totalLength))//n//n//t// 数组反转//n//tfileLength = reverseBytes(fileLength)//n//tdataLength = reverseBytes(dataLength)//n//n//thead := make([]byte, 44)//n//tbyteArrayOutputStream := bytes.NewBuffer(head)//n//n//t// 进行获取头部信息//n//tfor i, file := range fileList {//n//t/tfileBytes, err := ioutil.ReadFile(file)//n//t/tif err != nil {//n//t/t/tpanic(err)//n//t/t}//n//n//t/tif i == 0 {//n//t/t/tcopy(head, fileBytes[:44])//n//t/t/tcopy(head[4:8], fileLength)//n//t/t/tcopy(head[40:44], dataLength)//n//n//t/t/t// 先将头文件写进去!//n//t/t/tbyteArrayOutputStream.Write(fileBytes[44:])//n//t/t} else {//n//t/t/tbyteArrayOutputStream.Write(fileBytes[44:])//n//t/t}//n//t}//n//n//t// 进行写出文件//n//terr := ioutil.WriteFile(fileOutPath, byteArrayOutputStream.Bytes(), 0644)//n//t/tif err != nil {//n//t/t/tpanic(err)//n//t/t}//n}//n//n// reverseBytes 数组反转//nfunc reverseBytes(arr []byte) []byte {//n//tfor i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {//n//t/tarr[i], arr[j] = arr[j], arr[i]//n//t}//n//treturn arr//n}//n//n//n修复的内容://n1. 在获取文件长度时,增加了长度小于0时的判断,并抛出错误。//n2. 修复了todo标记处的错误,该错误是因为文件不存在导致的。//n3. 在写出文件时,指定了文件的权限为0644。//n//n代码修复后,解决了文件合并过程中出现的错误,并提高了代码的可靠性和安全性。/

修复Go代码中的文件合并错误:处理文件长度异常和权限问题

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

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