使用@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
2024-05-11 15:14:05
浏览
收藏 0
回答 1
待解决
回答 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);

分享
微博
QQ
微信
回复
2024-05-12 22:07:42
相关问题
HarmonyOS ZIP文件加密压缩和解压缩
791浏览 • 1回复 待解决
HarmonyOS gzip压缩和解压缩接口
581浏览 • 1回复 待解决
HarmonyOS gzip二进制流压缩和解压缩
796浏览 • 1回复 待解决
实现文件解压缩和数据流解压缩
1475浏览 • 1回复 待解决
鸿蒙里如何通过代码解压zip文件?
143浏览 • 0回复 待解决
HarmonyOS如何解压rawfile中的zip文件
394浏览 • 1回复 待解决
如何解压有密码的压缩
1769浏览 • 0回复 待解决
zip包的解压是在主线程还是IO线程
1668浏览 • 1回复 待解决
将rawfile中zip复制并解压到沙箱路径中
1445浏览 • 1回复 待解决
zlib.decompressFile()解压报错如何分析
1953浏览 • 1回复 待解决
是否有将文件夹压缩打包成zip的API?
911浏览 • 1回复 待解决
@ohos/smartrefreshlayout运行报错
375浏览 • 1回复 待解决