将rawfile中zip复制并解压到沙箱路径中

将rawfile中zip复制并解压到沙箱路径中

HarmonyOS
2024-05-21 20:34:45
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
vclearner

在resource目录下存放的文件,无法使用文件管理接口或以沙箱路径形式处理,如果这些文件比较多的话,就需要将它们压缩一下,一次转存到本地沙箱中进行解压,实现resource内文件的能力补全。

使用的核心API

ResourceManager.getRawFd(path: string): Promise 
fs.mkdtempSync(prefix: string): string 
fs.openSync(path: string, mode?: number): File 
fs.writeSync(fd: number, buffer: ArrayBuffer | string, options?: WriteOptions): ResourceManager 
decompressFile(inFile: string, outFile: string, options?: Options): Promise

核心代码解释

先从resource目录中将文件数据读取到内存,通过buffer将rawfile文件内容copy到沙箱路径,再对沙箱路径下的压缩文件进行解压。

核心代码如下:

import fs from '@ohos.file.fs'; 
import { Context } from '@ohos.abilityAccessCtrl'; 
import zlib from '@ohos.zlib'; 
 
@Entry 
@Component 
struct Index { 
  @State context: Context = getContext(this); 
 
  build() { 
    Row() { 
      Column() { 
        Button('复制zip到沙箱,并解压zip', { type: ButtonType.Normal, stateEffect: true }) 
          .borderRadius(8) 
          .backgroundColor(0x317aff) 
          .width(90) 
          .height(40) 
          .onClick(() => { 
            /** 
             * 通过fd来进行拷贝,避免文件过大的内存占用问题 
             * data.fd是hap包的fd,data.offset表示目标文件在hap包中的偏移,data.length表示目标文件的长度 
             */ 
            this.context.resourceManager.getRawFd("picture.zip", (err, data) => { 
              let sandboxPath = this.context.filesDir; 
              console.log("沙箱路径:" + sandboxPath); 
              fs.mkdtempSync(sandboxPath + "/XXXXXX"); 
              let filePath = this.context.tempDir + "/bfpicture.zip"; 
              console.log("压缩文件路径:" + filePath); 
              let dest = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 
              let bufsize = 4096; 
              let buf = new ArrayBuffer(bufsize); 
              let off = 0, len = 0, readedLen = 0; 
              /** 
               * 通过buffer将rawfile文件内容copy到沙箱路径 
               */ 
              while (len = fs.readSync(data.fd, buf, { offset: data.offset + off, length: bufsize })) { 
                readedLen += len; 
                fs.writeSync(dest.fd, buf, { offset: off, length: len }); 
                off = off + len; 
                if ((data.length - readedLen) < bufsize) { 
                  bufsize = data.length - readedLen; 
                } 
              } 
              fs.close(dest.fd); 
              // 对沙箱路径下的压缩文件进行解压 
              zlib.decompressFile(filePath, sandboxPath); 
              this.context.resourceManager.closeRawFd("bfpicture.zip"); 
            }) 
          }) 
          .width('100%') 
      } 
      .height('100%') 
    } 
  } 
}

实现效果

适配的版本信息

IDE:DevEco Studio 4.1.3.500

SDK:HarmoneyOS 4.0.10.16

分享
微博
QQ
微信
回复
2024-05-22 16:15:06
相关问题
在native侧创建file保存进沙箱路径
446浏览 • 1回复 待解决
如何docker容器的文件复制到主机?
1110浏览 • 1回复 待解决
Native获取Rawfile的内容打印
326浏览 • 1回复 待解决
沙箱路径的文件怎么拿取?
444浏览 • 1回复 待解决
怎样查看应用沙箱的文件?
470浏览 • 1回复 待解决
使用web组件实现预览沙箱pdf
504浏览 • 1回复 待解决
pthread创建的线程如何读取rawfile
617浏览 • 1回复 待解决
NAPI定义注册Class
272浏览 • 1回复 待解决
无法读取到hsp模块rawfile文件
590浏览 • 1回复 待解决