FileSystemManager.readZipEntry
基础库 2.90.0 开始支持本方法,这是一个异步方法。
读取压缩包内的文件。
| 前提条件 | 无 |
| 业务背景 | 无 |
| 使用限制 | 无 |
| 注意事项 | 无 |
| 相关教程 | 无 |
语法
FileSystemManager.readZipEntry(options)
参数说明
options 为 object 类型,属性如下:
| 属性名 | 类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
|---|---|---|---|---|---|
| encoding | enum | 否 | 统一指定读取文件的字符编码,只在 entries 值为"all"时有效。如果 entries 值为"all"且不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容 | 2.90.0 | |
| filePath | string | 是 | 要读取的压缩包的路径 (本地路径) | 2.90.0 | |
| entries | Array<object> | 是 | 要读取的压缩包内的文件列表(当传入"all" 时表示读取压缩包内所有文件) | 2.90.0 | |
| success | function | 否 | 接口调用成功的回调函数 | 2.90.0 | |
| fail | function | 否 | 接口调用失败的回调函数 | 2.90.0 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 2.90.0 |
entries 参数说明
Array<object> | 'all' 类型,传入 object 时属性如下:
属性名 | 类型 | 默认值 | 必填 | 说明 |
path | string | 是 | 压缩包内文件路径,entries中对象之间的path不可重复 | |
encoding | string | 否 | 指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容 |
encoding 的合法值
| 值 | 说明 | 最低支持版本 |
|---|---|---|
| ascii | - | 2.90.0 |
| base64 | - | 2.90.0 |
| binary | - | 2.90.0 |
| hex | - | 2.90.0 |
| ucs2 | 以小端序读取 | 2.90.0 |
| ucs-2 | 以小端序读取 | 2.90.0 |
| utf16le | 以小端序读取 | 2.90.0 |
| utf-16le | 以小端序读取 | 2.90.0 |
| utf-8 | - | 2.90.0 |
| utf8 | - | 2.90.0 |
| latin1 | - | 2.90.0 |
回调成功
object 类型,属性如下:
| 属性名 | 类型 | 说明 | 最低支持版本 |
|---|---|---|---|
| entries | object | 文件读取结果。entries 是一个对象,key是文件路径,value是一个对象 FileItem ,表示该文件的读取结果。每个 FileItem 包含 data (文件内容) 和 errMsg (错误信息) 属性。 | 2.90.0 |
| errMsg | string | "FileSystemManager.readZipEntry:ok" | 2.90.0 |
entries 参数说明
object 类型,属性如下:
属性名 | 类型 | 说明 | 最低支持版本 |
path |
| 文件路径 | 2.90.0 |
回调失败
object 类型,属性如下:
| 属性名 | 类型 | 说明 | 最低支持版本 |
|---|---|---|---|
| errMsg | string | "FileSystemManager.readZipEntry:fail" + 详细错误信息 | 2.90.0 |
错误码
| errNo | errMsg | 说明 | 最低支持版本 |
|---|---|---|---|
| 20001 | invalid param | 参数错误 | 2.90.0 |
| 10401 | error stack | 小游戏框架内部错误,有需要请创建工单咨询 | 2.90.0 |
| 21101 | permission denied, $apiName $dirPath | 2.90.0 | |
| 21102 | no such file or directory, $apiName $dirPath | 2.90.0 | |
| 21103 | operation not permitted, $apiName $filePath | 2.90.0 | |
| 21106 | unzip open file fail | 小游戏框架内部错误,有需要请创建工单咨询 | 2.90.0 |
| 20001 | option.entries.path should not be repeated | entries中对象之间的path不可重复 | 2.90.0 |
| 21103 | no such file or directory, $apiName $filePath | 2.90.0 | |
| 21102 | operation not permitted, $apiName $filePath | 2.90.0 | |
| 20000 | internal error | 小游戏框架内部错误,有需要请创建工单咨询 | 2.90.0 |
扫码体验
请使用字节宿主APP扫码
代码示例
const fs = tt.getFileSystemManager(); // 读取压缩包内所有文件 fs.readZipEntry({ filePath: "zipPath", entries: "all", encoding: "utf8", success(res) { console.log("读取压缩包内所有文件成功", res); }, fail(res) { console.log("读取压缩包内所有文件失败", res); }, }); // 读取压缩包内指定文件 fs.readZipEntry({ filePath: "zipPath", entries: [ { path: "zipEntryPath1", }, { path: "zipEntryPath2", encoding: "utf8", }, ], success(res) { console.log("读取指定压缩文件成功", res); }, fail(res) { console.log("读取指定压缩文件失败", res); }, }); const fs = tt.getFileSystemManager(); // 读取压缩包内所有文件 fs.readZipEntry({ filePath: "zipPath", entries: "all", encoding: "utf8", success(res) { console.log("读取压缩包内所有文件成功", res); }, fail(res) { console.log("读取压缩包内所有文件失败", res); }, }); // 读取压缩包内指定文件 fs.readZipEntry({ filePath: "zipPath", entries: [ { path: "zipEntryPath1", }, { path: "zipEntryPath2", encoding: "utf8", }, ], success(res) { console.log("读取指定压缩文件成功", res); }, fail(res) { console.log("读取指定压缩文件失败", res); }, });
