1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the 'License'); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an 'AS IS' BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { fileIo } from '@kit.CoreFileKit'; 17import { hilog } from '@kit.PerformanceAnalysisKit'; 18import { promptAction, router } from '@kit.ArkUI'; 19import { zlib } from '@kit.BasicServicesKit'; 20 21const DOMAIN = 0xF811; 22const BUNDLE = 'ZipLib_'; 23const TAG: string = '[ZLibInterfaceListPage]'; 24const SLEEP_TIME = 3000; 25const BUFFER_SIZE = 13; 26const OFFSET = 0; 27const STRING_DATA = 'hello, hello!'; 28 29function sleep(time: number) { 30 return new Promise<void>((resolve) => setTimeout(resolve, time)); 31}; 32 33@Entry 34@Component 35struct ZLibInterfaceListPage { 36 async zlibDecompression(): Promise<void> { 37 hilog.info(DOMAIN, TAG, BUNDLE + `zlibDecompression begin`); 38 let context = AppStorage.get('context') as Context; 39 let pathDir = context.cacheDir; 40 let zlibFile = fileIo.openSync(pathDir + '/zlibFile.txt', fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 41 fileIo.writeSync(zlibFile.fd, STRING_DATA); 42 let arrayBufferIn = new ArrayBuffer(BUFFER_SIZE); 43 fileIo.readSync(zlibFile.fd, arrayBufferIn, { offset: OFFSET }); 44 fileIo.closeSync(zlibFile); 45 46 let arrayBufferOut = new ArrayBuffer(BUFFER_SIZE); 47 // 创建zip对象 48 let zip = zlib.createZipSync(); 49 // 压缩初始化 50 await zip.deflateInit({}, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED); 51 // 校验数据 52 let checksum = zlib.createChecksumSync(); 53 checksum.crc32(0, arrayBufferIn); 54 try { 55 // 压缩数据 56 await zip.deflate({ 57 nextIn: arrayBufferIn, 58 availableIn: BUFFER_SIZE, 59 nextOut: arrayBufferOut, 60 availableOut: BUFFER_SIZE 61 }, zlib.CompressFlushMode.FINISH).then((data) => { 62 // 将压缩后的buffer写入zlibDeflateFile.txt文件中 63 let zlibDeflateFile = 64 fileIo.openSync(pathDir + '/zlibDeflateFile.txt', fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 65 fileIo.writeSync(zlibDeflateFile.fd, arrayBufferOut); 66 fileIo.closeSync(zlibDeflateFile); 67 promptAction.showToast({ 68 message: $r('app.string.zlib_compress_success', STRING_DATA), 69 bottom: '11%' 70 }); 71 }) 72 } catch (errData) { 73 promptAction.showToast({ 74 message: $r('app.string.zlib_compress_failure'), 75 bottom: '11%' 76 }); 77 } 78 // 释放动态分配的数据结构 79 await zip.deflateEnd({ nextOut: arrayBufferOut }); 80 await sleep(SLEEP_TIME); 81 // 解压初始化 82 await zip.inflateInit({}); 83 let inflateArrayBufferOut = new ArrayBuffer(BUFFER_SIZE); 84 // 校验数据 85 checksum.crc32(0, arrayBufferOut); 86 try { 87 // 解压数据 88 await zip.inflate({ 89 nextIn: arrayBufferOut, 90 availableIn: BUFFER_SIZE, 91 nextOut: inflateArrayBufferOut, 92 availableOut: BUFFER_SIZE 93 }, zlib.CompressFlushMode.NO_FLUSH).then((data) => { 94 let array = new Uint8Array(inflateArrayBufferOut); 95 let dataString = ''; 96 for (let i = 0; i < array.length; i++) { 97 dataString += String.fromCharCode(array[i]); 98 } 99 // 将解压后的数据写入zlibInflateFile.txt文件中 100 let zlibInflateFile = 101 fileIo.openSync(pathDir + '/zlibInflateFile.txt', fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 102 fileIo.writeSync(zlibInflateFile.fd, dataString); 103 fileIo.closeSync(zlibInflateFile); 104 // 验证压缩与解压的数据是否一致 105 if (STRING_DATA == dataString) { 106 promptAction.showToast({ 107 message: $r('app.string.zlib_uncompress_success', dataString), 108 bottom: '11%' 109 }); 110 } 111 }) 112 } catch (errData) { 113 promptAction.showToast({ 114 message: $r('app.string.zlib_uncompress_failure'), 115 bottom: '11%' 116 }); 117 } 118 // 释放动态分配的数据结构 119 await zip.inflateEnd({ nextOut: arrayBufferOut }); 120 hilog.info(DOMAIN, TAG, BUNDLE + `zlibDecompression end`); 121 } 122 123 async gzipDecompression(): Promise<void> { 124 hilog.info(DOMAIN, TAG, BUNDLE + `gzipDecompression begin`); 125 let context = AppStorage.get('context') as Context; 126 let pathDir = context.cacheDir; 127 let file = fileIo.openSync(pathDir + '/gzipTest.gz', fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 128 let path = pathDir + '/gzipTest.gz'; 129 fileIo.closeSync(file); 130 131 // 创建gzip对象 132 let gzip = zlib.createGZipSync(); 133 // 打开.gz文件,对文件进行写操作 134 await gzip.gzopen(path, 'wb'); 135 let str = STRING_DATA; 136 let arrayBufferIn = new ArrayBuffer(BUFFER_SIZE); 137 let byteArray = new Uint8Array(arrayBufferIn); 138 for (let i = 0, j = str.length; i < j; i++) { 139 byteArray[i] = str.charCodeAt(i); 140 } 141 try { 142 // 将bufferWithData压缩并写入文件 143 await gzip.gzfwrite(arrayBufferIn, 8, 2).then((data) => { 144 promptAction.showToast({ 145 message: $r('app.string.gzip_compress_success', str), 146 bottom: '11%' 147 }); 148 }) 149 } catch (errData) { 150 promptAction.showToast({ 151 message: $r('app.string.gzip_compress_failure'), 152 bottom: '11%' 153 }); 154 } 155 await gzip.gzflush(0); 156 await gzip.gzclose(); 157 158 await sleep(SLEEP_TIME); 159 // 打开.gz文件,对文件进行读操作 160 await gzip.gzopen(path, 'rb'); 161 try { 162 let bufferWithDataOut = new ArrayBuffer(BUFFER_SIZE); 163 // 从.gz文件中读取和解压数据 164 await gzip.gzfread(bufferWithDataOut, 8, 2).then((data) => { 165 let array = new Uint8Array(bufferWithDataOut); 166 let dataString = ''; 167 for (let i = 0; i < array.length; i++) { 168 dataString += String.fromCharCode(array[i]); 169 } 170 if (dataString == str) { 171 promptAction.showToast({ 172 message: $r('app.string.gzip_uncompress_success', dataString), 173 bottom: '11%' 174 }); 175 let gzipFile = 176 fileIo.openSync(pathDir + '/gzipTest.txt', fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 177 fileIo.writeSync(gzipFile.fd, dataString); 178 fileIo.closeSync(gzipFile); 179 } 180 }) 181 } catch (errData) { 182 promptAction.showToast({ 183 message: $r('app.string.gzip_uncompress_failure'), 184 bottom: '11%' 185 }); 186 } 187 await gzip.gzclose(); 188 hilog.info(DOMAIN, TAG, BUNDLE + `gzipDecompression end`); 189 } 190 191 build() { 192 Column() { 193 Column() { 194 Row() { 195 Image($r('app.media.icon_back')) 196 .size({ height: '71%' }) 197 .objectFit(ImageFit.Contain) 198 .onClick(() => { 199 router.pushUrl({ 200 url: 'pages/Index' 201 }); 202 }) 203 Text($r('app.string.zlibInterface')) 204 .fontSize(20) 205 .margin({ left: 8 }) 206 .width('100%') 207 .height('100%') 208 .fontWeight(FontWeight.Bold) 209 } 210 .height('15.64%') 211 .width('91%') 212 213 Row() { 214 Text($r('app.string.CompressFiles')) 215 .textAlign(TextAlign.Start) 216 .fontSize(16) 217 .fontColor($r('app.color.text_color')) 218 Image($r('app.media.icon_next')) 219 .size({ height: '42.8%' }) 220 .objectFit(ImageFit.Contain) 221 } 222 .id('compressFiles') 223 .onClick(() => { 224 router.pushUrl({ 225 url: 'zlib/CompressFilesPage' 226 }); 227 }) 228 .justifyContent(FlexAlign.SpaceBetween) 229 .padding({ right: 12, left: 12 }) 230 .height('15.64%') 231 .width('91%') 232 .backgroundColor($r('app.color.start_window_background')) 233 .borderRadius(15) 234 .margin({ top: 8 }) 235 } 236 .alignItems(HorizontalAlign.Center) 237 .width('100%') 238 .height('50%') 239 240 Column() { 241 Text($r('app.string.tip_Gzip')) 242 .textAlign(TextAlign.Start) 243 .fontSize(12) 244 .fontColor($r('app.color.tip_Gzip_backeGround')) 245 .width('86.4%') 246 .height('9%') 247 .margin({ bottom: 24 }) 248 249 Button($r('app.string.gzip_interface')) 250 .id('gzipinterface') 251 .width('100%') 252 .height('100%') 253 .fontSize(16) 254 .fontFamily('HarmonyHeiTi') 255 .type(ButtonType.Capsule) 256 .backgroundColor($r('app.color.button_backeGround')) 257 .fontColor($r('app.color.button_color')) 258 .borderRadius(20) 259 .onClick(() => { 260 this.gzipDecompression(); 261 }) 262 .margin({ bottom: 12 }) 263 .width('91%') 264 .height('11.2%') 265 266 Button($r('app.string.zlib_interface')) 267 .id('zlibinterface') 268 .width('100%') 269 .height('100%') 270 .fontSize(16) 271 .fontFamily('HarmonyHeiTi') 272 .type(ButtonType.Capsule) 273 .backgroundColor($r('app.color.button_backeGround')) 274 .fontColor($r('app.color.button_color')) 275 .borderRadius(20) 276 .onClick(() => { 277 this.zlibDecompression(); 278 }) 279 .margin({ bottom: 16 }) 280 .width('91%') 281 .height('11.2%') 282 } 283 .justifyContent(FlexAlign.End) 284 .alignItems(HorizontalAlign.Center) 285 .width('100%') 286 .height('50%') 287 } 288 .backgroundColor($r('app.color.backGrounding')) 289 .width('100%') 290 .height('100%') 291 } 292}