HarmonyOS 使用zip压缩文件怎么进行zip加密?

有一些定位日志需要打包成zip分享出去,因为日志里面有关键信息需要进行zip加密,当使用import { zlib, BusinessError } from ‘@kit.BasicServicesKit’; 压缩文件时无法进行加密,需要怎么做才可以在压缩文件时进行加密,设置密码。

阅读 517
1 个回答

可以使用三方库jszip实现,参考:https://gitee.com/openharmony-tpc/openharmony\_tpc\_samples/tree/master/ohos-jszip

示例参考:

build() {
  Scroll() {
    Column() {

      Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap }) {
        Row({ space: 8 }) {
          this.BuildButton("+新建文件夹", (): void => this.createDir())
          this.BuildButton("+新建文件", (): void => this.createFile(), {
            bgColor: Color.White,
            color: Color.Black,
            borderOptions: { width: 2, color: Color.Black }
          })
        }.margin({ bottom: 8 })

        this.BuildButton("加载压缩文件", (): void => this.loadAsyncFile(), { bgColor: Color.Green })
        this.BuildButton("生成压缩文件", (): void => this.generateZIP(), { bgColor: Color.Orange })
        // this.BuildButton("查看文件信息",this.debugger,{bgColor:Color.Gray});
        this.BuildButton("遍历(目录及文件)", (): void => this.forEachZIP())
        this.BuildButton("过滤(过滤文件)", (): void => this.filterZIP())
      }

      Row() {
        Text("设置密码:")
        TextInput({ placeholder: '请输入密码...', text: this.password || "" })
          .height(40)
          .width('50%')
          .onChange((value: string) => {
            this.password = value
          })
          .margin({ bottom: 12 })
      }
    }
  }
}