zdpgo_file

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2022 License: MIT Imports: 11 Imported by: 1

README

zdpgo_file

Golang操作文件或者文件夹的快捷组件

项目地址:https://github.com/zhangdapeng520/zdpgo_file

版本历史

  • v0.1.0 2022/2/7
  • v0.1.1 2022/4/3
  • v0.1.2 2022/4/21
  • v0.1.3 2022/5/2 新增:替换指定目录下的文件名
  • v0.1.4 2022/5/12 新增:指定目录创建和删除文件

使用示例

读写csv
package main

import (
	"fmt"
	"github.com/zhangdapeng520/zdpgo_file"
)

func main() {
	f := zdpgo_file.New()
	data := [][]string{
		{"a[test]", "b", "c[test]"},
		{"111", "222", "333"},
		{"111", "222", "333"},
		{"111", "222", "333"},
		{"111", "222", "333"},
		{"111", "222", "333"},
		{"111", "222", "333"},
		{"111", "222", "333"},
		{"111", "222", "333"},
		{"111", "222", "333"},
		{"111", "222", "333"},
	}

	// 写入
	f.Csv.Write("test.csv", data)

	// 读取
	dataNew, err := f.Csv.Read("test.csv")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(dataNew)
}
文件夹操作
package main

import (
	"fmt"
	"github.com/zhangdapeng520/zdpgo_file"
)

func main() {
	f := zdpgo_file.New()
	fmt.Println(f.Directory.GetDirectorySize("../download"))
	fmt.Println(f.Directory.IsExist("D:\\BaiduNetdiskWorkspace\\文档"))
	fmt.Println(f.Directory.CreateMultiDir("logs/zdpgo"))
}
下载网络文件
package main

import (
	"fmt"
	"github.com/zhangdapeng520/zdpgo_file"
)

func main() {
	f := zdpgo_file.New()
	savePath := "./"

	// 单个下载
	url := "https://avatar.csdnimg.cn/2/9/0/1_togolife.jpg"
	err := f.Download.Download(savePath, url)
	if err != nil {
		fmt.Println("下载失败:", err)
	} else {
		fmt.Println("下载成功")
	}

	// 批量下载
	urls := []string{
		"https://alifei04.cfp.cn/creative/vcg/nowarter800/new/VCG41N695593548.jpg",
		"https://tenfei02.cfp.cn/creative/vcg/nowarter800/new/VCG41N1014325904.jpg",
		"https://tenfei05.cfp.cn/creative/vcg/nowater800/new/VCG41545444880.jpg",
	}
	f.Download.Downloads(savePath, urls)
}
文件操作
package main

import (
	"fmt"
	"github.com/zhangdapeng520/zdpgo_file"
)

func main() {
	f := zdpgo_file.New()
	fmt.Println(f.File.Exists("./file.go"))
	fmt.Println(f.File.Exists("./file111.go"))

	fmt.Println(f.File.Size("./file.go"))
	fmt.Println(f.File.Size("./file1.go"))
}
去掉文件后缀
package main

import (
	"github.com/zhangdapeng520/zdpgo_file"
)

func main() {
	f := zdpgo_file.New()
	err := f.File.RemoveDirFilesSuffix("test")
	if err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v0.1.4

type Config struct {
	Debug       bool   `yaml:"debug" json:"debug"`                 // 是否为debug模式
	LogFilePath string `yaml:"log_file_path" json:"log_file_path"` // 日志路径
}

type File

type File struct {
	sync.Mutex                // 同步锁
	Log        *zdpgo_log.Log // 日志
	Config     *Config        // 配置对象
}

File 文件对象

func New added in v0.1.2

func New() *File

New 新建文件对象

func NewWithConfig added in v0.1.4

func NewWithConfig(config Config) *File

NewWithConfig 使用配置创建File对象

func (*File) CopyFile added in v0.1.4

func (f *File) CopyFile(src, dest string) error

CopyFile 复制文件 @param src 源文件地址 @param dest 目标文件地址

func (*File) CreateDirFile added in v0.1.4

func (f *File) CreateDirFile(dirtPath, fileName, content string) bool

CreateDirFile 在指定文件夹中创建文件

func (*File) CreateFile added in v0.1.4

func (f *File) CreateFile(filePath, content string) bool

CreateFile 创建文件

func (*File) CreateMultiDir added in v0.1.3

func (f *File) CreateMultiDir(dirtPath string) bool

CreateMultiDir 调用os.MkdirAll递归创建文件夹

func (*File) DeleteDir added in v0.1.4

func (f *File) DeleteDir(dirPath string) bool

DeleteDir 删除文件夹

func (*File) DeleteDirFile added in v0.1.4

func (f *File) DeleteDirFile(dirPath, fileName string) bool

DeleteDirFile 删除指定目录中的指定文件

func (*File) GetDirectorySize added in v0.1.3

func (f *File) GetDirectorySize(path string) (int64, error)

GetDirectorySize 文件夹占用磁盘大小

func (*File) GetFileDir added in v0.1.4

func (f *File) GetFileDir(absolutePath string) (fileDir string)

GetFileDir 获取文件路径

func (*File) GetFileDirAndFileName added in v0.1.4

func (f *File) GetFileDirAndFileName(absolutePath string) (fileDir string, fileName string)

GetFileDirAndFileName 获取文件路径和文件名

func (*File) GetFileName added in v0.1.4

func (f *File) GetFileName(absolutePath string) (fileName string)

GetFileName 获取文件名称

func (*File) GetFileSuffix added in v0.1.4

func (f *File) GetFileSuffix(absolutePath string) (suffix string)

GetFileSuffix 获取文件后缀

func (*File) IsDir added in v0.1.4

func (f *File) IsDir(dirPath string) bool

IsDir 判断所给路径是否为文件夹

func (*File) IsDirContainsFile added in v0.1.4

func (f *File) IsDirContainsFile(dirPath, fileName string) bool

IsDirContainsFile 判断文件夹中是否包含指定文件

func (*File) IsExists added in v0.1.4

func (f *File) IsExists(path string) bool

IsExists 判断所给路径文件/文件夹是否存在(返回true是存在)

func (*File) IsFile added in v0.1.4

func (f *File) IsFile(path string) bool

IsFile 判断所给路径是否为文件

func (*File) ReadCsv added in v0.1.3

func (f *File) ReadCsv(fileName string) (data [][]string, err error)

ReadCsv 读取csv

func (*File) RemoveDirFilesSuffix added in v0.1.3

func (f *File) RemoveDirFilesSuffix(dirPath string) error

RemoveDirFilesSuffix 去掉指定目录下所有文件的后缀

func (*File) Rename added in v0.1.3

func (f *File) Rename()

func (*File) ReplaceDirFilesName added in v0.1.3

func (f *File) ReplaceDirFilesName(dirPath string, oldStr, newStr string) error

ReplaceDirFilesName 替换指定目录下的文件名

func (*File) Size added in v0.1.3

func (f *File) Size(path string) int64

Size 获取文件大小

func (*File) WriteCsv added in v0.1.3

func (f *File) WriteCsv(filePath string, data [][]string) (err error)

WriteCsv 将数据保存为csv文件

Directories

Path Synopsis
examples
z01_csv command
z02_cirectory command
z03_download command
z04_file command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL