HarmonyOS如何高效拷贝rawfile下面的文件到files目录下

let fd = context.resourceManager.getRawFDSync("a.txt").fd;  
fs.copyFileSync(fd, context.filesDir+"/a.txt")
  • 1.
  • 2.

结果不是拷贝a.txt,而是拷贝了从a开始的整个har包。

HarmonyOS
2024-09-10 10:05:13
648浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

resourcemanager拿到的fd不支持copyfile(所有rawfile被压缩在一块), 得用fs.read接口指定offset和length去读出来,然后再从buffer把东西写进最终文件。参考demo如下:

import fs from '@ohos.file.fs'; 
@Entry 
@Component 
struct TestError { 
  @State message: string = 'Hello World' 
  @State rawfilePath:string = "config.txt"; 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
 
        Button(`将文件${this.rawfilePath}复制`) 
          .fontSize(16) 
          .fontWeight(FontWeight.Bold) 
          .margin(5) 
          .onClick(() => { 
            getContext(this).resourceManager.getRawFd(this.rawfilePath).then(file => { 
              this.copy2file(file) 
            }).catch((err) => { 
              console.log(JSON.stringify(err)); 
            }) 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
  async copy2file(item) { 
    try { 
      let dstPath = getContext(this).filesDir+'/test.txt' 
      this.saveFileToCache(item, dstPath) 
    } catch(e) { 
      console.log(`RawFile#copy2file() error : ${JSON.stringify(e)}`) 
    } 
  } 
  saveFileToCache(file, path) { 
    // 创建缓存文件(当前是覆盖式创建) 
    let cacheFile = fs.openSync( 
      path, 
      fs.OpenMode.WRITE_ONLY | fs.OpenMode.CREATE | fs.OpenMode.TRUNC) 
    let buffer = new ArrayBuffer(4096); 
    let currentOffset = file.offset; 
    let lengthNeedToReed = file.length; 
    let readOption = { 
      offset: currentOffset, 
      length: lengthNeedToReed > buffer.byteLength ? 4096 : lengthNeedToReed 
    } 
    while(true) { 
      // 读取buffer容量的内容 
      let readLength = fs.readSync(file.fd, buffer, readOption); 
      // 写入buffer容量的内容 
      fs.writeSync(cacheFile.fd, buffer, {length:readLength}) 
      // 判断后续内容 修改读文件的参数 
      if (readLength < 4096) { 
        break; 
      } 
      lengthNeedToReed -= readLength; 
      readOption.offset += readLength; 
      readOption.length = lengthNeedToReed > buffer.byteLength ? 4096 : lengthNeedToReed; 
    } 
    fs.close(cacheFile); 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
分享
微博
QQ
微信
回复
2024-09-10 16:16:02


相关问题
HarmonyOS rawfile文件拷贝沙箱
1156浏览 • 1回复 待解决
HarmonyOS 应用安装后在哪个目录下面
1882浏览 • 1回复 待解决
资源目录下文件沙箱的单向流动
1992浏览 • 1回复 待解决
HarmonyOS RawFile下的文件拷贝
700浏览 • 1回复 待解决
鸿蒙如何读取resources目录下文件
4500浏览 • 1回复 待解决