filejez

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package filejez 文件相关函数

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyFile

func CopyFile(src, dst string) error

CopyFile 拷贝文件

Example
dir := "./testdata/TestCopyFile/"

src := dir + "test-file-exist.txt"
dst := dir + "test-file-exist2.txt"

data := "test"

_ = CreateDirs(dir)
_ = CreateFileWithData(src, data)

_ = CopyFile(src, dst)

res, _ := ReadAll(dst)

fmt.Println(res)
Output:

test

func CreateDirs

func CreateDirs(dirPaths ...string) error

CreateDirs 创建目录,包含子目录,如果目录已存在,则忽略

Example
dir := "./testdata/TestCreateDirs/"

paths := []string{
	dir + "test-dir-create1",
	dir + "test-dir-create2",
}

_ = CreateDirs(paths...)

for _, path := range paths {
	fmt.Println(DirExists(path))
}
Output:

true <nil>
true <nil>

func CreateFileWithData

func CreateFileWithData(filePath, data string) error

CreateFileWithData 创建文件并写入字符串数据

Example
dir := "./testdata/TestCreateFileWithData/"

path := dir + "test-file-data.txt"

data := "test"

_ = CreateDirs(dir)

_ = CreateFileWithData(path, data)

data2, _ := ReadAll(path)

fmt.Println(data2)
Output:

test

func CreateFiles

func CreateFiles(filePaths ...string) error

CreateFiles 创建文件,如果文件已存在,则忽略

Example
dir := "./testdata/TestCreateFiles/"

paths := []string{
	dir + "test-file-create1.txt",
	dir + "test-file-create2.txt",
}

_ = CreateDirs(dir)

_ = CreateFiles(paths...)

for _, path := range paths {
	fmt.Println(FileExists(path))
}
Output:

true <nil>
true <nil>

func CreateFilesWithDirs

func CreateFilesWithDirs(filePaths ...string) error

CreateFilesWithDirs 创建文件,如果文件已存在,则忽略,同时创建目录,包含子目录

Example
dir := "./testdata/TestCreateFilesWithDirs/"

paths := []string{
	dir + "test-file-create1.txt",
	dir + "test-file-create2.txt",
}

_ = CreateFilesWithDirs(paths...)

for _, path := range paths {
	fmt.Println(FileExists(path))
}
Output:

true <nil>
true <nil>

func DeleteDirs

func DeleteDirs(dirPaths ...string) error

DeleteDirs 删除目录

Example
dir := "./testdata/TestDeleteDirs/"

paths := []string{
	dir + "test-dir-delete1",
	dir + "test-dir-delete2",
}

_ = CreateDirs(paths...)

for _, path := range paths {
	fmt.Println(DirExists(path))
}

_ = DeleteDirs(dir)

for _, path := range paths {
	fmt.Println(DirExists(path))
}
Output:

true <nil>
true <nil>
false <nil>
false <nil>

func DeleteEmptyDirWalk

func DeleteEmptyDirWalk(dirPath string) error

DeleteEmptyDirWalk 返回删除空目录,包含子目录

例如:

  • /a/b,当删除 b 时,如果 a 也是一个空目录,也会被删除
Example
dir := "./testdata/TestDeleteEmptyDirWalk/"

paths := []string{
	dir + "test-dirs-delete1",
	dir + "test-dirs-delete2",
}

_ = CreateDirs(paths...)

for _, path := range paths {
	fmt.Println(DirExists(path))
}

_ = DeleteEmptyDirWalk(dir)

for _, path := range paths {
	fmt.Println(DirExists(path))
}

fmt.Println(DirExists(dir))
Output:

true <nil>
true <nil>
false <nil>
false <nil>
false <nil>

func DeleteFiles

func DeleteFiles(filePaths ...string) error

DeleteFiles 删除文件

Example
dir := "./testdata/TestDeleteFiles/"

paths := []string{
	dir + "test-file-delete1.txt",
	dir + "test-file-delete2.txt",
}

_ = CreateDirs(dir)

_ = CreateFiles(paths...)

for _, path := range paths {
	fmt.Println(FileExists(path))
}

_ = DeleteFiles(paths...)

for _, path := range paths {
	fmt.Println(FileExists(path))
}
Output:

true <nil>
true <nil>
false <nil>
false <nil>

func DeleteWalkBy

func DeleteWalkBy(
	dirPath string, iteratee func(path string, entry os.DirEntry) (bool, error), withEmptyDir ...bool) (bool, error)

DeleteWalkBy 递归删除指定目录下的文件和子目录

参数:

  • dirPath: 目录路径
  • iteratee: 遍历函数,用于处理每个文件(不包括目录)的逻辑,接收文件路径和 os.DirEntry 实例作为参数
  • withEmptyDir: 可选参数,指定是否删除空目录,默认为 false

返回值:

  • bool: 当前目录是否已被删除
  • error: 错误

注意事项:

  • 如果要删除空目录,请注意 bool 返回值,错误的 bool 返回值可能导致文件意外删除或空目录删除失败。
Example
dir := "./testdata/TestDeleteWalkBy/"

filePaths := []string{
	dir + "test-dirs-delete.txt",
	dir + "test-dirs-delete.txt",
}

dir2 := dir + "test-dirs"

// 创建文件
_ = CreateFilesWithDirs(filePaths...)

for _, path := range filePaths {
	fmt.Println(FileExists(path))
}

// 创建空目录
_ = CreateDirs(dir2)

fmt.Println(DirExists(dir2))

// 删除包含空目录
isDelete, _ := DeleteWalkBy(dir, func(path string, entry os.DirEntry) (bool, error) {
	return true, nil
}, true)

fmt.Println(isDelete)
fmt.Println(DirExists(dir2))
fmt.Println(DirExists(dir))
Output:

true <nil>
true <nil>
true <nil>
true
false <nil>
false <nil>

func DirExists

func DirExists(dirPath string) (bool, error)

DirExists 判断目录是否存在

Example
dir := "./testdata/TestDirExists/"

_ = CreateDirs(dir)

exist, _ := DirExists(dir)

fmt.Println(exist)
Output:

true

func FileExists

func FileExists(filePath string) (bool, error)

FileExists 判断文件是否存在

Example
dir := "./testdata/TestFileExists/"

path := dir + "test-file-exist.txt"

_ = CreateFilesWithDirs(path)

exist, _ := FileExists(path)

fmt.Println(exist)
Output:

true

func Filenames

func Filenames(dirPath string) ([]string, error)

Filenames 返回目录下的文件名切片

Example
dir := "./testdata/TestFilenames/"

path := dir + "/test-dir/test-file.txt"
path1 := dir + "test-file1.txt"

_ = CreateFilesWithDirs(path, path1)

filenames, _ := Filenames(dir)

fmt.Println(filenames)
Output:

[test-file1.txt]

func FilenamesBy

func FilenamesBy(dirPath string, iteratee func(path string, entry os.DirEntry) string) ([]string, error)

FilenamesBy 遍历目录下的文件,对每个文件调用 iteratee 函数,将返回的字符串添加到切片中

Example
dir := "./testdata/TestFilenamesBy/"

path := dir + "/test-dir/test-file.txt"
path1 := dir + "test-file1.txt"

_ = CreateFilesWithDirs(path, path1)

filenames, _ := FilenamesBy(dir, func(path string, entry os.DirEntry) string {
	return "by-" + entry.Name()
})

fmt.Println(filenames)
Output:

[by-test-file1.txt]

func FilenamesFilter

func FilenamesFilter(dirPath string, iteratee func(path string, entry os.DirEntry) bool) ([]string, error)

FilenamesFilter 遍历目录下的文件,对每个文件调用 iteratee 函数,如果返回 true,则将文件名添加到切片中

Example
dir := "./testdata/TestFilenamesFilter/"

path := dir + "/test-dir/test-file.txt"
path1 := dir + "test-file1.txt"

_ = CreateFilesWithDirs(path, path1)

filenames, _ := FilenamesFilter(dir, func(path string, entry os.DirEntry) bool {
	return true
})

fmt.Println(filenames)
Output:

[test-file1.txt]

func FilenamesWalk

func FilenamesWalk(dirPath string) ([]string, error)

FilenamesWalk 返回目录下的文件名切片,包含子目录

Example
dir := "./testdata/TestFilenamesWalk/"

path := dir + "/test-dir/test-file.txt"
path1 := dir + "test-file1.txt"

_ = CreateFilesWithDirs(path, path1)

filenames, _ := FilenamesWalk(dir)

fmt.Println(filenames)
Output:

[test-file.txt test-file1.txt]

func FilenamesWalkBy

func FilenamesWalkBy(dirPath string, iteratee func(path string, entry os.DirEntry) string) ([]string, error)

FilenamesWalkBy 返回目录下的文件,包含子目录,对每个文件调用 iteratee 函数,将返回的字符串添加到切片中

Example
dir := "./testdata/TestFilenamesWalkBy/"

path := dir + "/test-dir/test-file.txt"
path1 := dir + "test-file1.txt"

_ = CreateFilesWithDirs(path, path1)

filenames, _ := FilenamesWalkBy(dir, func(path string, entry os.DirEntry) string {
	return "by-" + entry.Name()
})

fmt.Println(filenames)
Output:

[by-test-file.txt by-test-file1.txt]

func FilenamesWalkFilter

func FilenamesWalkFilter(dirPath string, iteratee func(path string, entry os.DirEntry) bool) ([]string, error)

FilenamesWalkFilter 返回目录下的文件,包含子目录,对每个文件调用 iteratee 函数,如果返回 true,则将文件名添加到切片中

Example
dir := "./testdata/TestFilenamesWalkFilter/"

path := dir + "/test-dir/test-file.txt"
path1 := dir + "test-file1.txt"

_ = CreateFilesWithDirs(path, path1)

filenames, _ := FilenamesWalkFilter(dir, func(path string, entry os.DirEntry) bool {
	return true
})

fmt.Println(filenames)
Output:

[test-file.txt test-file1.txt]

func FilterMap

func FilterMap[T any](dirPath string, iteratee func(entry os.DirEntry) (T, bool)) ([]T, error)

FilterMap 遍历当前目录,对每个文件调用 iteratee,如果返回 true,则将结果放入结果集中

Example
dir := "./testdata/TestFilterMap/"

dirPath := dir + "test-dir"
_ = CreateDirs(dirPath)

filePath := dir + "test-file.txt"
_ = CreateFiles(filePath)

resInt, _ := FilterMap(dir, func(entry os.DirEntry) (int, bool) {
	if !entry.IsDir() {
		return 1, true
	}

	return 0, false
})

fmt.Println(resInt)
Output:

[1]

func FilterMapWalk

func FilterMapWalk[T any](dirPath string, iteratee func(path string, d os.DirEntry) (T, bool)) ([]T, error)

FilterMapWalk 返回遍历所有目录、子目录,对每个文件调用 iteratee,如果返回 true,则将结果放入结果集中

Example
dir := "./testdata/TestFilterMapWalk/"

path := dir + "test-dir/test.txt"

_ = CreateFilesWithDirs(path)

resInt, _ := FilterMapWalk(path, func(path string, entry os.DirEntry) (int, bool) {
	if entry.Name() == "test.txt" {
		return 1, true
	}

	return 0, false
})

fmt.Println(resInt)
Output:

[1]

func FindFileWalk

func FindFileWalk(dirPath, filename string) (bool, error)

FindFileWalk 遍历目录、子目录,查找文件

Example
dir := "./testdata/TestFindFileWalk/"

path := dir + "/test-dir/test-file.txt"

_ = CreateFilesWithDirs(path)

exist, _ := FindFileWalk(dir, "test-file.txt")

fmt.Println(exist)
Output:

true

func FindFileWalkFilter

func FindFileWalkFilter(dirPath string, iteratee func(path string, entry os.DirEntry) bool) (bool, error)

FindFileWalkFilter 遍历目录、子目录,查找文件,对每个文件调用 iteratee 函数,如果返回 true,则表示找到了

Example
dir := "./testdata/TestFindFileWalkFilter/"

path := dir + "/test-dir/test-file.txt"
path1 := dir + "/test-dir/test-file1.txt"

_ = CreateFilesWithDirs(path, path1)

exist, _ := FindFileWalkFilter(dir, func(path string, entry os.DirEntry) bool {
	return entry.Name() == "test-file.txt"
})

fmt.Println(exist)
Output:

true

func IsDir

func IsDir(dirPath string) (bool, error)

IsDir 判断是否是目录

Example
dir := "./testdata/TestIsDir/"

_ = CreateDirs(dir)

exist, _ := IsDir(dir)

fmt.Println(exist)
Output:

true

func IsEmptyDir

func IsEmptyDir(dirPath string) (bool, error)

IsEmptyDir 判断目录是否为空

Example
dir := "./testdata/TestIsEmptyDir/"

_ = CreateDirs(dir)

exist, _ := IsEmptyDir(dir)

fmt.Println(exist)
Output:

true

func OsCreate

func OsCreate(filePath string) (*os.File, error)

OsCreate 等同于 os.Create,创建文件,如果文件已存在,则忽略,使用完毕后需要关闭

Example
dir := "./testdata/TestCreateFileWithOS/"

path := dir + "test-file.txt"

_ = CreateDirs(dir)

file, _ := OsCreate(path)
_ = file.Close()

fmt.Println(FileExists(path))
Output:

true <nil>

func OverwriteFiles

func OverwriteFiles(filePaths ...string) error

OverwriteFiles 创建文件,如果文件已存在,则覆盖

Example
dir := "./testdata/TestOverwriteFiles/"

paths := []string{
	dir + "test-file-create1.txt",
	dir + "test-file-create2.txt",
}

_ = CreateDirs(dir)

_ = OverwriteFiles(paths...)

for _, path := range paths {
	fmt.Println(FileExists(path))
}
Output:

true <nil>
true <nil>

func OverwriteFilesWithDirs

func OverwriteFilesWithDirs(filePaths ...string) error

OverwriteFilesWithDirs 创建文件,如果文件已存在,则覆盖,同时创建目录,包含子目录

Example
dir := "./testdata/TestOverwriteFilesWithDirs/"

paths := []string{
	dir + "test-file-create1.txt",
	dir + "test-file-create2.txt",
}

_ = OverwriteFilesWithDirs(paths...)

for _, path := range paths {
	fmt.Println(FileExists(path))
}
Output:

true <nil>
true <nil>

func ReadAll

func ReadAll(filePath string) (string, error)

ReadAll 将文件的所有内容读取为字符串。

Example
dir := "./testdata/TestReadAll/"

path := dir + "test-file-data.txt"

data := "test"

_ = CreateDirs(dir)
_ = CreateFileWithData(path, data)

data2, _ := ReadAll(path)

fmt.Println(data2)
Output:

test

func ReadLines

func ReadLines(filePath string, n int) ([]string, error)

ReadLines 读取文件的前 n 行,如果 n < 0,则读取所有行。

Example
dir := "./testdata/TestReadLine/"

path := dir + "test-file-data.txt"

data := "test\ntest"

_ = CreateDirs(dir)
_ = CreateFileWithData(path, data)

data2, _ := ReadLines(path, 1)

fmt.Println(data2)
Output:

[test]

func Unzip

func Unzip(src, dst string) error

Unzip 解压 zip 文件到指定目录,如果目录不存在,则会被创建。

Example
src := "./test-file-zip.zip"

dst := "./Unzip/"

_ = CreateDirs(dst)

_ = Unzip(src, dst)

fmt.Println(DirExists(dst))
Output:

true <nil>

func Zip

func Zip(src, dst string) error

Zip 将目录或文件压缩为 zip 文件,如果zip已存在,则会被覆盖。

Example
dir := "./testdata/TestZip/"

_ = CreateDirs(dir)

// target := "./test-file-zip.zip"

target := dir + "test-file-zip.zip"

// 将test-dir-walk 压缩到 ./testdata/TestZip/test-file-zip.zip
_ = Zip("./test-dir-walk", target)

fmt.Println(FileExists(target))
Output:

true <nil>

func ZipFilter

func ZipFilter(src, dst string, iteratee func(path string, entry os.DirEntry) bool) error

ZipFilter 对每个文件或目录调用 iteratee 函数,如果返回 true,则将其压缩到 zip 文件中,如果zip文件已存在,则会被覆盖。

Example
dir := "./testdata/TestZipFilter/"

_ = CreateDirs(dir)

// target := "./test-file-zip.zip"

target := dir + "test-file-zip.zip"

// 只把 将test-dir-walk 压缩到 ./testdata/TestZip/test-file-zip.zip
_ = ZipFilter(".", target, func(path string, entry os.DirEntry) bool {
	return path == "./test-dir-walk"
})

fmt.Println(FileExists(target))
Output:

true <nil>

Types

This section is empty.

Jump to

Keyboard shortcuts

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