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