【OpenHarmony】无损压缩算法:brotli

0 阅读2分钟

简介

Brotli 是一种通用无损压缩算法

效果展示

下载安装

ohpm install brotli-js 
ohpm install @types/brotli --save-dev //import brotli 的时候语法报错。其原因是brotli包内不含类型声明,需要 @types/brotli 下载这个包的声明文件,从而解决语法的报错。

使用说明

注意本库暂不支持中文字符的压缩/解压缩

  import brotli from 'brotli-js';

@Entry
@Component
struct Index {
  @State mgs_compressed:string = "空"
  @State mgs_decompressed:string = "空"

private stringToBytes(str): Int8Array  {
  let out = new Int8Array(str.length);
  for (let i = 0; i < str.length; ++i) out[i] = str.charCodeAt(i);
  return out;
}

 private bytesToString(bytes): string {
   return String.fromCharCode.apply(null, new Uint16Array(bytes));
 }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) {
      Text('Brotli').fontSize(15).fontColor(Color.Black)
      Text('Brotli is a generic-purpose lossless compression algorithm that compresses data using a combination of a modern variant of the LZ77 algorithm, Huffman coding and 2nd order context modeling, with a compression ratio comparable to the best currently available general-purpose compression methods. It is similar in speed with deflate but offers more dense compression..')
        .textOverflow({ overflow: TextOverflow.None })
        .fontSize(12).border({ width: 1 }).padding(10)
      Text('压缩')
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          const str = 'test txt'
          const buf = new ArrayBuffer(str.length)
          const bufView = new Uint8Array(buf)

          for (let i = 0, strLen = str.length; i < strLen; i++) {
            bufView[i] = str.charCodeAt(i)
          }

          const compressed = brotli.compressArray(bufView, 6)
          const decompressed = brotli.decompressArray(compressed)
          const restoredStr = String.fromCharCode.apply(null, decompressed)
          this.mgs_compressed = compressed
          this.mgs_decompressed = restoredStr
        })
      Text('编码结果:'+this.mgs_compressed)
        .textOverflow({ overflow: TextOverflow.None })
        .fontSize(12).border({ width: 1 }).padding(10)
      Text('解码结果:'+this.mgs_decompressed)
        .textOverflow({ overflow: TextOverflow.None })
        .fontSize(12).border({ width: 1 }).padding(10)
    }
    .width('100%')
    .height('100%')
  }
}

DD一下:欢迎大家关注工粽号<程序猿百晓生>,可以了解到以下知识点。

`欢迎大家关注工粽号<程序猿百晓生>,可以了解到以下知识点。`
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案) 
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......

接口说明

  1. 压缩接口
  export function compressArray(uint: Uint8Array, level?: number)

2. 解压接口

  export function decompressArray(compressed)

目录结构

│  config.json #应用程序配置文件
├─entry 
│	└─src
│		└─main
│        	├─ets
│          		└─MainAbility
│              		└─app.ets #应用程序入口  
│              		└─pages
│                      └─index.ets #调用实例
│              
│           ├─resources
│               └─base
│                   ├─element
│                   │    └─string.json #默认string文件
│                   └─media
│                        └─icon.png #默认程序图标