Files
4566704 c7e8eb14f8 feat(filex): 自 go-hua/common 迁入文件目录工具并独立成包
- 运行目录定位、路径存在性、带进度回调下载、Zip 打包/解压(兼容 GBK 中文文件名)
- 附单元测试、examples/filex 例程与包 README
2026-09-20 12:20:50 +08:00

39 lines
1.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# filex
文件与目录工具:工作目录/运行目录定位、路径存在性判断、带进度回调的文件下载、
Zip 打包与解压(兼容 GBK 编码的中文文件名)。
> 迁移自 go-hua/common(原 dir.go / path.go / downFile.go / zip.go)。
## 用法
```go
import "git.zeroonesoft.cn/golib/zogo/filex"
// 目录定位
filex.GetWorkDir() // os.Getwd()
filex.GetRunDir() // 可执行文件所在目录(兼容 go run,尾带分隔符)
filex.GetCurrentAbPathByCaller()
// 路径存在性
exists, err := filex.PathExists("/path/to/x")
// 下载文件(fb 进度回调:总长度、已下载长度)
err := filex.DownloadFile("https://example.com/a.zip", "D:/tmp/a.zip",
func(length, downLen int64) { ... })
// Zip 打包(目录或单文件)/ 解压(Flags==0 按 GB18030 解码文件名,兼容中文 zip)
err := filex.Zip("D:/data/src", "D:/tmp/src.zip")
err := filex.Unzip("D:/tmp/src.zip", "D:/data/out")
```
完整可运行例程:[examples/filex/main.go](../examples/filex/main.go)
## 注意
- `DownloadFile` 跳过 TLS 证书校验(`InsecureSkipVerify: true`),下载超时 300s,
只支持 GET;不要用于对安全性敏感的场景。
- `Zip` 目录项名以 `\` 结尾(历史行为,兼容易语言侧解压);`Unzip` 已兼容 UTF-8
与 GBK 两种文件名编码。
- `GetRunDir` 在 `go run`(临时目录)场景回退到工作目录。