- API 概览
- 开放能力
- 基础
- 渲染
- 设备
- 文件
- 位置
- 媒体
- 网络
- 游戏转发分享
- 实时语音
- 数据缓存
- 系统
- 广告
- 界面
- 支付
- Worker
FileSystemManager.unzip收藏我的收藏
收藏
我的收藏基础库 1.15.0 开始支持本方法,这是一个异步方法。
解压文件。
前提条件 | 无 |
业务背景 | 无 |
使用限制 | 由于开发者只有在 用户目录(ttfile://user) 才有写的权限, 因此 targetPath 必须以 ttfile://user 开头 |
注意事项 | 无 |
相关教程 | 无 |
语法
FileSystemManager.unzip(options)
参数说明
options 为 object 类型,属性如下:
属性名 | 类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
---|---|---|---|---|---|
zipFilePath | string | 是 | 源文件路径,只可以是 zip 压缩文件 | 1.15.0 | |
targetPath | string | 是 | 目标目录路径, 必须以 ttfile://user 开头 | 1.15.0 | |
success | function | 否 | 接口调用成功的回调函数 | 1.15.0 | |
fail | function | 否 | 接口调用失败的回调函数 | 1.15.0 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 1.15.0 |
回调成功
object 类型,属性如下:
属性名 | 类型 | 说明 | 最低支持版本 |
---|---|---|---|
errMsg | string | "FileSystemManager.unzip:ok" | 1.15.0 |
回调失败
object 类型,属性如下:
属性名 | 类型 | 说明 | 最低支持版本 |
---|---|---|---|
errMsg | string | "FileSystemManager.unzip:fail" + 详细错误信息 | 1.15.0 |
错误码
errNo | errMsg | 说明 | 最低支持版本 |
---|---|---|---|
20000 | unzip:fail {param_name} is invalid | zipFilePath 参数错误 | 1.99.0 |
20000 | unzip:fail {param_name} is invalid | targetPath 参数错误 | 1.99.0 |
20001 | unzip:fail param should be xxx, but got xxx | 参数校验错误 | 1.99.0 |
21101 | unzip:fail permission denied, unzip {path_value} | 无操作权限(源文件路径不可读/目标路径不可写) | 1.99.0 |
21102 | unzip:fail operation not permitted, unzip {zipFilePath_value} | 类型不正确(不是文件路径) | 1.99.0 |
21103 | unzip:fail no such file or directory, unzip {zipFilePath_value} -> {targetPath_value} | 目标/源路径文件不存在 | 1.99.0 |
21104 | unzip:fail user dir saved file size limit exceeded | 超出 user 目录大小限制,说明解压后的内容在放到 user 目录时,超过了 user 目录 的大小限制 。从 2.21.0 (包含)开始,每个小游戏 ttfile://user 开头的用户目录储存上限从 50M 提升为 200M, 每个小游戏之间储存相互隔离。单个小游戏储存不区分预览版,正式版,统一分配空间额度。 | 1.99.0 |
代码示例
const fileSystemManager = tt.getFileSystemManager();
// 下载一个 zip 文件
tt.downloadFile({
url: "https://sf1-cdn-tos.douyinstatic.com/obj/microapp/frontend/docs/images/test-4423286930445687.zip",
success(res) {
console.log("下载 zip 成功", res.tempFilePath);
// 解压 zip 文件
unzip(res.tempFilePath);
},
fail(res) {
console.log("下载 zip 失败", res.errMsg);
},
});
function unzip(zipFilePath) {
const targetPath = `ttfile://user/abc`;
fileSystemManager.unzip({
zipFilePath,
targetPath,
success(_res) {
console.log("解压成功");
const concent = fs.readdirSync(targetPath);
// 输出 zip 内文件内容
console.log("压缩包内文件:", concent);
},
fail(res) {
console.log("解压失败", res.errMsg);
},
});
}
该文档是否有帮助?
点击纠错