使用@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
按赞同
/
按时间
zdl2000

请修改下路径获取方式。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
相关问题
实现文件解压缩和数据流解压缩
390浏览 • 1回复 待解决
如何解压有密码的压缩
440浏览 • 0回复 待解决
zip包的解压是在主线程还是IO线程
464浏览 • 1回复 待解决
是否有将文件夹压缩打包成zip的API?
281浏览 • 1回复 待解决
zlib.decompressFile()解压报错如何分析
364浏览 • 1回复 待解决
网络请求使用gzip压缩数据
374浏览 • 1回复 待解决
OpenHarmony-v4.0-Beta1 编译报错,求助
3034浏览 • 3回复 待解决
hi3860v100语言模块代码报错
250浏览 • 0回复 待解决
import asset from '@ohos.security.asset'报错
402浏览 • 1回复 待解决
使用三方库pako压缩字节数组
391浏览 • 1回复 待解决
@ohos.inputMethod无法使用怎么回事?
1026浏览 • 1回复 待解决