package dird
import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"time"
)
// CreateDateDir 根据当前日期来创建文件夹
func CreateDateDir(Path string) string {
folderName := time.Now().Format("20060102")
folderPath := filepath.Join(Path, folderName)
res, b := PathExists(folderPath)
if res {
for i := 1; true; i++ {
res, b := PathExists(folderPath + "_" + strconv.Itoa(i))
if !res {
if b != nil {
fmt.Println(b)
os.Exit(1)
} else {
folderPath = folderPath + "_" + strconv.Itoa(i)
break
}
}
}
} else {
if b != nil {
fmt.Println(b)
os.Exit(1)
}
}
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
// 必须分成两步:先创建文件夹、再修改权限
os.Mkdir(folderPath, 0777) //0777也可以os.ModePerm
os.Chmod(folderPath, 0777)
g := filepath.Join(folderPath, "更新内容")
b := filepath.Join(folderPath, "备份内容")
os.Mkdir(g, 0777) //0777也可以os.ModePerm
os.Chmod(g, 0777)
os.Mkdir(b, 0777) //0777也可以os.ModePerm
os.Chmod(b, 0777)
}
return folderPath
}
// PathExists 判断文件夹是否存在
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
//CopyFile fuzhi
func CopyFile(dstFileName string, srcFileName string) (written int64, err error) {
srcFile, err := os.Open(srcFileName)
if err != nil {
fmt.Printf("open file err = %v\n", err)
return
}
defer srcFile.Close()
//打开dstFileName
dstFile, err := os.OpenFile(dstFileName, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
fmt.Printf("open file err = %v\n", err)
return
}
defer dstFile.Close()
return io.Copy(dstFile, srcFile)
}
//Base64ToFile 232
func Base64ToFile(dd string, oo string) {
ddd, _ := base64.StdEncoding.DecodeString(dd) //成图片文件并把文件写入到buffer
ioutil.WriteFile(oo, ddd, 0666) //buffer输出到jpg文件中(不做处理,直接写到文件)
}
// Img2Base64 123
func Img2Base64() {
ff, _ := ioutil.ReadFile("D:\\study\\右键菜单\\mytools.ico") //我还是喜欢用这个快速读文件
bufstore := make([]byte, 13000) //数据缓存
base64.StdEncoding.Encode(bufstore, ff) // 文件转base64
_ = ioutil.WriteFile("./output2.jpg.txt", bufstore, 0666) //直接写入到文件就ok完活了。
}
// WriteToFile 123
func WriteToFile(files string, msg string) {
if err := ioutil.WriteFile(files, []byte(msg), 777); err != nil {
os.Exit(111)
}
}
网友评论