鸿蒙笔记2:将定制文件夹打包到hap并查看安装后的相关信息
1、需求简介
有这样一个需求:lazarus应用程序需要对 data 和 sys 文件夹中的子文件夹及文件进行读写、创建。这在windows平台和linux平台都很容易实现。现在需要在鸿蒙平台实现这些功能。
把2个文件夹放在鸿蒙项目文件夹:
entry\src\main\resources\rawfile
由于lazarus应用对rawfile文件夹没有读写权限,改为将2个文件夹放在
entry\src\main\resources\resfile
hap安装后,resfile 资源会被解压到应用沙箱路径中。
现在需要验证 resfile 下的 data 文件夹是否已正确打包并部署到模拟器,可以通过 hdc 命令在 DevEco Studio 终端中查看
2、 命令行方式查看应用沙箱下 el2/base 目录的文件列表
(包名: com.example.hello20260714b)
hdc shell -b com.example.hello20260714b ls -A "./data/storage/el2/base/"
-b {bundleName} 参数表示以指定应用的身份执行命令,这样可以正确访问应用沙箱目录。
运行结果:
.backup
cache
files
haps
preferences
3、查看 haps 目录
hdc shell -b com.example.hello20260714b ls -A "./data/storage/el2/base/haps/"
运行结果:
entry
4、在 click 事件中获取并显示resfile对应的路径代码
修改 index.ets 代码,在日志信息中查看resfile对应的资源路径(resourceDir)
修改 Index.ets,在 click 事件中添加获取并显示路径代码。
import { common } from '@kit.AbilityKit';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
RelativeContainer() {
Text(this.message)
.id('HelloWorld')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(() => {
this.message = 'Welcome';
// 获取 resourceDir 路径并打印
let context = getContext(this) as common.UIAbilityContext;
console.info(`resourceDir 路径: ${context.resourceDir}`);
})
}
.height('100%')
.width('100%')
}
}
点击 hello world,hilog中显示:
07-15 14:09:32.299 3855-3855 A03d00/JSAPP com.examp...0260714b I resourceDir 路径: /data/storage/el1/bundle/entry/resources/resfile
此时仍然不能确认 data 、sys 文件夹是否被正确放在沙箱的 resfile 文件夹中。
5、查看虚拟机中资源路径( resourceDir)包含的内容
修改 index.ets 代码
import { common } from '@kit.AbilityKit';
import { fileIo } from '@kit.CoreFileKit';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
@State fileList: string[] = [];
aboutToAppear(): void {
let context = getContext(this) as common.UIAbilityContext;
// 打印 resourceDir 路径
console.info(`resourceDir 路径: ${context.resourceDir}`);
try {
// 列出 resourceDir 目录下的所有文件和文件夹
let list = fileIo.listFileSync(context.resourceDir);
console.info(`resourceDir 根目录内容: ${JSON.stringify(list)}`);
// 尝试列出 data 子目录
let dataPath = `${context.resourceDir}/data`;
let dataList = fileIo.listFileSync(dataPath);
console.info(`resourceDir/data 目录内容: ${JSON.stringify(dataList)}`);
// 尝试列出 sys 子目录
let sysPath = `${context.resourceDir}/sys`;
let sysList = fileIo.listFileSync(sysPath);
console.info(`resourceDir/sys 目录内容: ${JSON.stringify(sysList)}`);
} catch (e) {
console.error(`列出目录失败: ${(e as Error).message}`);
}
}
build() {
RelativeContainer() {
Text(this.message)
.id('HelloWorld')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(() => {
this.message = 'Welcome';
// 获取 resourceDir 路径并打印
let context = getContext(this) as common.UIAbilityContext;
console.info(`resourceDir 路径: ${context.resourceDir}`);
})
}
.height('100%')
.width('100%')
}
}
运行后,HiLog 过滤 resourceDir 关键词显示内容:
resourceDir 路径: /data/storage/el1/bundle/entry/resources/resfile
resourceDir 根目录内容: ["sys","data"]
resourceDir/data 目录内容: ["370000000000000000","370000000000000002"]
resourceDir/sys 目录内容: ["tpl","reg_help.mht","setup.ini"]
说明2个定制文件夹打包并安装成功了。

备注:resfile 目录中的资源只能以只读方式访问1,如果需要读写操作,需要先将文件复制到应用的 filesDir 沙箱目录中
更多推荐



所有评论(0)