HarmonyOS zip组件无法解压rawfile里面的zip包?

如题:HarmonyOS zip组件无法解压rawfile里面的zip包?

阅读 434
1 个回答

1、resources文件夹下,需要使用resourceManager资源管理模块调用rawfile文件夹用getRawFdSync方法获取,getRawFd(“test\_file.zip”…)参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-resource-manager-V5\#getrawfd9

2、zlib.decompressFile(inFile, outFile, options, callback)inFile,和outFile都必须是沙箱路径。

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-zlib-V5\#zlibdecompressfile9

3、demo参考:

import fs, { ListFileOptions } 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("test_file.zip", (err, data) => {
              let sandboxPath = this.context.filesDir
              console.log("沙箱路径:" + sandboxPath)
              //fs.mkdtempSync(sandboxPath + "/cik")
              let filePath = this.context.tempDir + "/test_file.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("test_file.zip")
            })
          })
          .width('100%')

        Button('4、获取沙箱文件列表')
          .onClick(() => {

            // 获取应用文件路径
            let filesDir = getContext(this).filesDir;
            console.log('查看文件列表:' + filesDir)
            let listFileOption: ListFileOptions = {
              recursion: false,
              listNum: 0,
              filter: {
                suffix: [".png", ".docx", ".txt"],
                displayName: ["test*"],
                fileSizeOver: 0,
                lastModifiedAfter: new Date(0).getTime()
              }
            };
            let files = fs.listFileSync(filesDir, listFileOption);
            for (let i = 0; i < files.length; i++) {
              console.info(`The name of file: ${files[i]}`);
            }
          })
      }
      .height('100%')
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏