文件处理压缩删除原文件
This commit is contained in:
@@ -7,6 +7,9 @@ import (
|
||||
"io"
|
||||
"omc/handle/model"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
//BJ/HX/RJ/OMC/FM/告警文件生成时间
|
||||
@@ -14,25 +17,25 @@ import (
|
||||
//<省份简称>-<数据类别>-<网元类型>[-网元子类]-<主机编号>-<数据版本>-<数据时间>[-登录用户名][-同步请求标识][-Ri][-统计周期] [-序列号].<后缀>
|
||||
// BJ-FM-OMC-主机编码-v0-告警文件生成时间-001.txt
|
||||
|
||||
type FileMeta struct {
|
||||
DirRoot string `json:"dir_root"`
|
||||
type FileNameMeta struct {
|
||||
FTPRoot string `json:"ftp_root"` // FTP根目录
|
||||
Province string `json:"province"` // 网元所在省份
|
||||
DeviceCode string `json:"device_code"` // 主机编码 四位,每1位可用0-9、A-Z编码
|
||||
Index string `json:"index"` //文件标识
|
||||
Compress bool `json:"compress"` //文件是否压缩
|
||||
ReqId string `json:"req_id"`
|
||||
ReqId string `json:"req_id"` // 同步请求标识
|
||||
Suffix string `json:"suffix"` // 文件后缀
|
||||
}
|
||||
|
||||
// func FileName(meta *FileMeta) string {
|
||||
// reqIdStr := fmt.Sprintf("%03s", meta.ReqId)
|
||||
// timeStr := time.Now().Format()
|
||||
// fileName := strings.Join([]string{meta.Province, "FM", "OMC", meta.DeviceCode, "v0", meta.Time, meta.Index}, "-", reqIdStr)
|
||||
// return strings.ToUpper(fileName)
|
||||
// }
|
||||
|
||||
func CreateFile(fileName string, data []model.OmcAlarm) error {
|
||||
filePath := fileName
|
||||
// 生成告警文件文件名
|
||||
// GD-FM-OMC-100A-V1.1.0-20150611011603-001.txt.zip
|
||||
func FileName(meta *FileNameMeta) string {
|
||||
reqIdStr := fmt.Sprintf("%03s", meta.ReqId)
|
||||
timeStr := time.Now().Format("20060102150405")
|
||||
fileNameGroup := []string{meta.Province, "FM", "OMC", meta.DeviceCode, "V0", timeStr, reqIdStr}
|
||||
fileName := strings.Join(fileNameGroup, "-")
|
||||
return path.Join(meta.FTPRoot, strings.ToUpper(fileName)+meta.Suffix)
|
||||
}
|
||||
|
||||
func CreateFile(filePath string, data []model.OmcAlarm) error {
|
||||
// 创建或打开文件
|
||||
file, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
@@ -104,3 +107,21 @@ func FileToZip(zipFileName, filePath string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 文件同级进行zip压缩
|
||||
func FileToCompress(filePath string, data []model.OmcAlarm, delOrgFile bool) (string, error) {
|
||||
defer os.Remove(filePath)
|
||||
// 导出文件
|
||||
err := CreateFile(filePath, data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 压缩文件
|
||||
zipFfileNmae := filePath + ".zip"
|
||||
err = FileToZip(zipFfileNmae, filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return zipFfileNmae, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user