fileutil

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MD5    = "md5"
	SHA1   = "sha1"
	SHA256 = "sha256"
	SHA512 = "sha512"
)

Variables

This section is empty.

Functions

func CopyDir added in v0.1.8

func CopyDir(srcPath string, dstPath string, mode os.FileMode) error

CopyDir 递归复制目录

func CopyFile added in v0.1.7

func CopyFile(src, dst string) error

CopyFile 高性能拷贝文件 (支持大文件、零拷贝)

func DirSize added in v0.1.7

func DirSize(path string) (int64, error)

DirSize 计算目录总大小

func EnsureDir added in v0.1.7

func EnsureDir(dir string, perm os.FileMode) error

EnsureDir 确保目录存在(不存在则创建)

func Exists added in v0.1.7

func Exists(path string) bool

Exists 判断文件/目录是否存在。 注意,权限不足时也认为文件不存在,保守策略

func FileMode added in v0.1.7

func FileMode(path string) os.FileMode

FileMode 获取文件权限(unix风格)

func FileSize added in v0.1.7

func FileSize(path string) int64

FileSize 获取文件大小(字节数)

func HashBytes

func HashBytes(data []byte, alg string) (string, error)

针对 []byte 流式哈希。alg为"md5"|"sha1"|"sha256"|"sha512"

func HashFile

func HashFile(path string, alg string) (string, error)

针对 文件 流式哈希。alg为"md5"|"sha1"|"sha256"|"sha512"

func HashReader

func HashReader(r io.Reader, alg string) (string, error)

流式哈希,alg为"md5"|"sha1"|"sha256"|"sha512"

func IsDir added in v0.1.7

func IsDir(path string) bool

IsDir 判断路径是否为目录

func IsEmpty added in v0.1.7

func IsEmpty(path string) (bool, error)

IsEmpty 检查文件或目录是否为空。 对于文件,检查其大小是否为 0。 对于目录,检查其中是否没有任何条目。

func IsFile added in v0.1.7

func IsFile(path string) bool

IsFile 判断路径是否为普通文件

func IsReadable added in v0.1.7

func IsReadable(path string) bool

IsReadable 判断文件是否可读

func IsSameFile added in v0.1.7

func IsSameFile(path1, path2 string) bool

IsSameFile 判断两个路径是否是同一个文件 (inode)

func IsWritable added in v0.1.7

func IsWritable(path string) bool

IsWritable 判断文件是否可写

func IsZipFile added in v0.1.9

func IsZipFile(filepath string) (bool, error)

IsZipFile 函数

func RemoveAllFiles added in v0.1.7

func RemoveAllFiles(dir string) error

RemoveAllFiles 删除目录下所有文件但保留目录本身

func SafeRename added in v0.1.7

func SafeRename(src, dst string) error

SafeRename 原子替换文件 (Linux/Unix)

func SaveFile

func SaveFile(fileHeader *multipart.FileHeader, dstPath, fileType, expectedHash string) error

SaveFile 保存上传的文件 增加了严格的安全校验 参数: fileHeader *multipart.FileHeader: 上传的文件 dstPath string: 文件保存的目标路径 fileType: 文件类型, 如 "application/zip",可以为空,表示不进行文件类型校验 expectedHash string: 预期的文件的哈希值,用于严格校验,为空表示不进行校验

func UnzipSafe added in v0.1.9

func UnzipSafe(source, destination string, maxSize int64, maxFiles int) error

UnzipSafe 是一个经过安全加固的解压函数。 它能有效防御路径遍历(Zip Slip)、解压炸弹(Zip Bomb)、 符号链接攻击、不安全的文件权限以及非预期的文件类型(如管道、设备文件)。

参数:

source: zip 压缩包的文件路径。
destination: 解压目标目录。
maxSize: 允许解压的总大小上限(字节)。
maxFiles: 允许解压的文件数量上限。

func ZipDir added in v0.1.9

func ZipDir(folderPath, destPath string) (rerr error)

ZipDir — 递归压缩目录(安全+路径检查)

func ZipFiles added in v0.1.9

func ZipFiles(destZipPath string, srcFilePaths ...string) (rerr error)

ZipFiles 安全地将多个源文件压缩到一个目标 ZIP 文件中。

destZipPath: 目标 ZIP 文件的路径。如果文件已存在,它将被覆盖。 srcFilePaths: 一个或多个源文件的路径(可变参数)。

该函数具有以下特点: 1. 安全性:只将文件的基本名(如 "report.docx")存入 ZIP,避免泄露本地文件结构。 2. 高效性:使用流式处理(io.Copy),内存占用低,适合处理大文件。 3. 健壮性:对输入进行校验,确保只处理文件,不处理目录,并正确管理资源。

Types

This section is empty.

Jump to

Keyboard shortcuts

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