From 542ccfcb993f823dd2218fe3aaea10cf85ba994f Mon Sep 17 00:00:00 2001 From: 15128022404 <1421485150@qq.com> Date: Fri, 14 Oct 2022 16:50:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 70 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index c6ba715..7b1e6d5 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,22 @@ import ( "os" ) +//PathExists 判断一个文件或文件夹是否存在 +//输入文件路径,根据返回的bool值来判断文件或文件夹是否存在 +func PathExists(path string) (bool, string) { + if len(path) == 0 { + return false, "path 不能为空" + } + _, err := os.Stat(path) + if err == nil { + return true, "存在" + } + if os.IsNotExist(err) { + return false, "path路径文件不存在" + } + return false, "path路径文件不存在" +} + func readFileToStr(path string) string { content, err := ioutil.ReadFile(path) if err != nil { @@ -73,30 +89,38 @@ var key = flag.String("key", "", "加密秘钥-限制大小写字母数字16位 func main() { flag.Parse() - //fmt.Println(*mode) - //fmt.Println(*path) - var out string = *path - var modeName string - if len(*outPath) != 0 { - out = *outPath - } - if len(*key) == 16 { - util.InitBykey(*key) - } else if (len(*key) > 0) && (len(*key) < 16) { - fmt.Printf("秘钥限制大小写字母数字16位") + //验证操作文件路径是否有效 + flag, message := PathExists(*path) + if !flag { + fmt.Printf(message) + } else { + // 输出路径设置 + var out string = *path + var modeName string + if len(*outPath) != 0 { + out = *outPath + } + //验证秘钥是否有效 + if len(*key) == 16 { + util.InitBykey(*key) + } else if (len(*key) > 0) && (len(*key) < 16) { + fmt.Printf("秘钥限制大小写字母数字16位") + } + //验证mode 输入是否正确 + if *mode != "en" && *mode != "de" { + fmt.Printf("mode 只能是 en 或者 de") + //执行加密 + } else if *mode == "en" { + modeName = "加密" + enFileToOutPath(*path, out) + //执行解密 + } else if *mode == "de" { + modeName = "解密" + deFileToOutPath(*path, out) + } + fmt.Println(modeName + "成功,被" + modeName + "文件是在: " + *path + " " + modeName + "后文件生产在:" + out) } - if len(*path) == 0 { - fmt.Printf("path 不能为空") - } else if *mode != "en" && *mode != "de" { - fmt.Printf("mode 只能是 en 或者 de") - } else if *mode == "en" { - modeName = "加密" - enFileToOutPath(*path, out) - } else if *mode == "de" { - modeName = "解密" - deFileToOutPath(*path, out) - } - fmt.Println(modeName + "成功,被" + modeName + "文件是在: " + *path + " " + modeName + "后文件生产在:" + out) + //test() // testGenFile("C:\\METSIM\\example\\gascleaning_1.5_30.sfw", // "C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw.EN",