使用@ohos/commons-compress (V2.0.1)zip压缩和解压报错
代码:Zip.ets
import zlib from ‘@ohos.zlib’; 
 
export default class Zip { 
  public static jsZipTest(): void { 
    try { 
      let inFile = ‘/data/storage/el1/bundle/hello.txt’; 
      let outFile = ‘/data/storage/el1/bundle/hello.zip’; 
      let options: zlib.Options = { 
        level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 
        memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 
        strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 
      }; 
 
      zlib.compressFile(inFile, outFile, options, (errData) => { 
        if (errData !== null) { 
          console.log(errData is errCode:${errData.code} message:${errData.message}); 
          AlertDialog.show({ 
            title: 压缩失败:${errData.message}, 
            message: errData.message, 
          confirm: { 
            value: ‘OK’, action: () => { 
            } 
          } 
        }) 
      } else { 
        console.log(“压缩成功”) 
        AlertDialog.show({ 
          title: ‘压缩成功’, 
          message: '请查看沙箱路径 ’ + ‘/’, 
          confirm: { 
            value: ‘OK’, action: () => { 
            } 
          } 
        }) 
      } 
    }) 
  } catch (error) { 
    console.error('File to obtain the file directory. Cause: ’ + error.message); 
  } 
} 
}Index.ets:
import hilog from ‘@ohos.hilog’; 
import testNapi from ‘libentry.so’; 
import Zip from ‘…/entryability/Zip’ 
import Unzip from ‘…/entryability/Unzip’ 
@Entry 
@Component 
struct Index { 
  @State message: string = ‘Hello World’; 
 
  build() { 
    Column(){ 
      Row() { 
        Column() { 
          Text(‘点击压缩’) 
          .fontSize(50) 
            .fontWeight(FontWeight.Bold) 
            .onClick(() => { 
              Zip.jsZipTest() 
            }) 
        } 
        .width(‘100%’) 
      } 
      .height(‘50%’) 
      .backgroundColor(’#ff289b25’) 
 
      Row() { 
        Column() { 
          Text(‘点击解压’+testNapi.add(1,1)) 
          .fontSize(50) 
            .fontWeight(FontWeight.Bold) 
            .onClick(() => { 
              Unzip.unJsZipTest() 
            }) 
        } 
        .width(‘100%’) 
      } 
      .height(‘50%’) 
      .backgroundColor(’#ff134eb5’) 
    }.width(‘100%’) 
    .height(‘100%’) 
  } 
}
        HarmonyOS
      
        赞
        
 收藏 0
 回答 1
 
        待解决
        
相关问题
 HarmonyOS  ZIP文件加密压缩和解压缩 
2239浏览  • 1回复 待解决
HarmonyOS  gzip压缩和解压缩接口 
1817浏览  • 1回复 待解决
HarmonyOS ohos.zlib (Zip模块)能否支持加密压缩,对压缩文件设置解压密码 
1285浏览  • 1回复 待解决
HarmonyOS gzip二进制流压缩和解压缩 
2195浏览  • 1回复 待解决
【7zip】使用7zip三方库,只能压缩,解压失败 
2446浏览  • 1回复 待解决
HarmonyOS zip文件解压缩有参考的demo吗? 
1343浏览  • 1回复 待解决
zlib是否支持解压带密码的压缩包(.zip)? 
3693浏览  • 2回复 待解决
HarmonyOS 是否有API支持解压加密的压缩包(zip) 
1476浏览  • 1回复 待解决
HarmonyOS 原生有没有自带的zip,压缩以及解压缩工具类 
1421浏览  • 1回复 待解决
HarmonyOS gz有解压方法吗?用@ohos.zlib只能解压zip文件 
1131浏览  • 1回复 待解决
@ohos.zlib (Zip模块)解压文件返回解压成功,但未没有找到解压后的文件 
3090浏览  • 1回复 待解决
HarmonyOS 在websocket收到消息zip压缩数据,怎么高性能的解压数据 
1181浏览  • 1回复 待解决
HarmonyOS zip压缩咨询 
1273浏览  • 1回复 待解决
HarmonyOS ZIP压缩问题 
1321浏览  • 1回复 待解决
HarmonyOS fluttertpc_video_compress报错 
872浏览  • 1回复 待解决
实现文件解压缩和数据流解压缩 
3396浏览  • 1回复 待解决
HarmonyOS zip组件无法解压rawfile里面的zip包 
1204浏览  • 1回复 待解决
如何解决使用zip模块解压文件,解压接口返回解压成功,进入设备查看解压路径未找到解压后的文件 
3204浏览  • 1回复 待解决
HarmonyOS  针对流形式文件(zip文件)AES解密,再zip解压 
1593浏览  • 1回复 待解决
HarmonyOS 代码如何解压zip文件 
1328浏览  • 1回复 待解决
RN集成HarmonyOS应用的方式,解压空工程压缩包 HarmonyEmptyProject.zip无法生成bundle包 
1484浏览  • 1回复 待解决
HarmonyOS 关于 Zip模块 从项目内解压zip文件的问题 
1300浏览  • 1回复 待解决
HarmonyOS 系统的ohos.zlib是否支持解压缩带密码功能 
1060浏览  • 1回复 待解决
HarmonyOS 使用pako包进行gzip压缩解压缩的时候,解压缩后的数据比原数据少了10个字符 - 
1399浏览  • 1回复 待解决





















请修改下路径获取方式。let inFile = ‘/data/storage/el1/bundle/hello.txt’;let outFile = ‘/data/storage/el1/bundle/hello.zip’;
这样是没有权限的而且import zlib from ‘@ohos.zlib’; 属于系统能力。
请参考下面的写法:
let data = GlobalContext.getContext().getObject(“FilesDir”);const writer = fileio.openSync(data + ‘/’ + this.newFolder + ‘/hello.txt’, 0o102, 0o666);及文档 https://gitee.com/openharmony-tpc/CommonsCompress/blob/master/entry/src/main/ets/component/zip/test.ets
详细看下gitee上的示例配置全局路径需要在EntryAbility文件配置全局路径GlobalContext.getContext().setObject(“context”, this.context);