# FileSystemManager.readZipEntry(Object object)

以 Promise 风格调用:不支持

# 功能描述

读取压缩包内的文件

# 参数

# Object object

属性类型默认值必填说明
filePathstring要读取的压缩包的路径 (本地路径)
encodingstring统一指定读取文件的字符编码,只在 entries 值为"all"时有效。如果 entries 值为"all"且不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
合法值说明
ascii
base64
binary
hex
ucs2以小端序读取
ucs-2以小端序读取
utf16le以小端序读取
utf-16le以小端序读取
utf-8
utf8
latin1
entriesArray.<Object>/'all'要读取的压缩包内的文件列表(当传入"all" 时表示读取压缩包内所有文件)
结构属性类型默认值必填说明
pathstring压缩包内文件路径
encodingstring指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
合法值说明
ascii
base64
binary
hex
ucs2以小端序读取
ucs-2以小端序读取
utf16le以小端序读取
utf-16le以小端序读取
utf-8
utf8
latin1
positionnumber从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte
lengthnumber指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

# object.success 回调函数

# 参数
# Object res
属性类型说明
entriesObject文件读取结果。res.entries 是一个对象,key是文件路径,value是一个对象 FileItem ,表示该文件的读取结果。每个 FileItem 包含 data (文件内容) 和 errMsg (错误信息) 属性。
结构属性类型说明
pathObject文件路径
结构属性类型说明
datastring/ArrayBuffer文件内容
errMsgstring错误信息

# 错误

错误码错误信息说明
1300001operation not permitted操作不被允许(例如,filePath 预期传入一个文件而实际传入一个目录)
1300002no such file or directory ${path}文件/目录不存在,或者目标文件路径的上层目录不存在
1300005Input/output error输入输出流不可用
1300009bad file descriptor无效的文件描述符
1300013permission denied权限错误,文件是只读或只写
1300014Path permission denied传入的路径没有权限
1300020not a directorydirPath 指定路径不是目录,常见于指定的写入路径的上级路径为一个文件的情况
1300021Is a directory指定路径是一个目录
1300022Invalid argument无效参数,可以检查length或offset是否越界
1300036File name too long文件名过长
1300066directory not empty目录不为空
1300201system error系统接口调用失败
1300202the maximum size of the file storage limit is exceeded存储空间不足,或文件大小超出上限(上限100M)
1300203base64 encode error字符编码转换失败(例如 base64 格式错误)
1300300sdcard not mountedandroid sdcard 挂载失败
1300301unable to open as fileType无法以fileType打开文件
1301000permission denied, cannot access file path目标路径无访问权限(usr目录)
1301002data to write is empty写入数据为空
1301003illegal operation on a directory不可对目录进行此操作(例如,指定的 filePath 是一个已经存在的目录)
1301004illegal operation on a package directory不可对代码包目录进行此操作
1301005file already exists ${dirPath}已有同名文件或目录
1301006value of length is out of range传入的 length 不合法
1301007value of offset is out of range传入的 offset 不合法
1301009value of position is out of rangeposition值越界
1301100store directory is emptystore目录为空
1301102unzip open file fail压缩文件打开失败
1301103unzip entry fail解压单个文件失败
1301104unzip fail解压失败
1301111brotli decompress failbrotli解压失败(例如,指定的 compressionAlgorithm 与文件实际压缩格式不符)
1301112tempFilePath file not exist指定的 tempFilePath 找不到文件
1302001fail permission denied指定的 fd 路径没有读权限/没有写权限
1302002excced max concurrent fd limitfd数量已达上限
1302003invalid flag无效的flag
1302004permission denied when open using flag无法使用flag标志打开文件
1302005array buffer does not exist未传入arrayBuffer
1302100array buffer is readonlyarrayBuffer只读

# 示例代码

const fs = tap.getFileSystemManager()
// 读取zip内某个或多个文件
fs.readZipEntry({
  filePath: 'tap.env.USER_DATA_PATH' + 'from/to.zip',
  entries: [{
    path: 'some_folder/my_file.txt', // zip内文件路径
    encoding: 'utf-8', // 指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
    position: 0, // 从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte
    length: 10000, // 指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte
  }, {
    path: 'other_folder/orther_file.txt', // zip内文件路径
  }],
  success(res) {
    console.log(res.entries)
    // res.entries === {
    //     'some_folder/my_file.txt': {
    //         errMsg: 'readZipEntry:ok',
    //         data: 'xxxxxx'
    //     },
    //     'other_folder/orther_file.txt': {
    //         data: (ArrayBuffer)
    //     }
    // }
  },
  fail(res) {
    console.log(res.errMsg)
  },
})

// 读取zip内所有文件。允许指定统一的encoding。position、length则不再允许指定,分别默认为0和文件长度
fs.readZipEntry({
  filePath: 'tap.env.USER_DATA_PATH' + 'from/to.zip',
  entries: 'all'
  encoding: 'utf-8', // 统一指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
  success(res) {
    console.log(res.entries)
    // res.entries === {
    //     'some_folder/my_file.txt': {
    //         errMsg: 'readZipEntry:ok',
    //         data: 'xxxxxx'
    //     },
    //     'other_folder/orther_file.txt': {
    //         errMsg: 'readZipEntry:ok',
    //         data: 'xxxxxx'
    //     }
    //  }
  },
  fail(res) {
    console.log(res.errMsg)
  },
})