HarmonyOS 鸿蒙Next LZ4解压缩文本
HarmonyOS 鸿蒙Next LZ4解压缩文本
这里使用的commons-compress 三方库 进行的LZ4格式 压缩 解压 当前仅 commons-compress 三方库 支持此能力 但是这个库不能对string 进行解压 但是可以对压缩包进行解压
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { lz4Compressed, lz4Decompressed, System } from '[@ohos](/user/ohos)/commons-compress';
import fs from '[@ohos](/user/ohos).file.fs';
import { GlobalContext } from './GlobalContext';
import common from '[@ohos](/user/ohos).app.ability.common';
let context = getContext(this) as common.UIAbilityContext;
[@Entry](/user/Entry)
[@Component](/user/Component)
export struct Lz4Test {
TAG: string = 'lz4Test----'
[@State](/user/State) isCompressLz4Show: boolean = false;
[@State](/user/State) isDeCompressLz4Show: boolean = false;
preTimestamp: number = 0;
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text('lz4相关功能')
.fontSize(20)
.margin({ top: 16 })
Text('点击生成test.txt')
.fontSize(16)
.margin({ top: 32 })
.padding(8)
.border({ width: 2, color: '#535353', radius: 6 })
.onClick((event) => {
if (!this.isFastClick()) {
this.generateTextFile()
}
})
if (true) {
Text('点击压缩test.txt为lz4文件')
.fontSize(16)
.margin({ top: 32 })
.padding(8)
.border({ width: 2, color: '#535353', radius: 6 })
.onClick((event) => {
if (!this.isFastClick()) {
this.lz4CompressedTest()
}
})
}
if (true) {
Text('点击解压lz4文件')
.fontSize(16)
.margin({ top: 32 })
.padding(8)
.border({ width: 2, color: '#535353', radius: 6 })
.onClick((event) => {
if (!this.isFastClick()) {
this.lz4DecompressedTest()
}
})
}
}
.width('100%')
.height('100%')
}
isFastClick(): boolean {
let timestamp:number = Date.parse(new Date().toString());
if ((timestamp - this.preTimestamp) > 1500) {
this.preTimestamp = timestamp;
return false;
} else {
return true;
}
}
generateTextFile(): void {
try {
let data = getContext(this).filesDir
// let data = context.filesDir
const writer = fs.openSync(data + '/aaaaa.txt', fs.OpenMode.WRITE_ONLY | fs.OpenMode.CREATE);
fs.writeSync(writer.fd, "xxx"
);
fs.closeSync(writer);
AlertDialog.show({ title: '生成成功',
message: '请查看沙箱路径' + data + '/aaaaa.txt',
confirm: { value: 'OK', action: () => {
this.isCompressLz4Show = true
} }
})
} catch (error) {
console.error('File to obtain the file directory. Cause: ' + error.message);
}
}
lz4CompressedTest(): void {
try {
let data = getContext(this).filesDir
// let data = context.filesDir
let timer1 = System.currentTimeMillis().toString()
console.info(this.TAG + timer1)
lz4Compressed(data + '/aaaaa.txt', data + '/aaaaa.txt.lz4')
let timer2: string = System.currentTimeMillis().toString()
console.info(this.TAG + timer2)
AlertDialog.show({ title: '压缩成功',
message: '请查看沙箱路径 ' + data + '/aaaaa.txt.lz4',
confirm: { value: 'OK', action: () => {
this.isDeCompressLz4Show = true
} }
})
} catch (error) {
console.error('File to obtain the file directory. Cause: ' + error.message);
}
}
lz4DecompressedTest(): void {
try {
let data = getContext(this).filesDir
// let data = context.filesDir
let timer1 = System.currentTimeMillis().toString()
console.info(this.TAG + timer1)
lz4Decompressed(data + '/aaaaa.txt.lz4', data + '/aaaaabv.txt')
let timer2: string = System.currentTimeMillis().toString()
console.info(this.TAG + timer2)
AlertDialog.show({ title: '解缩成功',
message: '请查看沙箱路径 ' + data + '/aaaaabv.txt',
confirm: { value: 'OK', action: () => {
} }
})
} catch (error) {
console.error('File to obtain the file directory. Cause: ' + error.message);
}
}
}
当前LZ4的压缩解压 仅commons-compress 三方库支持 仅对文件解压缩 不支持对字符串进行解压缩 目前鸿蒙自带的解压缩 不支持LZ4格式
更多关于HarmonyOS 鸿蒙Next LZ4解压缩文本的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS(鸿蒙)系统中,LZ4解压缩文本通常涉及对特定格式压缩数据的处理。LZ4是一种压缩算法,常用于减少数据大小以提高存储和传输效率。以下是关于在鸿蒙系统中进行LZ4解压缩文本的基本步骤:
-
获取LZ4库:首先,确保你有适用于鸿蒙系统的LZ4解压缩库。这可以通过鸿蒙系统的官方仓库或相关第三方库获取。
-
加载压缩数据:将LZ4压缩的文本数据加载到内存中,以便进行解压缩操作。
-
初始化解压缩器:使用LZ4库提供的API,初始化一个解压缩器实例。
-
执行解压缩:调用LZ4库中的解压缩函数,传入压缩数据和目标缓冲区,以及必要的参数(如压缩数据大小和解压缩后的预期大小)。
-
处理解压缩结果:检查解压缩函数的返回值,确保解压缩操作成功。然后,你可以访问目标缓冲区以获取解压缩后的文本数据。
请注意,具体的API调用和参数设置可能因LZ4库的不同版本和鸿蒙系统的具体要求而有所差异。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html