Finish the mirror feature. Finish the prototype of zip compress.
This commit is contained in:
95
code/tool/util/util.zip.go
Normal file
95
code/tool/util/util.zip.go
Normal file
@ -0,0 +1,95 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"fmt"
|
||||
"github.com/eyebluecn/tank/code/tool/result"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Zip(srcPath string, destPath string) {
|
||||
|
||||
if PathExists(destPath) {
|
||||
panic(result.BadRequest("%s 已经存在了", destPath))
|
||||
}
|
||||
|
||||
// 创建准备写入的文件
|
||||
fileWriter, err := os.Create(destPath)
|
||||
PanicError(err)
|
||||
defer func() {
|
||||
err := fileWriter.Close()
|
||||
PanicError(err)
|
||||
}()
|
||||
|
||||
// 通过 fileWriter 来创建 zip.Write
|
||||
zipWriter := zip.NewWriter(fileWriter)
|
||||
defer func() {
|
||||
// 检测一下是否成功关闭
|
||||
if err := zipWriter.Close(); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}()
|
||||
|
||||
// 下面来将文件写入 zipWriter ,因为有可能会有很多个目录及文件,所以递归处理
|
||||
err = filepath.Walk(srcPath, func(path string, fileInfo os.FileInfo, errBack error) (err error) {
|
||||
if errBack != nil {
|
||||
return errBack
|
||||
}
|
||||
|
||||
fmt.Println("遍历文件: " + path)
|
||||
|
||||
// 通过文件信息,创建 zip 的文件信息
|
||||
fileHeader, err := zip.FileInfoHeader(fileInfo)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 替换文件信息中的文件名
|
||||
fileHeader.Name = strings.TrimPrefix(path, string(filepath.Separator))
|
||||
|
||||
// 目录加上/
|
||||
if fileInfo.IsDir() {
|
||||
fileHeader.Name += "/"
|
||||
}
|
||||
|
||||
fmt.Println("头部情况: " + fileHeader.Name)
|
||||
|
||||
// 写入文件信息,并返回一个 Write 结构
|
||||
writer, err := zipWriter.CreateHeader(fileHeader)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 检测,如果不是标准文件就只写入头信息,不写入文件数据到 writer
|
||||
// 如目录,也没有数据需要写
|
||||
if !fileHeader.Mode().IsRegular() {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 打开要压缩的文件
|
||||
fileToBeZip, err := os.Open(path)
|
||||
defer func() {
|
||||
err = fileToBeZip.Close()
|
||||
PanicError(err)
|
||||
}()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 将打开的文件 Copy 到 writer
|
||||
_, err = io.Copy(writer, fileToBeZip)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 输出压缩的内容
|
||||
//fmt.Printf("成功压缩文件: %s, 共写入了 %d 个字符的数据\n", path, n)
|
||||
|
||||
return nil
|
||||
})
|
||||
PanicError(err)
|
||||
}
|
46
code/tool/util/util_env.go
Normal file
46
code/tool/util/util_env.go
Normal file
@ -0,0 +1,46 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/user"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//是否为win开发环境
|
||||
func EnvWinDevelopment() bool {
|
||||
|
||||
ex, err := os.Executable()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
//如果exPath中包含了 \\AppData\\Local\\Temp 我们认为是在Win的开发环境中
|
||||
systemUser, err := user.Current()
|
||||
if systemUser != nil {
|
||||
|
||||
return strings.HasPrefix(ex, systemUser.HomeDir+"\\AppData\\Local\\Temp")
|
||||
|
||||
}
|
||||
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
//是否为mac开发环境
|
||||
func EnvMacDevelopment() bool {
|
||||
|
||||
ex, err := os.Executable()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return strings.HasPrefix(ex, "/private/var/folders")
|
||||
|
||||
}
|
||||
|
||||
//是否为开发环境 (即是否在IDE中运行)
|
||||
func EnvDevelopment() bool {
|
||||
|
||||
return EnvWinDevelopment() || EnvMacDevelopment()
|
||||
|
||||
}
|
@ -7,22 +7,23 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//判断文件或文件夹是否已经存在
|
||||
func PathExists(path string) (bool, error) {
|
||||
func PathExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
return true
|
||||
} else {
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
} else {
|
||||
panic(result.BadRequest(err.Error()))
|
||||
}
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
//获取GOPATH路径
|
||||
@ -58,21 +59,12 @@ func GetHomePath() string {
|
||||
}
|
||||
exPath := filepath.Dir(ex)
|
||||
|
||||
GetDevHomePath()
|
||||
|
||||
//如果exPath中包含了 /private/var/folders 我们认为是在Mac的开发环境中
|
||||
macDev := strings.HasPrefix(exPath, "/private/var/folders")
|
||||
if macDev {
|
||||
if EnvMacDevelopment() {
|
||||
exPath = GetDevHomePath() + "/tmp"
|
||||
}
|
||||
|
||||
//如果exPath中包含了 \\AppData\\Local\\Temp 我们认为是在Win的开发环境中
|
||||
systemUser, err := user.Current()
|
||||
if systemUser != nil {
|
||||
winDev := strings.HasPrefix(exPath, systemUser.HomeDir+"\\AppData\\Local\\Temp")
|
||||
if winDev {
|
||||
exPath = GetDevHomePath() + "/tmp"
|
||||
}
|
||||
if EnvWinDevelopment() {
|
||||
exPath = GetDevHomePath() + "/tmp"
|
||||
}
|
||||
|
||||
return exPath
|
||||
@ -84,12 +76,10 @@ func GetHtmlPath() string {
|
||||
|
||||
homePath := GetHomePath()
|
||||
filePath := homePath + "/html"
|
||||
exists, err := PathExists(filePath)
|
||||
if err != nil {
|
||||
panic("判断上传文件是否存在时出错!")
|
||||
}
|
||||
exists := PathExists(filePath)
|
||||
|
||||
if !exists {
|
||||
err = os.MkdirAll(filePath, 0777)
|
||||
err := os.MkdirAll(filePath, 0777)
|
||||
if err != nil {
|
||||
panic("创建上传文件夹时出错!")
|
||||
}
|
||||
@ -101,13 +91,11 @@ func GetHtmlPath() string {
|
||||
//如果文件夹存在就不管,不存在就创建。 例如:/var/www/matter
|
||||
func MakeDirAll(dirPath string) string {
|
||||
|
||||
exists, err := PathExists(dirPath)
|
||||
if err != nil {
|
||||
panic("判断文件是否存在时出错!")
|
||||
}
|
||||
exists := PathExists(dirPath)
|
||||
|
||||
if !exists {
|
||||
//TODO:文件权限需要进一步考虑
|
||||
err = os.MkdirAll(dirPath, 0777)
|
||||
err := os.MkdirAll(dirPath, 0777)
|
||||
if err != nil {
|
||||
panic("创建文件夹时出错!")
|
||||
}
|
||||
@ -178,16 +166,13 @@ func DeleteEmptyDirRecursive(dirPath string) {
|
||||
}
|
||||
}
|
||||
|
||||
//移除某个文件夹。 例如:/var/www/matter => /var/www
|
||||
//移除某个文件夹。
|
||||
func RemoveDirectory(dirPath string) string {
|
||||
|
||||
exists, err := PathExists(dirPath)
|
||||
if err != nil {
|
||||
panic("判断文件是否存在时出错!")
|
||||
}
|
||||
exists := PathExists(dirPath)
|
||||
if exists {
|
||||
|
||||
err = os.Remove(dirPath)
|
||||
err := os.Remove(dirPath)
|
||||
if err != nil {
|
||||
panic("删除文件夹时出错!")
|
||||
}
|
||||
@ -202,12 +187,10 @@ func GetConfPath() string {
|
||||
|
||||
homePath := GetHomePath()
|
||||
filePath := homePath + "/conf"
|
||||
exists, err := PathExists(filePath)
|
||||
if err != nil {
|
||||
panic("判断日志文件夹是否存在时出错!")
|
||||
}
|
||||
exists := PathExists(filePath)
|
||||
|
||||
if !exists {
|
||||
err = os.MkdirAll(filePath, 0777)
|
||||
err := os.MkdirAll(filePath, 0777)
|
||||
if err != nil {
|
||||
panic("创建日志文件夹时出错!")
|
||||
}
|
||||
@ -222,12 +205,10 @@ func GetLogPath() string {
|
||||
|
||||
homePath := GetHomePath()
|
||||
filePath := homePath + "/log"
|
||||
exists, err := PathExists(filePath)
|
||||
if err != nil {
|
||||
panic("判断日志文件夹是否存在时出错!")
|
||||
}
|
||||
exists := PathExists(filePath)
|
||||
|
||||
if !exists {
|
||||
err = os.MkdirAll(filePath, 0777)
|
||||
err := os.MkdirAll(filePath, 0777)
|
||||
if err != nil {
|
||||
panic("创建日志文件夹时出错!")
|
||||
}
|
||||
|
Reference in New Issue
Block a user