FileSystemManager.unzip
基础库 1.15.0 开始支持本方法,低版本需做兼容处理。
解压文件。
语法
FileSystemManager.unzip(options)
参数说明
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 |
回调成功
属性 | 类型 | 说明 | 最低支持版本 |
---|---|---|---|
errMsg | string | 1.15.0 |
回调失败
属性 | 类型 | 说明 | 最低支持版本 |
---|---|---|---|
errMsg | string | "unzip:fail " + 详细错误信息 | 1.15.0 |
扫码体验
请使用字节宿主APP扫码
代码示例
const fileSystemManager = tt.getFileSystemManager();
// 下载一个 zip 文件
tt.downloadFile({
url: "https://sf3-ttcdn-tos.pstatp.com/obj/developer/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);
},
});
}
Bug & Tip
- Tip: 由于开发者只有在 用户目录(ttfile://user) 才有写的权限, 因此
targetPath
必须以ttfile://user
开头
点击纠错
评价此篇文档