65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
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
|
||
})
|
||
}
|
||
}
|
||
|
||
} |