cyfile

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckDependencies

func CheckDependencies(deps ...string) error

CheckDependencies checks if the specified command-line tools are available in PATH. Returns an error listing all missing dependencies if any are not found.

func CheckDependenciesWithWarning

func CheckDependenciesWithWarning(deps ...string)

CheckDependenciesWithWarning checks if the specified tools are available and prints warnings for missing dependencies, but does not return an error.

func CopyDir

func CopyDir(src, dst string) error

CopyDir 递归复制目录及其所有内容。

参数:

  • src: 源目录路径
  • dst: 目标目录路径

返回值:

  • error: 如果源不是目录或复制失败返回错误

用法:

err := CopyDir("/src/project", "/dst/project")
if err != nil {
    log.Fatal(err)
}

特点:

  • 递归复制所有子目录和文件
  • 保留目录权限
  • 自动创建目标目录

局限:

  • 源必须是目录
  • 复制失败时可能留下部分文件

func CopyFile

func CopyFile(src, dst string) error

CopyFile 复制文件从源到目标。

参数:

  • src: 源文件路径
  • dst: 目标文件路径

返回值:

  • error: 如果源不是文件或复制失败返回错误

用法:

err := CopyFile("/src/file.txt", "/dst/file.txt")
if err != nil {
    log.Fatal(err)
}

特点:

  • 如果源和目标相同则直接返回
  • 优先尝试创建硬链接(快速)
  • 硬链接失败时复制文件内容
  • 保留源文件的权限

局限:

  • 源必须是文件,不能是目录
  • 硬链接可能不支持跨文件系统

func CopyPath

func CopyPath(src, dst string) error

CopyPath 复制文件或目录。

参数:

  • src: 源路径(文件或目录)
  • dst: 目标路径

返回值:

  • error: 如果复制失败返回错误

用法:

err := CopyPath("/src", "/dst")
if err != nil {
    log.Fatal(err)
}

特点:

  • 自动判断源是文件还是目录
  • 文件调用 CopyFile,目录调用 CopyDir

局限:

  • 源路径必须存在

func EnsureDir

func EnsureDir(dir string) error

EnsureDir 确保目录存在,如果不存在则创建。

参数:

  • dir: 目录路径

返回值:

  • error: 如果创建失败返回错误

用法:

err := EnsureDir("/path/to/dir")
if err != nil {
    log.Fatal(err)
}

局限:

  • 创建权限为 0755
  • 会创建所有必要的父目录

func ExtractTarGz

func ExtractTarGz(targzPath, targetDir string) error

ExtractTarGz extracts .tar.gz archive to the specified directory using Go's standard libraries.

func GetExeDir

func GetExeDir() (string, error)

GetExeDir 获取当前可执行文件所在的目录。

返回值:

  • string: 可执行文件所在的目录路径
  • error: 如果获取失败返回错误

用法:

exeDir, err := GetExeDir()
if err != nil {
    log.Fatal(err)
}
fmt.Println("Executable directory:", exeDir)

局限:

  • 在某些特殊环境下可能失败
  • 返回的是符号链接解析后的真实路径

func GetFileSize

func GetFileSize(path string) (int64, error)

func GetFileType

func GetFileType(fileName string) string

func GetParentDirName

func GetParentDirName(path string) (string, error)

GetParentDirName 获取路径的父目录名称(倒数第二级目录)

func GzipFile

func GzipFile(filePath string, removeOriginal ...bool) (string, error)

GzipFile 将单个文件压缩为 .gz 文件。

参数:

  • filePath: 需要压缩的文件路径
  • removeOriginal: 可选参数,是否在压缩成功后删除原文件(默认 false)

返回值:

  • string: 生成的 .gz 文件路径
  • error: 压缩或写入失败时返回错误

用法:

gzPath, err := GzipFile("/path/to/file.txt")           // 仅压缩
gzPath, err := GzipFile("/path/to/file.txt", true)     // 压缩后删除原文件

特点:

  • 使用 gzip 默认压缩等级
  • 自动设置 gzip 头部文件名
  • 支持可选删除原文件,适合节省磁盘空间

func HasCommand

func HasCommand(cmd string) bool

HasCommand checks if a specific command is available in PATH.

func HasSudoAccess

func HasSudoAccess() bool

HasSudoAccess checks if the current user has sudo privileges without password.

func IsDir

func IsDir(path string) bool

IsDir 检查路径是否为目录。

参数:

  • path: 文件或目录路径

返回值:

  • bool: 是目录返回 true,否则返回 false

用法:

if IsDir("/path/to/dir") {
    fmt.Println("It's a directory")
}

局限:

  • 路径不存在时返回 false
  • 不检查权限

func IsRoot

func IsRoot() bool

IsRoot returns true if the current process is running as root.

func MatchFiles

func MatchFiles(dir, rule string) ([]string, error)

MatchFiles 遍历指定目录并返回符合规则的文件列表。

参数:

  • dir: 目录路径
  • rule: 文件名匹配规则(支持 glob 模式,如 "*.txt")

返回值:

  • []string: 匹配的文件路径列表
  • error: 如果目录不存在或读取失败返回错误

用法:

files, err := MatchFiles("/path/to/dir", "*.txt")
if err != nil {
    log.Fatal(err)
}
for _, f := range files {
    fmt.Println(f)
}

局限:

  • 只返回文件,不返回目录
  • 不递归搜索子目录
  • 规则遵循 filepath.Match 的 glob 语法

func MatchSingleFile

func MatchSingleFile(dir, rule string) (string, error)

MatchSingleFile 在指定目录中查找符合规则的文件,必须恰好匹配一个文件。

参数:

  • dir: 目录路径
  • rule: 文件名匹配规则(支持 glob 模式)

返回值:

  • string: 匹配的文件路径
  • error: 如果匹配到 0 个或多于 1 个文件,返回错误

用法:

file, err := MatchSingleFile("/path/to/dir", "config.yaml")
if err != nil {
    log.Fatal(err)
}
fmt.Println("Found:", file)

局限:

  • 必须恰好匹配一个文件,否则返回错误
  • 不递归搜索子目录

func MatchSingleFileOrEmpty

func MatchSingleFileOrEmpty(dir, rule string) (string, error)

MatchSingleFileOrEmpty 在指定目录中查找符合规则的文件,最多返回一个文件。

参数:

  • dir: 目录路径
  • rule: 文件名匹配规则(支持 glob 模式)

返回值:

  • string: 匹配的文件路径,如果没有匹配返回空字符串
  • error: 如果匹配到多于 1 个文件,返回错误;否则返回 nil

用法:

file, err := MatchSingleFileOrEmpty("/path/to/dir", "optional.txt")
if err != nil {
    log.Fatal(err)
}
if file != "" {
    fmt.Println("Found:", file)
}

局限:

  • 最多返回一个文件,如果有多个匹配返回错误
  • 不递归搜索子目录

func MovePath

func MovePath(src, dst string) error

MovePath 移动或重命名文件/目录。

参数:

  • src: 源路径
  • dst: 目标路径

返回值:

  • error: 如果移动失败返回错误

用法:

err := MovePath("/old/path", "/new/path")
if err != nil {
    log.Fatal(err)
}

特点:

  • 优先使用 os.Rename(快速)
  • 跨文件系统时自动复制后删除
  • 支持文件和目录

局限:

  • 跨文件系统时较慢
  • 目标已存在时可能失败

func PathExists

func PathExists(path string) bool

PathExists 检查文件或目录是否存在。

参数:

  • path: 文件或目录路径

返回值:

  • bool: 存在返回 true,不存在返回 false

用法:

if PathExists("/path/to/file") {
    fmt.Println("File exists")
}

局限:

  • 不检查权限,只检查存在性
  • 不区分文件和目录

func ReadFileFromZip

func ReadFileFromZip(zipPath string, filePath string) ([]byte, error)

ReadFileFromZip reads a specific file from a ZIP archive and returns its content. zipPath: path to the ZIP file filePath: path of the file inside the ZIP archive (use forward slashes) Returns the file content as bytes or an error if the file is not found or cannot be read.

func RemovePath

func RemovePath(path string) error

RemovePath 删除文件或目录及其所有内容。

参数:

  • path: 文件或目录路径

返回值:

  • error: 如果删除失败返回错误

用法:

err := RemovePath("/path/to/remove")
if err != nil {
    log.Fatal(err)
}

特点:

  • 递归删除目录及其内容
  • 路径不存在时返回 nil
  • 删除失败时返回第一个错误

局限:

  • 无法恢复已删除的文件
  • 权限不足时会失败

func RequireRootOrSudo

func RequireRootOrSudo() error

RequireRootOrSudo checks if the current process is running with root privileges or has sudo access. Returns an error if neither condition is met.

func SplitFilePath

func SplitFilePath(filePath string) (string, string, string)

SplitFilePath 拆分文件路径为目录、文件名和扩展名。

参数:

  • filePath: 文件路径

返回值:

  • string: 目录路径
  • string: 文件名(不包含扩展名)
  • string: 文件扩展名(包含点号)

用法:

dir, name, ext := SplitFilePath("/path/to/file.txt")
// dir = "/path/to", name = "file", ext = ".txt"

局限:

  • 处理 Windows 路径时会转换为 Unix 风格
  • 扩展名包含点号

func UnzipFile

func UnzipFile(zipfile, target string) error

UnzipFile 解压 ZIP 文件到目标目录。

参数:

  • zipfile: ZIP 文件路径
  • target: 目标目录路径

返回值:

  • error: 如果解压失败返回错误

用法:

err := UnzipFile("archive.zip", "/target/dir")
if err != nil {
    log.Fatal(err)
}

特点:

  • 自动创建目标目录
  • 保留目录结构
  • 防止路径遍历攻击
  • 保留文件权限

局限:

  • 目标目录必须可写
  • 如果文件已存在会被覆盖

func WalkZip

func WalkZip(zipfile string, ext string, proc func(path string, r io.Reader) error) error

WalkZip 遍历 ZIP 文件中指定扩展名的文件。

参数:

  • zipfile: ZIP 文件路径
  • ext: 文件扩展名(如 ".txt")
  • proc: 处理函数,接收文件路径和读取器

返回值:

  • error: 如果打开或处理失败返回错误

用法:

err := WalkZip("archive.zip", ".txt", func(path string, r io.Reader) error {
    data, _ := io.ReadAll(r)
    fmt.Printf("File: %s, Size: %d\n", path, len(data))
    return nil
})

特点:

  • 只处理指定扩展名的文件
  • 自动关闭文件读取器
  • 处理函数返回错误时中断遍历

局限:

  • 扩展名必须包含点号(如 ".txt")
  • 处理函数中的错误会中断遍历

func WalkZipOrdered

func WalkZipOrdered(zipfile string, ext string, proc func(path string, r io.Reader) error, sorts ...string) error

sorts 参数指定优先处理的文件名(不含扩展名),其余文件随后处理

func WalkerZip

func WalkerZip(zipfile string, ext string, proc func(path string, r io.Reader) error) error

WalkerZip 已废弃: 使用 WalkZip 代替 Deprecated: Use WalkZip instead.

func ZipFolder

func ZipFolder(folder string) (string, error)

ZipFolder 将目录压缩为 ZIP 文件。

参数:

  • folder: 需要压缩的目录路径

返回值:

  • string: 生成的 ZIP 文件路径(目录路径 + ".zip")
  • error: 如果压缩失败返回错误

用法:

zipPath, err := ZipFolder("/path/to/folder")
if err != nil {
    log.Fatal(err)
}
fmt.Println("Created:", zipPath)  // /path/to/folder.zip

特点:

  • 递归压缩所有子目录和文件
  • 使用 Deflate 压缩算法
  • 保留目录结构

局限:

  • ZIP 文件名为目录名 + ".zip"
  • 如果 ZIP 文件已存在会被覆盖

Types

This section is empty.

Jump to

Keyboard shortcuts

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