初始化项目

This commit is contained in:
皮蛋13361098506
2025-01-06 16:01:02 +08:00
commit 1b77f62820
575 changed files with 69193 additions and 0 deletions

View File

@@ -0,0 +1,200 @@
package fileUtil
import (
"fmt"
"os"
"path/filepath"
"time"
"goutil/timeUtil"
)
// 大文件对象,可用于连续写入内容而不关闭文件,直到达到指定的大小
type BigFile struct {
// 文件夹名称
path string
// 当前文件名称
fileName string
// 文件名称前缀
fileNamePrefix string
// 当前文件大小单位Byte
fileSize int
// 最大的文件大小单位Byte
maxFileSize int
// 文件对象
file *os.File
// 获得新文件名称的方法
newFileNameFunc func(string, string) string
}
// 获取文件的完整路径
func (this *BigFile) getFullPath() string {
return filepath.Join(this.path, this.fileName)
}
// 初始化文件对象
func (this *BigFile) initFile() error {
// 初始化文件名称
this.fileName = this.newFileNameFunc(this.fileNamePrefix, this.fileName)
// 初始化文件大小
this.fileSize = 0
// 打开文件
file, err := os.OpenFile(this.getFullPath(), os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModePerm|os.ModeTemporary)
if err != nil {
return fmt.Errorf("打开文件%s错误错误信息为%s", this.getFullPath(), err)
} else {
this.file = file
}
return nil
}
// 返回当前文件名称
// 返回值:
// 当前文件名称
func (this *BigFile) FileName() string {
return this.fileName
}
// 保存消息
// message消息内容
// 返回值:错误对象
func (this *BigFile) SaveMessage(message string) error {
if this.file == nil {
return fmt.Errorf("文件对象为空path:%s", this.getFullPath())
}
// 增加文件大小
this.fileSize += len([]byte(message))
// 写入消息(在结尾处增加一个换行符\n
message = fmt.Sprintf("%s\n", message)
if _, err := this.file.WriteString(message); err != nil {
return fmt.Errorf("向文件%s写入信息错误错误信息为%s", this.getFullPath(), err)
}
// 如果达到了文件的上限,则关闭文件并重新打开一个新文件
if this.fileSize >= this.maxFileSize {
this.Close()
this.initFile()
}
return nil
}
// 写入字节消息
// message消息内容
// 返回值:错误对象
func (this *BigFile) WriteMessage(message []byte) error {
if this.file == nil {
return fmt.Errorf("文件对象为空path:%s", this.getFullPath())
}
// 增加文件大小
this.fileSize += len(message)
// 写入消息
if _, err := this.file.Write(message); err != nil {
return fmt.Errorf("向文件%s写入信息错误错误信息为%s", this.getFullPath(), err)
}
// 如果达到了文件的上限,则关闭文件并重新打开一个新文件
if this.fileSize >= this.maxFileSize {
this.Close()
this.initFile()
}
return nil
}
// 关闭对象
// 返回值:无
func (this *BigFile) Close() {
if this.file != nil {
this.file.Close()
this.file = nil
}
}
// 创建新的大文件对象(obsolete)
// _path:文件夹路径
// _maxFileSize:单个文件大小的最大值单位Byte
// 返回值:
// 大文件对象
// 错误对象
func NewBigFile(_path string, _maxFileSize int) (*BigFile, error) {
return NewBigFileWithNewFileNameFunc(_path, "default", _maxFileSize, newFileName)
}
// 创建新的大文件对象
// _path:文件夹路径
// _fileNamePrefix:文件名称前缀
// _maxFileSize:单个文件大小的最大值单位Byte
// 返回值:
// 大文件对象
// 错误对象
func NewBigFile2(_path, _fileNamePrefix string, _maxFileSize int) (*BigFile, error) {
return NewBigFileWithNewFileNameFunc(_path, _fileNamePrefix, _maxFileSize, newFileName)
}
// 创建新的大文件对象
// _path:文件夹路径
// _fileNamePrefix:文件名称前缀
// _maxFileSize:单个文件大小的最大值单位Byte
// _newFileNameFunc:创建新文件名称的方法
// 返回值:
// 大文件对象
// 错误对象
func NewBigFileWithNewFileNameFunc(_path, _fileNamePrefix string, _maxFileSize int, _newFileNameFunc func(string, string) string) (*BigFile, error) {
return NewBigFileWithNewFileNameFunc2(_path, _fileNamePrefix, "default", _maxFileSize, _newFileNameFunc)
}
// 创建新的大文件对象
// _path:文件夹路径
// _fileNamePrefix:文件名称前缀
// _fileName:文件名称
// _maxFileSize:单个文件大小的最大值单位Byte
// _newFileNameFunc:创建新文件名称的方法
// 返回值:
// 大文件对象
// 错误对象
func NewBigFileWithNewFileNameFunc2(_path, _fileNamePrefix, _fileName string, _maxFileSize int, _newFileNameFunc func(string, string) string) (*BigFile, error) {
// 判断文件夹是否存在,如果不存在则创建
if !IsDirExists(_path) {
os.MkdirAll(_path, os.ModePerm|os.ModeTemporary)
}
// 初始化对象
obj := &BigFile{
path: _path,
fileNamePrefix: _fileNamePrefix,
fileName: _fileName,
maxFileSize: _maxFileSize,
newFileNameFunc: _newFileNameFunc,
}
// 初始化文件对象
if err := obj.initFile(); err != nil {
obj.Close()
return nil, err
}
return obj, nil
}
// 创建新的文件名称
// prefix:前缀
// currFileName:当前文件名称
// 返回值:
// 新的文件名称
func newFileName(prefix, currFileName string) string {
return fmt.Sprintf("%s_%s.data", prefix, timeUtil.Format(time.Now(), "yyyyMMddHHmmss"))
}

View File

@@ -0,0 +1,37 @@
package fileUtil
import (
"fmt"
"testing"
)
func BenchmarkSaveMessage(b *testing.B) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
bigFileObj, err := NewBigFile(path, 1024*1024*1024)
if err != nil {
b.Errorf("there should no err, but not there is:%s", err)
}
for i := 0; i < b.N; i++ {
bigFileObj.SaveMessage(fmt.Sprintf("line %d", i))
}
}
func TestSaveMessage(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
bigFileObj, err := NewBigFile(path, 1024)
if err != nil {
t.Errorf("there should no err, but not there is:%s", err)
}
for i := 0; i < 100000; i++ {
bigFileObj.SaveMessage(fmt.Sprintf("line %d", i))
}
fileList, err := GetFileList(path)
for _, item := range fileList {
fmt.Printf("file:%s\n", item)
}
}

View File

@@ -0,0 +1,4 @@
/*
文件助手类
*/
package fileUtil

View File

@@ -0,0 +1,298 @@
package fileUtil
import (
"bufio"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
)
var (
mutex sync.Mutex
)
// 文件是否存在
// 文件路径
// 返回值:
// 是否存在
// 错误对象
func IsFileExists(path string) (bool, error) {
file, err := os.Stat(path)
if err == nil {
return file.IsDir() == false, nil
} else {
if os.IsNotExist(err) {
return false, nil
}
}
return true, err
}
// 文件夹是否存在
// 文件夹路径
// 返回值:
// 是否存在
// 错误对象
func IsDirectoryExists(path string) (bool, error) {
file, err := os.Stat(path)
if err == nil {
return file.IsDir(), nil
} else {
if os.IsNotExist(err) {
return false, nil
}
}
return true, err
}
// 文件夹是否存在(obsolete)
// 文件夹路径
// 返回值:
// 是否存在
func IsDirExists(path string) bool {
file, err := os.Stat(path)
if err != nil {
return false
} else {
return file.IsDir()
}
}
// 获取当前路径
// 返回值:
// 当前路径
func GetCurrentPath() string {
file, _ := exec.LookPath(os.Args[0])
fileAbsPath, _ := filepath.Abs(file)
return filepath.Dir(fileAbsPath)
}
// 获取目标文件列表(完整路径)
// path文件夹路径
// 返回值:文件列表(完整路径)
func GetFileList(path string) (fileList []string, err error) {
if exists, err1 := IsDirectoryExists(path); err1 != nil {
err = err1
return
} else if !exists {
return
}
// 遍历目录,获取所有文件列表
err = filepath.Walk(path, func(fileName string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
// 忽略目录
if fi.IsDir() {
return nil
}
// 添加到列表
fileList = append(fileList, fileName)
return nil
})
return
}
// 获取目标文件列表(完整路径)
// path文件夹路径
// prefix文件前缀
// suffix文件后缀
// 返回值:文件列表(完整路径)
func GetFileList2(path, prefix, suffix string) (fileList []string, err error) {
if exists, err1 := IsDirectoryExists(path); err1 != nil {
err = err1
return
} else if !exists {
return
}
// 遍历目录,获取所有文件列表
err = filepath.Walk(path, func(fileName string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
// 忽略目录
if fi.IsDir() {
return nil
}
// 添加到列表
baseName := filepath.Base(fileName)
if prefix != "" && strings.HasPrefix(baseName, prefix) == false {
return nil
}
if suffix != "" && strings.HasSuffix(baseName, suffix) == false {
return nil
}
fileList = append(fileList, fileName)
return nil
})
return
}
// 按行读取每一个文件的内容
// fileName:文件的绝对路径
// 返回值:
// 行内容列表
// 错误信息
func ReadFileLineByLine(fileName string) (lineList []string, err error) {
// 打开文件
file, err1 := os.Open(fileName)
if err1 != nil {
err = err1
return
}
defer file.Close()
// 读取文件
buf := bufio.NewReader(file)
for {
// 按行读取
line, _, err2 := buf.ReadLine()
if err2 == io.EOF {
break
}
//将byte[]转换为string并添加到列表中
lineList = append(lineList, string(line))
}
return
}
// 读取文件内容(字符串)
// fileName文件的绝对路径
// 返回值:
// 文件内容
// 错误信息
func ReadFileContent(fileName string) (content string, err error) {
bytes, err1 := ioutil.ReadFile(fileName)
if err1 != nil {
err = err1
return
}
content = string(bytes)
return
}
// 读取文件内容(字符数组)
// fileName文件的绝对路径
// 返回值:
// 文件内容
// 错误信息
func ReadFileBytes(fileName string) (content []byte, err error) {
content, err = ioutil.ReadFile(fileName)
return
}
// 写入文件
// filePath文件夹路径
// fileName文件名称
// ifAppend是否追加内容
// args可变参数
// 返回值:
// error:错误信息
func WriteFile(filePath, fileName string, ifAppend bool, args ...string) error {
// 得到最终的fileName
fileName = filepath.Join(filePath, fileName)
// 判断文件夹是否存在,如果不存在则创建
mutex.Lock()
if !IsDirExists(filePath) {
os.MkdirAll(filePath, os.ModePerm|os.ModeTemporary)
}
mutex.Unlock()
// 打开文件(如果文件存在就以写模式打开,并追加写入;如果文件不存在就创建,然后以写模式打开。)
var f *os.File
var err error
if ifAppend == false {
f, err = os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm|os.ModeTemporary)
} else {
f, err = os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm|os.ModeTemporary)
}
if err != nil {
return err
}
defer f.Close()
// 写入内容
for _, arg := range args {
_, err = f.WriteString(arg)
if err != nil {
return err
}
}
return nil
}
// 写入文件
// filePath文件夹路径
// fileName文件名称
// ifAppend是否追加内容
// args可变参数
// 返回值:
// error:错误信息
func WriteFile4Byte(filePath, fileName string, ifAppend bool, args ...[]byte) error {
// 得到最终的fileName
fileName = filepath.Join(filePath, fileName)
// 判断文件夹是否存在,如果不存在则创建
mutex.Lock()
if !IsDirExists(filePath) {
os.MkdirAll(filePath, os.ModePerm|os.ModeTemporary)
}
mutex.Unlock()
// 打开文件(如果文件存在就以写模式打开,并追加写入;如果文件不存在就创建,然后以写模式打开。)
var f *os.File
var err error
if ifAppend == false {
f, err = os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm|os.ModeTemporary)
} else {
f, err = os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm|os.ModeTemporary)
}
if err != nil {
return err
}
defer f.Close()
// 写入内容
for _, arg := range args {
_, err = f.Write(arg)
if err != nil {
return err
}
}
return nil
}
// 删除文件
// fileName文件的绝对路径
// 返回值:
// 错误对象
func DeleteFile(fileName string) error {
return os.Remove(fileName)
}

View File

@@ -0,0 +1,345 @@
package fileUtil
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"testing"
"time"
)
func BenchmarkWriteFile(b *testing.B) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
for i := 0; i < b.N; i++ {
WriteFile(path, "test.txt", true, fmt.Sprintf("line %d", i))
}
}
func TestIsFileExists(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
fileName := fmt.Sprintf("%s/%s", path, "test.txt")
fmt.Printf("FileName:%s\n", fileName)
if exists, err := IsFileExists(fileName); err != nil || exists {
t.Errorf("the file %s should not be exists, but now it's exists", fileName)
}
if err := WriteFile(path, "test.txt", true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if exists, err := IsFileExists(fileName); err != nil || !exists {
t.Errorf("the file %s should be exists, but now it's not exists", fileName)
}
if content, err := ReadFileContent(fileName); err != nil {
t.Errorf("there should be no error, but now err:%s", err)
} else {
fmt.Printf("Content:%s\n", content)
}
DeleteFile(fileName)
}
func TestIsDirectoryExists(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
filePath := filepath.Join(path, "Parent")
if exists, err := IsDirectoryExists(filePath); err != nil || exists {
t.Errorf("the file %s should not be exists, but now it's exists", filePath)
}
fileName := fmt.Sprintf("%s/%s", filePath, "test.txt")
if err := WriteFile(filePath, "test.txt", true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if exists, err := IsDirectoryExists(filePath); err != nil || !exists {
t.Errorf("the file %s should be exists, but now it's not exists", filePath)
}
if content, err := ReadFileContent(fileName); err != nil {
t.Errorf("there should be no error, but now err:%s", err)
} else {
fmt.Printf("Content:%s\n", content)
}
DeleteFile(fileName)
}
func TestIsDirExists(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
filePath := filepath.Join(path, "Parent2")
if IsDirExists(filePath) {
t.Errorf("the file %s should not be exists, but now it's exists", filePath)
}
fileName := fmt.Sprintf("%s/%s", filePath, "test.txt")
if err := WriteFile(filePath, "test.txt", true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if IsDirExists(filePath) == false {
t.Errorf("the file %s should be exists, but now it's not exists", filePath)
}
if content, err := ReadFileContent(fmt.Sprintf("%s/%s", filePath, "test.txt")); err != nil {
t.Errorf("there should be no error, but now err:%s", err)
} else {
fmt.Printf("Content:%s\n", content)
}
DeleteFile(fileName)
}
func TestGetFileList(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
fileName1 := "2017-09-12-12.txt"
fileName2 := "2017-09-12-13.txt"
fileName3 := "2017-09-12-14.txt"
fileName4 := "2017-09-12.tar.bz2"
seperator := "\\"
if runtime.GOOS != "windows" {
seperator = "/"
}
filePath1 := fmt.Sprintf("%s%s%s", path, seperator, fileName1)
filePath2 := fmt.Sprintf("%s%s%s", path, seperator, fileName2)
filePath3 := fmt.Sprintf("%s%s%s", path, seperator, fileName3)
filePath4 := fmt.Sprintf("%s%s%s", path, seperator, fileName4)
if err := WriteFile(path, fileName1, true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if err := WriteFile(path, fileName2, true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if err := WriteFile(path, fileName3, true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if err := WriteFile(path, fileName4, true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
fileList, err := GetFileList(path)
if err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if fileList[0] != filePath1 {
t.Errorf("Expected:%s, now got:%s", filePath1, fileList[0])
}
if fileList[1] != filePath2 {
t.Errorf("Expected:%s, now got:%s", filePath2, fileList[1])
}
if fileList[2] != filePath3 {
t.Errorf("Expected:%s, now got:%s", filePath3, fileList[2])
}
if fileList[3] != filePath4 {
t.Errorf("Expected:%s, now got:%s", filePath4, fileList[3])
}
DeleteFile(filePath1)
DeleteFile(filePath2)
DeleteFile(filePath3)
DeleteFile(filePath4)
}
func TestGetFileList2(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
fileName1 := "2017-09-12-12.txt"
fileName2 := "2017-09-12-13.txt"
fileName3 := "2017-09-12-14.txt"
fileName4 := "2017-09-12.tar.bz2"
seperator := "\\"
if runtime.GOOS != "windows" {
seperator = "/"
}
filePath1 := fmt.Sprintf("%s%s%s", path, seperator, fileName1)
filePath2 := fmt.Sprintf("%s%s%s", path, seperator, fileName2)
filePath3 := fmt.Sprintf("%s%s%s", path, seperator, fileName3)
filePath4 := fmt.Sprintf("%s%s%s", path, seperator, fileName4)
if err := WriteFile(path, fileName1, true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if err := WriteFile(path, fileName2, true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if err := WriteFile(path, fileName3, true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if err := WriteFile(path, fileName4, true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
fileList, err := GetFileList2(path, "2017-09-12", "txt")
if err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
fmt.Printf("fileList:%v\n", fileList)
if fileList[0] != filePath1 {
t.Errorf("Expected:%s, now got:%s", filePath1, fileList[0])
}
if fileList[1] != filePath2 {
t.Errorf("Expected:%s, now got:%s", filePath2, fileList[1])
}
if fileList[2] != filePath3 {
t.Errorf("Expected:%s, now got:%s", filePath3, fileList[2])
}
fileList2, err := GetFileList2(path, "2017-09-12", "tar.bz2")
if err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
fmt.Printf("fileList2:%v\n", fileList2)
if fileList2[0] != filePath4 {
t.Errorf("Expected:%s, now got:%s", filePath4, fileList2[0])
}
DeleteFile(filePath1)
DeleteFile(filePath2)
DeleteFile(filePath3)
DeleteFile(filePath4)
}
func TestReadFileLineByLine(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
fileName := fmt.Sprintf("%s/%s", path, "test.txt")
if err := WriteFile(path, "test.txt", true, "first line\n"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if err := WriteFile(path, "test.txt", true, "second line\n"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
expectedFirstLine := "first line"
expectedSecondLine := "second line"
lineList, err := ReadFileLineByLine(fileName)
if err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if lineList[0] != expectedFirstLine {
t.Errorf("Expected:%s, but now got:%s", expectedFirstLine, lineList[0])
}
if lineList[1] != expectedSecondLine {
t.Errorf("Expected:%s, but now got:%s", expectedSecondLine, lineList[1])
}
if err := DeleteFile(fileName); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
}
}
func TestReadFileContent(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
fileName := fmt.Sprintf("%s/%s", path, "test.txt")
if err := WriteFile(path, "test.txt", true, "first line\n"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if err := WriteFile(path, "test.txt", true, "second line\n"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
expectedContent := "first line\nsecond line\n"
if content, err := ReadFileContent(fileName); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
} else if content != expectedContent {
t.Errorf("Expected:%s, but now got:%s", expectedContent, content)
}
if err := DeleteFile(fileName); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
}
}
func TestDeleteFile(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
fileName := fmt.Sprintf("%s/%s", path, "test.txt")
if err := WriteFile(path, "test.txt", true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if err := DeleteFile(fileName); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
}
}
func TestReadWriteSimultaneously(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
fileName := fmt.Sprintf("%s/%s", path, "test.txt")
file1, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm|os.ModeTemporary)
if err != nil {
t.Errorf("1:there should be no err, but now err:%s", err)
}
// for i := 0; i < 10; i++ {
// file1.WriteString(fmt.Sprintf("line %d\n", i))
// }
go func() {
for i := 0; i < 10; i++ {
file1.WriteString(fmt.Sprintf("line %d\n", i))
time.Sleep(time.Second)
}
}()
file2, err := os.OpenFile(fileName, os.O_RDONLY, os.ModePerm|os.ModeTemporary)
if err != nil {
t.Errorf("2:there should be no err, but now err:%s", err)
}
go func() {
offset := 0
// 读取文件
buf := bufio.NewReader(file2)
for {
// 按行读取
line, _, err2 := buf.ReadLine()
if err2 == io.EOF {
time.Sleep(500 * time.Millisecond)
continue
}
if len(line) == 0 {
continue
}
//将byte[]转换为string并添加到列表中
fmt.Printf("line %d:%s\n", offset, string(line))
offset += 1
if offset >= 10 {
break
}
}
}()
time.Sleep(30 * time.Second)
fmt.Println("end")
}

View File

@@ -0,0 +1,78 @@
package fileUtil
import (
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
)
// 对文件进行gzip压缩
// source:源文件完整路径
// target:目标文件文件夹(如果传空字符串,则为当前文件夹)
// 返回值
// 错误对象
func Gzip(source, target string) error {
reader, err := os.Open(source)
if err != nil {
return err
}
defer reader.Close()
// 给目标文件夹赋值,如果传空,则默认为当前文件夹
if target == "" {
target = filepath.Dir(source)
}
fileName := filepath.Base(source)
targetFilePath := filepath.Join(target, fmt.Sprintf("%s.gz", fileName))
writer, err := os.Create(targetFilePath)
if err != nil {
return err
}
defer writer.Close()
archiver := gzip.NewWriter(writer)
archiver.Name = fileName
defer archiver.Close()
_, err = io.Copy(archiver, reader)
return err
}
// 对文件进行gzip解压缩
// source:源文件完整路径
// target:目标文件文件夹(解压缩文件的名字是内部自动赋值)
// 返回值
// 错误对象
func UnGzip(source, target string) error {
reader, err := os.Open(source)
if err != nil {
return err
}
defer reader.Close()
archive, err := gzip.NewReader(reader)
if err != nil {
return err
}
defer archive.Close()
// 给目标文件夹赋值,如果传空,则默认为当前文件夹
if target == "" {
target = filepath.Dir(source)
}
targetFilePath := filepath.Join(target, archive.Name)
writer, err := os.Create(targetFilePath)
if err != nil {
return err
}
defer writer.Close()
_, err = io.Copy(writer, archive)
return err
}

View File

@@ -0,0 +1,54 @@
package fileUtil
import (
"fmt"
"testing"
)
func TestGzip(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
fileName := fmt.Sprintf("%s/%s", path, "test.txt")
if err := WriteFile(path, "test.txt", true, "first line\nHello world"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if err := Gzip(fileName, ""); err != nil {
// if err := Gzip(fileName, path); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
}
if fileList, err := GetFileList(path); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
} else {
for _, item := range fileList {
fmt.Printf("item:%s\n", item)
}
}
DeleteFile(fileName)
}
func TestUnGzip(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
fileName := fmt.Sprintf("%s/%s", path, "test.txt.gz")
if err := UnGzip(fileName, ""); err != nil {
// if err := UnGzip(fileName, path); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
}
content, err := ReadFileContent(fmt.Sprintf("%s/%s", path, "test.txt"))
if err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
} else {
fmt.Printf("content:%s\n", content)
}
DeleteFile(fileName)
fileName = fmt.Sprintf("%s/%s", path, "test.txt")
DeleteFile(fileName)
}

View File

@@ -0,0 +1,55 @@
package fileUtil
import (
"io"
"net/http"
"os"
"path"
)
// 下载网络文件
// netUrl网络文件地址
// saveDir存储位置
// saveFileName:存储的文件名
// ifTruncate:如果文件存在了,是否覆盖此文件
// 返回值:
// err:错误对象
func DownLoadNetFile(netUrl string, saveDir string, saveFileName string, ifTruncate bool) (err error) {
resp, err := http.Get(netUrl)
defer func() {
if resp != nil {
resp.Body.Close()
}
}()
if err != nil {
return
}
// 创建文件夹
if IsDirExists(saveDir) == false {
os.MkdirAll(saveDir, os.ModePerm|os.ModeTemporary)
}
// 创建文件
filePath := path.Join(saveDir, saveFileName)
var fileObj *os.File
if ifTruncate {
fileObj, err = os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm|os.ModeTemporary)
} else {
// 如果文件已经存在,则不能打开
fileObj, err = os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, os.ModePerm|os.ModeTemporary)
}
defer func() {
if fileObj != nil {
fileObj.Close()
}
}()
if err != nil {
return
}
// 写入文件数据
_, err = io.Copy(fileObj, resp.Body)
return
}

View File

@@ -0,0 +1,13 @@
package fileUtil
import "testing"
func TestDownLoadNetFile(t *testing.T) {
err := DownLoadNetFile("https://www.baidu.com/img/bd_logo1.png", "./", "baidu.png", false)
if err != nil {
t.Error(err)
return
}
t.Log("成功了")
}

View File

@@ -0,0 +1,104 @@
package fileUtil
import (
"archive/tar"
"io"
"os"
"path/filepath"
)
// 对一组文件进行tar打包
// sourceList:源文件完整路径列表
// target:目标文件名称
// 返回值
// 错误对象
func Tar(sourceList []string, target string) error {
tarFile, err := os.Create(target)
if err != nil {
return err
}
defer tarFile.Close()
tarball := tar.NewWriter(tarFile)
defer tarball.Close()
// 对源文件遍历处理
for _, item := range sourceList {
info, err := os.Stat(item)
if err != nil || info.IsDir() {
continue
}
header, err := tar.FileInfoHeader(info, info.Name())
if err != nil {
return err
}
header.Name = filepath.Base(item)
if err := tarball.WriteHeader(header); err != nil {
return err
}
file, err := os.Open(item)
if err != nil {
return err
}
defer file.Close()
if _, err = io.Copy(tarball, file); err != nil {
return err
}
}
return nil
}
// 对一组文件进行tar解包
// source:源文件完整路径
// target:目标文件名称
// 返回值
// 错误对象
func Untar(source, target string) error {
reader, err := os.Open(source)
if err != nil {
return err
}
defer reader.Close()
tarReader := tar.NewReader(reader)
// 给目标文件夹赋值,如果传空,则默认为当前文件夹
if target == "" {
target = filepath.Dir(source)
}
for {
header, err := tarReader.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}
targetFilePath := filepath.Join(target, header.Name)
info := header.FileInfo()
if info.IsDir() {
if err = os.MkdirAll(targetFilePath, info.Mode()); err != nil {
return err
}
continue
}
file, err := os.OpenFile(targetFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, tarReader)
if err != nil {
return err
}
}
return nil
}

View File

@@ -0,0 +1,73 @@
package fileUtil
import (
"fmt"
"strings"
"testing"
)
func TestTar(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
fileName1 := fmt.Sprintf("%s/%s", path, "test1.txt")
fileName2 := fmt.Sprintf("%s/%s", path, "test2.txt")
if err := WriteFile(path, "test1.txt", true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if err := WriteFile(path, "test2.txt", true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
sourceList := make([]string, 0, 2)
sourceList = append(sourceList, fileName1)
sourceList = append(sourceList, fileName2)
target := fmt.Sprintf("%s/%s", path, "test.tar")
if err := Tar(sourceList, target); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
}
if fileList, err := GetFileList(path); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
} else {
for _, item := range fileList {
fmt.Printf("item:%s\n", item)
}
}
DeleteFile(fileName1)
DeleteFile(fileName2)
}
func TestUntar(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
source := fmt.Sprintf("%s/%s", path, "test.tar")
// target := path
target := ""
if err := Untar(source, target); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
}
if fileList, err := GetFileList(path); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
} else {
for _, item := range fileList {
fmt.Printf("item:%s\n", item)
if strings.HasSuffix(item, "txt") {
if content, err := ReadFileContent(item); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
} else {
fmt.Printf("content:%s\n", content)
}
DeleteFile(item)
}
}
DeleteFile(source)
}
}