1 个回答

请参考如下demo:

//解压rawfile下文件
function rawfileZlibDecompress() {
  getContext().resourceManager.getRawFileContent('demo.zip', (_err, value) => {
    let myBuffer: ArrayBufferLike = value.buffer
    //将rawfile下的文件拷贝至沙箱下,沙箱路径:/data/storage/el2/base/haps/entry/files/demo.zip
    let filePath = getContext().filesDir + '/demo.zip';
    let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
    let writeLen = fs.writeSync(file.fd, myBuffer);
    hilog.info(0xFF00, LOG_TAG, `write data to file succeed and size is:${writeLen}`);
    fs.closeSync(file);

    let outFileDir = getContext().filesDir;
    let options: zlib.Options = {
      level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION
    };
    //解压沙箱下的文件
    try {
      zlib.decompressFile(filePath, outFileDir, options, (errData: BusinessError) => {
        if (errData !== null) {
          hilog.error(0xFF00, LOG_TAG, `errData is errCode:${errData.code}  message:${errData.message}`);
        }
      })
    } catch (errData) {
      let code = (errData as BusinessError).code;
      let message = (errData as BusinessError).message;
      hilog.error(0xFF00, LOG_TAG, `errData is errCode:${code}  message:${message}`);
    }
  })
}
logo
HarmonyOS
子站问答
访问
宣传栏