文件系统
类: FileSystem
提供文件系统操作的单例类。
静态方法
getInstance
static getInstance(): FileSystem
获取 FileSystem 的单例实例。
- 返回: FileSystem 实例
方法
ls
ls(targetPath: string, directory?: boolean): Promise<FileInfo[]>
列出指定路径下的文件和目录。
- 参数:
targetPath: 要列出的路径directory: 可选标志,用于过滤目录
- 返回:
Promise<FileInfo[]>
exists
exists(p: string): Promise<boolean>
检查文件或目录是否存在。
- 参数:
p: 要检查的路径
- 返回:
Promise<boolean>
isDirectory
isDirectory(p: string): Promise<boolean>
检查路径是否为目录。
- 参数:
p: 要检查的路径
- 返回:
Promise<boolean>
isFile
isFile(p: string): Promise<boolean>
检查路径是否为文件。
- 参数:
p: 要检查的路径
- 返回:
Promise<boolean>
mkdir
mkdir(p: string, parents?: boolean): Promise<boolean>
创建目录。
- 参数:
p: 目录路径parents: 可选标志,用于创建父目录
- 返回:
Promise<boolean>
cp
cp(source: string, target: string): Promise<boolean>
复制文件或目录。
- 参数:
source: 源路径target: 目标路径
- 返回:
Promise<boolean>
mv
mv(source: string, target: string): Promise<boolean>
移动文件或目录。
- 参数:
source: 源路径target: 目标路径
- 返回:
Promise<boolean>
rm
rm(p: string): Promise<boolean>
删除文件。
- 参数:
p: 文件路径
- 返回:
Promise<boolean>
rmdir
rmdir(p: string, recursive?: boolean): Promise<boolean>
删除目录。
- 参数:
p: 目录路径recursive: 可选标志,用于递归删除
- 返回:
Promise<boolean>
ren
ren(source: string, name: string): Promise<boolean>
重命名文件或目录。
- 参数:
source: 源路径name: 新名称
- 返回:
Promise<boolean>
cat
cat(p: string): Promise<string>
读取文件内容为字符串。
- 参数:
p: 文件路径
- 返回:
Promise<string>
read
read(p: string): Promise<string>
cat 的别名。
- 参数:
p: 文件路径
- 返回:
Promise<string>
write
write(p: string, text?: string, mode?: WriteMode): Promise<boolean>
写入内容到文件。
- 参数:
p: 文件路径text: 可选的内容mode: 可选的写入模式(截断/创建/追加)
- 返回:
Promise<boolean>
touch
touch(p: string): Promise<boolean>
创建空文件或更新其时间戳。
- 参数:
p: 文件路径
- 返回:
Promise<boolean>
zip
zip(source: string[], target: string): Promise<boolean>
创建 zip 压缩文件。
- 参数:
source: 源路径数组target: 目标 zip 文件路径
- 返回:
Promise<boolean>
unzip
unzip(source: string, target: string): Promise<boolean>
解压 zip 文件。
- 参数:
source: 源 zip 文件路径target: 目标目录路径
- 返回:
Promise<boolean>
tar
tar(source: string[], target: string, mode?: CompressionMode): Promise<boolean>
创建 tar 压缩文件。
- 参数:
source: 源路径数组target: 目标 tar 文件路径mode: 可选的压缩模式
- 返回:
Promise<boolean>
untar
untar(source: string, target: string, mode?: CompressionMode): Promise<boolean>
解压 tar 文件。
- 参数:
source: 源 tar 文件路径target: 目标目录路径mode: 可选的压缩模式
- 返回:
Promise<boolean>
枚举
WriteMode
enum WriteMode {
TRUNCATE = "truncate",
CREATE = "create",
APPEND = "append"
}
CompressionMode
enum CompressionMode {
NONE = "",
GZIP = "gzip"
}
类型
FileInfo
interface FileInfo {
name: string;
fullname: string;
dirname: string;
mode: number;
size: number;
atimeMs: number;
mtimeMs: number;
ctimeMs: number;
}