
回复
前言:
jszip仓库链接:OpenHarmony三方库中心仓
本地下载
ohpm install @ohos/jszip
或者项目集成:
在oh-package.json5配置文件中加入依赖"@ohos/jszip": "1.0.0"
接口列表
接口 | 参数 | 功能 |
file | (path,data[, options]) | 创建文件 |
folder | (name) | 创建文件夹 |
forEach | (callback:(relativePath,file) => void) | 遍历目录及文件 |
filter | (predicate:(relativePath,file) => boolean) | 支持过滤目录/文件 |
remove | (path) | 移除目录或者文件 |
generateAsync | (options,onUpdate) | 异步生成压缩文件,支持设置密码 |
loadAsync | (data,options) | 异步加载压缩文件 |
使用:
// 参数校验
if (!this.filePath || this.filePath.length < 1) {
promptAction.showToast({
message: `暂无可用文件,请先加密压缩文件`
})
return;
}
if (!this.password || this.password.length < 1) {
promptAction.showToast({
message: `请先输入解压缩密码`
})
return;
}
try {
// 检查是否有权限处理文件
let isAccess: boolean = fs.accessSync(this.filePath)
if (!isAccess) {
promptAction.showToast({
message: `暂无权限处理文件 ${this.filePath}`
})
return
}
// 将应用沙箱内的本地文件读取出来,转化为 Uint8Array 注意文件不能太大 否则造成OOM
let totalSize = fs.statSync(this.filePath).size;
let fileId = fs.openSync(this.filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
let cacheData: Uint8Array = new Uint8Array(totalSize);
let readLength = 0;
let buff = new ArrayBuffer(8 * 1024)
let readSize = 0;
while (readLength < totalSize) {
if (readLength + buff.byteLength < totalSize) {
readSize = fs.readSync(fileId.fd, buff, {
offset: readLength,
length: buff.byteLength
})
cacheData.set(new Uint8Array(buff), readLength)
} else {
let trueLength = totalSize - readLength
readSize = fs.readSync(fileId.fd, buff, {
offset: readLength,
length: trueLength
})
let trueBuff = buff.slice(0, trueLength)
cacheData.set(new Uint8Array(trueBuff), readLength)
}
readLength += readSize;
}
// 添加密码解压缩文件
JSZip.loadAsync(cacheData, {
password: this.password
}).then((data: JSZip) => {
promptAction.showToast({
message: `解压缩解密文件成功 ${JSON.stringify(data)}`
})
}).catch((err: Error) => {
promptAction.showToast({
message: `解压缩加密文件失败! 错误原因: ${err.message}`
})
})
} catch (err) {
promptAction.showToast({
message: `解压缩加密文件出错! 错误原因: ${err.message}`
})
}
以下是封装的获取压缩文件列表和获取单个文件的方法:
// 参数校验
if (!this.filePath || this.filePath.length < 1) {
promptAction.showToast({
message: `暂无可用文件,请先加密压缩文件`
})
return;
}
if (!this.password || this.password.length < 1) {
promptAction.showToast({
message: `请先输入解压缩密码`
})
return;
}
try {
// 检查是否有权限处理文件
let isAccess: boolean = fs.accessSync(this.filePath)
if (!isAccess) {
promptAction.showToast({
message: `暂无权限处理文件 ${this.filePath}`
})
return
}
// 将应用沙箱内的本地文件读取出来,转化为 Uint8Array 注意文件不能太大 否则造成OOM
let totalSize = fs.statSync(this.filePath).size;
let fileId = fs.openSync(this.filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
let cacheData: Uint8Array = new Uint8Array(totalSize);
let readLength = 0;
let buff = new ArrayBuffer(8 * 1024)
let readSize = 0;
while (readLength < totalSize) {
if (readLength + buff.byteLength < totalSize) {
readSize = fs.readSync(fileId.fd, buff, {
offset: readLength,
length: buff.byteLength
})
cacheData.set(new Uint8Array(buff), readLength)
} else {
let trueLength = totalSize - readLength
readSize = fs.readSync(fileId.fd, buff, {
offset: readLength,
length: trueLength
})
let trueBuff = buff.slice(0, trueLength)
cacheData.set(new Uint8Array(trueBuff), readLength)
}
readLength += readSize;
}
// 添加密码解压缩文件
JSZip.loadAsync(cacheData, {
password: this.password
}).then((data: JSZip) => {
promptAction.showToast({
message: `解压缩解密文件成功 ${JSON.stringify(data)}`
})
}).catch((err: Error) => {
promptAction.showToast({
message: `解压缩加密文件失败! 错误原因: ${err.message}`
})
})
} catch (err) {
promptAction.showToast({
message: `解压缩加密文件出错! 错误原因: ${err.message}`
})
}
通过上述方法我们就可以实现