2024-08-08 20:12:19 +08:00

65 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import FileUtil from '../../../common/utils/File'
import zlib from '@ohos.zlib';
import promptAction from '@ohos.promptAction';
import router from '@ohos.router';
export default class FileModel{
//后续文件路径待替换
private fileUtil: FileUtil
public folderPath: string
constructor(context){
(async ()=>{
const fileUtil = new FileUtil(context)
this.fileUtil = fileUtil
})()
}
// 设置文件夹
public initFolder = async () => {
const {fileUtil} = this
await fileUtil.initFolder(`/models/model_enc`);
const folderPath = await fileUtil.initFolder(`/models`);
this.folderPath = folderPath;
}
// 存储zip文件并解压
public storingFiles = async (str) => {
const {fileUtil,folderPath} = this;
await fileUtil.editFile(`${folderPath}/model.zip`,str,'overWrite')
let options = {
level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
};
zlib.unzipFile(
`${folderPath}/model.zip`,
`${folderPath}`,
options).then((data) => {
console.log("unzipFile result" + data);
}).catch((err)=>{
console.log("catch((err)=>" + err);
})
}
//获取文件内容
public getModelContent = (folderPath,fileName) => {
const {fileUtil} = this;
try {
const content = fileUtil.getFileContent(`${folderPath}/${fileName}`)
return content;
}catch (e){
console.info('surenjun',JSON.stringify(e))
promptAction.showToast({
message:`请检查模型路径${folderPath}/${fileName}是否正确!`,
duration:4000
})
}
}
}