You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package util
import (
"fmt"
"io/ioutil"
"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 {
fmt.Println("read file failed, err:", err)
return ""
}
return string(content)
}
func GenFile(str, path string) {
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
fmt.Println("open file failed, err:", err)
return
}
defer file.Close()
file.Write([]byte(str)) //写入字节切片数据
//file.WriteString("hello go") //直接写入字符串数据
}
func TestGenFile(path, enPath, dePath string) {
strContent := ReadFileToStr(path)
Encrypt, _ := AesEcpt.AesBase64Encrypt(strContent)
GenFile(Encrypt, enPath)
//fmt.Printf("Encrypt: %v\n", Encrypt)
Decrypt, _ := AesEcpt.AesBase64Decrypt(Encrypt)
GenFile(Decrypt, dePath)
//fmt.Printf("Decrypt: %v\n", Decrypt)
fmt.Printf("明文长度: %v\n解密后长度: %v\n", len(strContent), len(Decrypt))
}
func EnFile(path string) {
EnFileToOutPath(path, path)
}
func EnFileToOutPath(path, outPath string) {
strContent := ReadFileToStr(path)
Encrypt, _ := AesEcpt.AesBase64Encrypt(strContent)
GenFile(Encrypt, outPath)
}
func DeFile(path string) {
DeFileToOutPath(path, path)
}
func DeFileToOutPath(path string, outPath string) {
strContent := ReadFileToStr(path)
Decrypt, _ := AesEcpt.AesBase64Decrypt(strContent)
GenFile(Decrypt, outPath)
}