package main
import (
"encoding/binary"
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
"time"
)
const (
TargetFeature = "_zys"
)
func getSrcTargetFileMap(path string) (res map[string]string) {
res = make(map[string]string)
dirs, err := ioutil.ReadDir(path)
if err != nil {
fmt.Println("read path : " + path + " error, please check input path")
}
for _, dir := range dirs {
fileName := dir.Name()
fileNameTrimSuffix := strings.Split(fileName, ".")[0]
if strings.Count(fileName, ".") != 1 {
continue
}
if strings.HasSuffix(fileName, "bin") && !strings.HasSuffix(fileNameTrimSuffix, TargetFeature) {
res[fileName] = fileNameTrimSuffix + TargetFeature + ".bin"
}
}
return
}
// func getBinSize(path string) (size int64, err error) {
// fileInfo, err := os.Stat(path)
// if err != nil {
// fmt.Println(err)
// }
// size = fileInfo.Size()
// return
// }
func convert(srcFile, tarFile, curPath string, tmpChan chan int) {
srcFilePath := curPath + "/" + srcFile
tarFilePath := curPath + "/" + tarFile
binContent, _ := ioutil.ReadFile(srcFilePath)
for i := 0; i < len(binContent); i += 2 {
tmp := binary.LittleEndian.Uint16(binContent[i : i+2])
binary.BigEndian.PutUint16(binContent[i:i+2], tmp)
}
err := ioutil.WriteFile(tarFilePath, binContent, os.ModePerm)
if err != nil {
fmt.Println(err)
}
tmpChan <- 1
}
func main() {
t1 := time.Now()
runtime.GOMAXPROCS(runtime.NumCPU())
curPath, err := os.Getwd()
if err != nil {
os.Exit(1)
}
files := getSrcTargetFileMap(curPath)
var fileNumChan = make(chan int)
for srcFile, tarFile := range files {
go convert(srcFile, tarFile, curPath, fileNumChan)
}
for i := 0; i < len(files); i++ {
<-fileNumChan
}
t2 := time.Since(t1)
fmt.Println(t2)
}
有疑问加站长微信联系(非本文作者)