1# @ohos.zlib (Zip模块) 2<!--Kit: Ability Kit--> 3<!--Subsystem: BundleManager--> 4<!--Owner: @wanghang904--> 5<!--Designer: @hanfeng6--> 6<!--Tester: @kongjing2--> 7<!--Adviser: @Brilliantry_Rui--> 8 9本模块提供压缩解压缩文件的能力。 10 11> **说明:** 12> 13> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14 15## 导入模块 16 17```javascript 18import { zlib } from '@kit.BasicServicesKit'; 19``` 20 21## zlib.zipFile<sup>(deprecated)</sup> 22zipFile(inFile: string, outFile: string, options: Options): Promise<void> 23 24压缩接口,压缩完成后返回执行结果,使用Promise异步返回。 25 26> **说明:** 27> 28> 从API version 7开始支持,从API version 9开始废弃。建议使用[zlib.compressFile](#zlibcompressfile9)。 29 30**系统能力:** SystemCapability.BundleManager.Zlib 31 32**参数:** 33 34| 参数名 | 类型 | 必填 | 说明 | 35| ------- | ------------------- | ---- | ------------------------------------------------------------ | 36| inFile | string | 是 | 指定压缩的文件夹路径或者文件路径,路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](../apis-ability-kit/js-apis-inner-app-context.md),[Stage模型](../apis-ability-kit/js-apis-inner-application-context.md)。 | 37| outFile | string | 是 | 指定压缩结果的文件路径(文件的扩展名zip)。 | 38| options | [Options](#options) | 是 | 压缩的可选参数。 | 39 40**返回值:** 41 42| 类型 | 说明 | 43| -------------- | ------------------------------------------------------------ | 44| Promise\<void> | Promise对象,无返回值。 | 45 46**示例:** 47 48```ts 49// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/temp,也可以通过context获取。 50import { zlib, BusinessError } from '@kit.BasicServicesKit'; 51 52let inFile = '/data/storage/el2/base/temp/filename.xxx'; 53let outFile = '/data/storage/el2/base/temp/xxx.zip'; 54let options: zlib.Options = { 55 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 56 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 57 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 58}; 59 60zlib.zipFile(inFile, outFile, options).then((data: void) => { 61 console.info('zipFile result is ' + JSON.stringify(data)); 62}).catch((err: BusinessError) => { 63 console.error('error is ' + JSON.stringify(err)); 64}); 65``` 66 67## zlib.unzipFile<sup>(deprecated)</sup> 68 69unzipFile(inFile:string, outFile:string, options: Options): Promise<void> 70 71解压文件,解压完成后返回执行结果,使用Promise异步返回。 72 73> **说明:** 74> 75> 从API version 7开始支持,从API version 9开始废弃。建议使用[zlib.decompressFile](#zlibdecompressfile9)。 76> 77> 传入的压缩包内部文件或者文件夹名称不能包含“../”,否则会返回-1错误码。 78 79**系统能力:** SystemCapability.BundleManager.Zlib 80 81**参数:** 82 83| 参数名 | 类型 | 必填 | 说明 | 84| ------- | ------------------- | ---- | ------------------------------------------------------------ | 85| inFile | string | 是 | 指定的待解压缩文件的文件路径,路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](../apis-ability-kit/js-apis-inner-app-context.md),[Stage模型](../apis-ability-kit/js-apis-inner-application-context.md)。如果待解压的.zip文件中包含中文的文件名或目录名,需使用UTF8进行编码,避免解压时文件名或目录名出现中文乱码。 | 86| outFile | string | 是 | 指定的解压文件路径。 | 87| options | [Options](#options) | 是 | 解压的可选参数。 | 88 89**返回值:** 90 91| 类型 | 说明 | 92| -------------- | ------------------------------------------------------------ | 93| Promise\<void> | Promise对象,无返回值。 | 94 95**示例:** 96 97```ts 98// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/temp,也可以通过context获取。 99import { zlib, BusinessError } from '@kit.BasicServicesKit'; 100 101let inFile = '/data/storage/el2/base/temp/xxx.zip'; 102let outFile = '/data/storage/el2/base/temp/xxx'; 103let options: zlib.Options = { 104 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 105 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 106 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 107}; 108 109zlib.unzipFile(inFile, outFile, options).then((data: void) => { 110 console.info('unzipFile result is ' + JSON.stringify(data)); 111}).catch((err: BusinessError) => { 112 console.error('error is ' + JSON.stringify(err)); 113}) 114``` 115 116## zlib.compressFile<sup>9+</sup> 117 118compressFile(inFile: string, outFile: string, options: Options, callback: AsyncCallback\<void>): void 119 120压缩文件,压缩的结果,使用callback异步回调返回。成功返回null,失败返回错误码。 121 122> **说明:** 123> 124> 为了避免路径穿越,从API version 13开始,inFile和outFile传入的参数不允许包含“../”,否则会返回900001、900002错误码。 125 126**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 127 128**系统能力:** SystemCapability.BundleManager.Zlib 129 130**参数:** 131 132| 参数名 | 类型 | 必填 | 说明 | 133| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ | 134| inFile | string | 是 | 指定压缩的文件夹路径或者文件路径,路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](../apis-ability-kit/js-apis-inner-app-context.md),[Stage模型](../apis-ability-kit/js-apis-inner-application-context.md)。待压缩的文件夹不可为空,否则使用[decompressFile](#zlibdecompressfile9)对压缩后的文件解压时会报错。 | 135| outFile | string | 是 | 指定的压缩结果的文件路径。多个线程同时压缩文件时,outFile不能相同。 | 136| options | [Options](#options) | 是 | 压缩的配置参数。 | 137| callback | AsyncCallback\<void> | 是 | 异步获取压缩结果之后的回调。成功返回null,失败返回错误码。 | 138 139**错误码:** 140 141以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 142 143| 错误码ID | 错误信息 | 144| -------- | --------------------------------------| 145| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 146| 900001 | The input source file is invalid. | 147| 900002 | The input destination file is invalid. | 148 149**示例:** 150 151```ts 152// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/temp,也可以通过context获取。 153import { zlib, BusinessError } from '@kit.BasicServicesKit'; 154 155let inFile = '/data/storage/el2/base/temp/filename.xxx'; 156let outFile = '/data/storage/el2/base/temp/xxx.zip'; 157let options: zlib.Options = { 158 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 159 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 160 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 161}; 162 163try { 164 zlib.compressFile(inFile, outFile, options, (errData: BusinessError) => { 165 if (errData !== null) { 166 console.error(`compressFile errData is errCode:${errData.code} message:${errData.message}`); 167 } else { 168 console.info(`compressFile success.`); 169 } 170 }) 171} catch (errData) { 172 let code = (errData as BusinessError).code; 173 let message = (errData as BusinessError).message; 174 console.error(`compressFile errData is errCode:${code} message:${message}`); 175} 176``` 177 178## zlib.compressFile<sup>9+</sup> 179 180compressFile(inFile: string, outFile: string, options: Options): Promise\<void> 181 182压缩文件,压缩的结果,使用Promise异步返回。成功时返回null,失败时返回错误码。 183 184> **说明:** 185> 186> 为了避免路径穿越,从API version 13开始,inFile和outFile传入的参数不允许包含“../”,否则会返回900001、900002错误码。 187 188**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 189 190**系统能力:** SystemCapability.BundleManager.Zlib 191 192**参数:** 193 194| 参数名 | 类型 | 必填 | 说明 | 195| ------- | ------------------- | ---- | ------------------------------------------------------------ | 196| inFile | string | 是 | 指定压缩的文件夹路径或者文件路径,路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](../apis-ability-kit/js-apis-inner-app-context.md),[Stage模型](../apis-ability-kit/js-apis-inner-application-context.md)。待压缩的文件夹不可为空,否则使用[decompressFile](#zlibdecompressfile9)对压缩后的文件解压时会报错。 | 197| outFile | string | 是 | 指定的压缩结果的文件路径。多个线程同时压缩文件时,outFile不能相同。 | 198| options | [Options](#options) | 是 | 压缩的配置参数。 | 199 200**返回值:** 201 202| 类型 | 说明 | 203| -------------- | ----------------------- | 204| Promise\<void> | Promise对象,无返回值。 | 205 206**错误码:** 207 208以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 209 210| 错误码ID | 错误信息 | 211| -------- | ------------------------------------- | 212| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 213| 900001 | The input source file is invalid. | 214| 900002 | The input destination file is invalid. | 215 216**示例:** 217 218```ts 219// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/temp,也可以通过context获取。 220import { zlib, BusinessError } from '@kit.BasicServicesKit'; 221 222let inFile = '/data/storage/el2/base/temp/filename.xxx'; 223let outFile = '/data/storage/el2/base/temp/xxx.zip'; 224let options: zlib.Options = { 225 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 226 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 227 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 228}; 229 230try { 231 zlib.compressFile(inFile, outFile, options).then((data: void) => { 232 console.info('compressFile success. data: ' + JSON.stringify(data)); 233 }).catch((errData: BusinessError) => { 234 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 235 }) 236} catch (errData) { 237 let code = (errData as BusinessError).code; 238 let message = (errData as BusinessError).message; 239 console.error(`errData is errCode:${code} message:${message}`); 240} 241``` 242 243## zlib.decompressFile<sup>9+</sup> 244 245decompressFile(inFile: string, outFile: string, options: Options, callback: AsyncCallback\<void>): void 246 247解压文件,解压的结果,使用callback异步回调返回。成功时返回null,失败时返回错误码。 248 249> **说明:** 250> 251> 为了避免路径穿越,从API version 13开始,inFile和outFile传入的参数不允许包含“../”,否则会返回900001、900002错误码。 252> 253> 传入的压缩包内部文件或者文件夹名称不能包含“../”,否则会返回900003错误码。 254 255**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 256 257**系统能力:** SystemCapability.BundleManager.Zlib 258 259**参数:** 260 261| 参数名 | 类型 | 必填 | 说明 | 262| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ | 263| inFile | string | 是 | 指定的待解压缩文件的文件路径,文件后缀需要以.zip结尾。文件路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](../apis-ability-kit/js-apis-inner-app-context.md),[Stage模型](../apis-ability-kit/js-apis-inner-application-context.md)。如果待解压的.zip文件中包含中文的文件名或目录名,需使用UTF8进行编码,避免解压时文件名或目录名出现中文乱码。 | 264| outFile | string | 是 | 指定的解压后的文件夹路径,文件夹目录路径需要在系统中存在,不存在则会解压失败。路径必须为沙箱路径,沙箱路径可以通过context获取,具体方法可参考[application/context(Stage模型)](../apis-ability-kit/js-apis-inner-application-context.md)或 [app/context(FA模型)](../apis-ability-kit/js-apis-inner-app-context.md)。如果待解压的文件或文件夹在解压后的路径下已经存在,则会直接覆盖同名文件或同名文件夹中的同名文件。多个线程同时解压文件时,outFile不能相同。 | 265| options | [Options](#options) | 是 | 解压的配置参数。 | 266| callback | AsyncCallback\<void> | 是 | 异步获取解压结果之后的回调。成功返回null,失败返回错误码。 | 267 268**错误码:** 269 270以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 271 272| 错误码ID | 错误信息 | 273| -------- | --------------------------------------| 274| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 275| 900001 | The input source file is invalid. | 276| 900002 | The input destination file is invalid. | 277| 900003 | The input source file is not in ZIP format or is damaged. | 278 279**示例:** 280 281```ts 282// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/temp,也可以通过context获取。 283import { zlib, BusinessError } from '@kit.BasicServicesKit'; 284 285let inFile = '/data/storage/el2/base/temp/xxx.zip'; 286let outFileDir = '/data/storage/el2/base/temp'; 287let options: zlib.Options = { 288 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 289 parallel: zlib.ParallelStrategy.PARALLEL_STRATEGY_PARALLEL_DECOMPRESSION 290}; 291 292try { 293 zlib.decompressFile(inFile, outFileDir, options, (errData: BusinessError) => { 294 if (errData !== null) { 295 console.error(`decompressFile errData is errCode:${errData.code} message:${errData.message}`); 296 } else { 297 console.info(`decompressFile success.`); 298 } 299 }) 300} catch (errData) { 301 let code = (errData as BusinessError).code; 302 let message = (errData as BusinessError).message; 303 console.error(`decompressFile errData is errCode:${code} message:${message}`); 304} 305``` 306 307## zlib.decompressFile<sup>9+</sup> 308 309decompressFile(inFile: string, outFile: string, options?: Options): Promise\<void> 310 311解压文件,解压的结果,使用Promise异步返回,成功时返回null,失败时返回错误码。 312 313> **说明:** 314> 315> 为了避免路径穿越,从API version 13开始,inFile和outFile传入的参数不允许包含“../”,否则会返回900001、900002错误码。 316> 317> 传入的压缩包内部文件或者文件夹名称不能包含“../”,否则会返回900003错误码。 318 319**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 320 321**系统能力:** SystemCapability.BundleManager.Zlib 322 323**参数:** 324 325| 参数名 | 类型 | 必填 | 说明 | 326| ------- | ------------------- | ---- | ------------------------------------------------------------ | 327| inFile | string | 是 | 指定的待解压缩文件的文件路径,文件后缀需要以.zip结尾。文件路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](../apis-ability-kit/js-apis-inner-app-context.md),[Stage模型](../apis-ability-kit/js-apis-inner-application-context.md)。如果待解压的.zip文件中包含中文的文件名或目录名,需使用UTF8进行编码,避免解压时文件名或目录名出现中文乱码。 | 328| outFile | string | 是 | 指定的解压后的文件夹路径,文件夹目录路径需要在系统中存在,不存在则会解压失败。路径必须为沙箱路径,沙箱路径可以通过context获取,具体方法可参考[application/context(Stage模型)](../apis-ability-kit/js-apis-inner-application-context.md)或 [app/context(FA模型)](../apis-ability-kit/js-apis-inner-app-context.md)。如果待解压的文件或文件夹在解压后的路径下已经存在,则会直接覆盖同名文件或同名文件夹中的同名文件。多个线程同时解压文件时,outFile不能相同。 | 329| options | [Options](#options) | 否 | 解压时的配置参数。 | 330 331**返回值:** 332 333| 类型 | 说明 | 334| -------------- | ----------------------- | 335| Promise\<void> | Promise对象,无返回值。 | 336 337**错误码:** 338 339以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 340 341| 错误码ID | 错误信息 | 342| ------ | ------------------------------------- | 343| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 344| 900001 | The input source file is invalid. | 345| 900002 | The input destination file is invalid. | 346| 900003 | The input source file is not in ZIP format or is damaged. | 347 348**示例:** 349 350```ts 351// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/temp,也可以通过context获取。 352import { zlib, BusinessError } from '@kit.BasicServicesKit'; 353 354let inFile = '/data/storage/el2/base/temp/xxx.zip'; 355let outFileDir = '/data/storage/el2/base/temp'; 356let options: zlib.Options = { 357 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION 358}; 359 360try { 361 zlib.decompressFile(inFile, outFileDir, options).then((data: void) => { 362 console.info('decompressFile success. data: ' + JSON.stringify(data)); 363 }).catch((errData: BusinessError) => { 364 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 365 }) 366} catch (errData) { 367 let code = (errData as BusinessError).code; 368 let message = (errData as BusinessError).message; 369 console.error(`errData is errCode:${code} message:${message}`); 370} 371``` 372 373## zlib.decompressFile<sup>10+</sup> 374 375decompressFile(inFile: string, outFile: string, callback: AsyncCallback\<void\>): void 376 377解压文件,解压的结果,使用callback异步回调返回。成功时返回null,失败时返回错误码。 378 379> **说明:** 380> 381> 为了避免路径穿越,从API version 13开始,inFile和outFile传入的参数不允许包含“../”,否则会返回900001、900002错误码。 382> 383> 传入的压缩包内部文件或者文件夹名称不能包含“../”,否则会返回900003错误码。 384 385**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 386 387**系统能力:** SystemCapability.BundleManager.Zlib 388 389**参数:** 390 391| 参数名 | 类型 | 必填 | 说明 | 392| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ | 393| inFile | string | 是 | 指定的待解压缩文件的文件路径,文件后缀需要以.zip结尾。文件路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](../apis-ability-kit/js-apis-inner-app-context.md),[Stage模型](../apis-ability-kit/js-apis-inner-application-context.md)。如果待解压的.zip文件中包含中文的文件名或目录名,需使用UTF8进行编码,避免解压时文件名或目录名出现中文乱码。 | 394| outFile | string | 是 | 指定的解压后的文件夹路径,文件夹目录路径需要在系统中存在,不存在则会解压失败。路径必须为沙箱路径,沙箱路径可以通过context获取,具体方法可参考[application/context(Stage模型)](../apis-ability-kit/js-apis-inner-application-context.md)或 [app/context(FA模型)](../apis-ability-kit/js-apis-inner-app-context.md)。如果待解压的文件或文件夹在解压后的路径下已经存在,则会直接覆盖同名文件或同名文件夹中的同名文件。多个线程同时解压文件时,outFile不能相同。 | 395| callback | AsyncCallback\<void> | 是 | 异步获取解压结果之后的回调。成功返回null,失败返回错误码。 | 396 397**错误码:** 398 399以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 400 401| 错误码ID | 错误信息 | 402| -------- | --------------------------------------| 403| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 404| 900001 | The input source file is invalid. | 405| 900002 | The input destination file is invalid. | 406| 900003 | The input source file is not in ZIP format or is damaged. | 407 408**示例:** 409 410```ts 411// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/temp,也可以通过context获取。 412import { zlib, BusinessError } from '@kit.BasicServicesKit'; 413 414let inFile = '/data/storage/el2/base/temp/xxx.zip'; 415let outFileDir = '/data/storage/el2/base/temp'; 416 417try { 418 zlib.decompressFile(inFile, outFileDir, (errData: BusinessError) => { 419 if (errData !== null) { 420 console.error(`decompressFile failed. code is ${errData.code}, message is ${errData.message}`); 421 } else { 422 console.info(`decompressFile success.`); 423 } 424 }) 425} catch (errData) { 426 let code = (errData as BusinessError).code; 427 let message = (errData as BusinessError).message; 428 console.error(`decompressFile failed. code is ${code}, message is ${message}`); 429} 430``` 431 432## zlib.getOriginalSize<sup>12+</sup> 433 434getOriginalSize(compressedFile: string): Promise\<number> 435 436获取压缩文件的原始大小,使用Promise异步返回。成功时返回压缩文件的原始大小,失败时返回错误码。 437 438**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 439 440**系统能力:** SystemCapability.BundleManager.Zlib 441 442**参数:** 443 444| 参数名 | 类型 | 必填 | 说明 | 445| ------- | ------------------- | ---- | ------------------------------------------------------------ | 446| compressedFile | string | 是 | 指定的压缩文件的文件路径,只支持zip格式压缩文件。文件路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](../apis-ability-kit/js-apis-inner-app-context.md),[Stage模型](../apis-ability-kit/js-apis-inner-application-context.md)。 | 447 448**返回值:** 449 450| 类型 | 说明 | 451| -------------- | ----------------------- | 452| Promise\<number> | Promise对象,返回压缩文件的原始大小,单位字节。 | 453 454**错误码:** 455 456以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 457 458| 错误码ID | 错误信息 | 459| ------ | ------------------------------------- | 460| 401 | The parameter check failed. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 461| 900001 | The input source file is invalid. | 462| 900003 | The input source file is not in ZIP format or is damaged. | 463 464**示例:** 465 466```ts 467// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/temp,也可以通过context获取。 468import { zlib, BusinessError } from '@kit.BasicServicesKit'; 469 470let compressedFile = '/data/storage/el2/base/temp/test.zip'; 471 472try { 473 zlib.getOriginalSize(compressedFile).then((data: number) => { 474 console.info(`getOriginalSize success. getOriginalSize: ${data}`); 475 }).catch((errData: BusinessError) => { 476 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 477 }) 478} catch (errData) { 479 let code = (errData as BusinessError).code; 480 let message = (errData as BusinessError).message; 481 console.error(`errData is errCode:${code} message:${message}`); 482} 483``` 484 485## zlib.compressFiles<sup>12+</sup> 486 487compressFiles(inFiles: Array<string>, outFile: string, options: Options): Promise<void> 488 489压缩指定的多个文件,使用Promise异步返回。成功时返回null,失败时返回错误码。 490 491**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 492 493**系统能力:** SystemCapability.BundleManager.Zlib 494 495**参数:** 496 497| 参数名 | 类型 | 必填 | 说明 | 498| ------- | ------------------- | ---- | ------------------------------------------------------------ | 499| inFiles | Array<string> | 是 | 指定压缩的文件夹路径或者文件路径,路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](../apis-ability-kit/js-apis-inner-app-context.md),[Stage模型](../apis-ability-kit/js-apis-inner-application-context.md)。待压缩的文件夹不可为空,否则使用[decompressFile](#zlibdecompressfile9)对压缩后的文件解压时会报错。 | 500| outFile | string | 是 | 指定的压缩结果的文件路径。多个线程同时压缩文件时,outFile不能相同。 | 501| options | [Options](#options) | 是 | 压缩的配置参数。 | 502 503**返回值:** 504 505| 类型 | 说明 | 506| ------------------- | ----------------------- | 507| Promise<void> | Promise对象,无返回值。 | 508 509**错误码:** 510 511以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 512 513| 错误码ID | 错误信息 | 514| -------- | ------------------------------------------------------------ | 515| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 516| 900001 | The input source file is invalid. | 517| 900002 | The input destination file is invalid. | 518 519**示例:** 520 521```typescript 522// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/temp,也可以通过context获取。 523import { zlib, BusinessError } from '@kit.BasicServicesKit'; 524 525let inFile = '/data/storage/el2/base/temp/filename.xxx'; 526let pathDir = 'data/storage/el2/base/temp/xxx'; 527let outFile = '/data/storage/el2/base/temp/xxx.zip'; 528let options: zlib.Options = { 529 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 530 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 531 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 532}; 533 534try { 535 zlib.compressFiles([inFile, pathDir], outFile, options).then((data: void) => { 536 console.info('compressFiles success. data: ' + JSON.stringify(data)); 537 }).catch((errData: BusinessError) => { 538 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 539 }) 540} catch (errData) { 541 let code = (errData as BusinessError).code; 542 let message = (errData as BusinessError).message; 543 console.error(`errData is errCode:${code} message:${message}`); 544} 545``` 546 547## zlib.createChecksum<sup>12+</sup> 548 549createChecksum(): Promise<Checksum> 550 551创建校验对象,使用Promise异步返回。成功时返回Checksum对象实例。 552 553**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 554 555**系统能力:** SystemCapability.BundleManager.Zlib 556 557**返回值:** 558 559| 类型 | 说明 | 560| -------------------------------------- | ------------------------------- | 561| Promise<[Checksum](#checksum12)> | Promise对象。返回校验对象实例。 | 562 563**示例:** 564 565```ts 566import { zlib } from '@kit.BasicServicesKit'; 567 568zlib.createChecksum().then((data) => { 569 console.info('createChecksum success'); 570}) 571``` 572 573## zlib.createChecksumSync<sup>12+</sup> 574 575createChecksumSync(): Checksum 576 577创建校验对象。成功时返回Checksum对象实例。 578 579**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 580 581**系统能力:** SystemCapability.BundleManager.Zlib 582 583**返回值:** 584 585| 类型 | 说明 | 586| ----------------------- | -------------- | 587| [Checksum](#checksum12) | 校验对象实例。 | 588 589**示例:** 590 591```ts 592import { zlib } from '@kit.BasicServicesKit'; 593 594let checksum = zlib.createChecksumSync() 595``` 596 597## Checksum<sup>12+</sup> 598 599校验对象。 600 601### adler32<sup>12+</sup> 602 603adler32(adler: number, buf: ArrayBuffer): Promise<number> 604 605计算Adler-32校验和,使用Promise异步返回。成功时返回计算后的Adler-32校验和,失败时返回错误码。 606 607**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 608 609**系统能力:** SystemCapability.BundleManager.Zlib 610 611**参数:** 612 613| 参数名 | 类型 | 必填 | 说明 | 614| ------ | ----------- | ---- | ------------------------ | 615| adler | number | 是 | Adler-32校验和的初始值。 | 616| buf | ArrayBuffer | 是 | 计算校验和数据缓冲区。 | 617 618**返回值:** 619 620| 类型 | 说明 | 621| --------------------- | ----------------------------------------- | 622| Promise<number> | Promise对象。返回计算后的Adler-32校验和。 | 623 624**错误码:** 625 626以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 627 628| 错误码ID | 错误信息 | 629| -------- | --------------------------------------| 630| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 631 632**示例:** 633 634```ts 635import { zlib } from '@kit.BasicServicesKit'; 636 637let str = 'hello world!'; 638let arrayBufferIn = new ArrayBuffer(12); 639let data = new Uint8Array(arrayBufferIn); 640 641for (let i = 0, j = str.length; i < j; i++) { 642 data[i] = str.charCodeAt(i); 643} 644 645let checksum = zlib.createChecksumSync() 646 647checksum.adler32(0, arrayBufferIn).then(data => { 648 console.info('adler32 success', data); 649}) 650``` 651 652### adler32Combine<sup>12+</sup> 653 654adler32Combine(adler1: number, adler2: number, len2: number): Promise<number> 655 656将两个Adler-32校验和合并,使用Promise异步返回。成功时返回合并后的Adler-32校验和,失败时返回错误码。 657 658**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 659 660**系统能力:** SystemCapability.BundleManager.Zlib 661 662**参数:** 663 664| 参数名 | 类型 | 必填 | 说明 | 665| ------ | ------ | ---- | ------------------------------------ | 666| adler1 | number | 是 | 第一个要合并的Adler-32校验和。 | 667| adler2 | number | 是 | 第二个要合并的Adler-32校验和。 | 668| len2 | number | 是 | 第二个Adler-32校验和的数据块的长度。 | 669 670**返回值:** 671 672| 类型 | 说明 | 673| --------------------- | ----------------------------------------- | 674| Promise<number> | Promise对象。返回合并后的Adler-32校验和。 | 675 676**错误码:** 677 678以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 679 680| 错误码ID | 错误信息 | 681| -------- | --------------------------------------| 682| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 683 684**示例:** 685 686```ts 687import { zlib, BusinessError } from '@kit.BasicServicesKit'; 688 689async function demo() { 690 let str = 'hello world!'; 691 let arrayBufferIn = new ArrayBuffer(12); 692 let data = new Uint8Array(arrayBufferIn); 693 for (let i = 0, j = str.length; i < j; i++) { 694 data[i] = str.charCodeAt(i); 695 } 696 let checksum = zlib.createChecksumSync() 697 let adler1 = 0; 698 let adler2 = 1; 699 await checksum.adler32(0, arrayBufferIn).then(data => { 700 console.info('adler32 success', data); 701 adler1 = data; 702 }) 703 await checksum.adler32(1, arrayBufferIn).then(data => { 704 console.info('adler32 success', data); 705 adler2 = data; 706 }) 707 await checksum.adler32Combine(adler1, adler2, 12).then((data) => { 708 console.info('adler32Combine success', data); 709 }).catch((errData: BusinessError) => { 710 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 711 }) 712} 713``` 714 715### crc32<sup>12+</sup> 716 717crc32(crc: number, buf: ArrayBuffer): Promise<number> 718 719更新CRC-32校验,使用Promise异步返回。成功时返回更新后的CRC-32校验,失败时返回错误码。 720 721**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 722 723**系统能力:** SystemCapability.BundleManager.Zlib 724 725**参数:** 726 727| 参数名 | 类型 | 必填 | 说明 | 728| ------ | ----------- | ---- | -------------------- | 729| crc | number | 是 | CRC-32校验的初始值。 | 730| buf | ArrayBuffer | 是 | 计算校验数据缓冲区。 | 731 732**返回值:** 733 734| 类型 | 说明 | 735| --------------------- | ------------------------------------- | 736| Promise<number> | Promise对象。返回更新后的CRC-32校验。 | 737 738**错误码:** 739 740以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 741 742| 错误码ID | 错误信息 | 743| -------- | --------------------------------------| 744| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 745 746**示例:** 747 748```ts 749import { zlib, BusinessError } from '@kit.BasicServicesKit'; 750 751let str = 'hello world!'; 752let arrayBufferIn = new ArrayBuffer(12); 753let data = new Uint8Array(arrayBufferIn); 754 755for (let i = 0, j = str.length; i < j; i++) { 756 data[i] = str.charCodeAt(i); 757} 758 759let checksum = zlib.createChecksumSync() 760 761checksum.crc32(0, arrayBufferIn).then((data) => { 762 console.info('crc32 success', data); 763}).catch((errData: BusinessError) => { 764 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 765}) 766``` 767 768### crc32Combine<sup>12+</sup> 769 770crc32Combine(crc1: number, crc2: number, len2: number): Promise<number> 771 772将两个CRC-32校验合并,使用Promise异步返回。成功时返回合并后的CRC-32校验,失败时返回错误码。 773 774**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 775 776**系统能力:** SystemCapability.BundleManager.Zlib 777 778**参数:** 779 780| 参数名 | 类型 | 必填 | 说明 | 781| ------ | ------ | ---- | -------------------------------- | 782| crc1 | number | 是 | 第一个要合并的CRC-32校验。 | 783| crc2 | number | 是 | 第二个要合并的CRC-32校验。 | 784| len2 | number | 是 | 第二个CRC-32校验的数据块的长度。 | 785 786**返回值:** 787 788| 类型 | 说明 | 789| --------------------- | ------------------------------------- | 790| Promise<number> | Promise对象。返回合并后的CRC-32校验。 | 791 792**错误码:** 793 794以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 795 796| 错误码ID | 错误信息 | 797| -------- | --------------------------------------| 798| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 799 800**示例:** 801 802```ts 803import { zlib, BusinessError } from '@kit.BasicServicesKit'; 804 805async function demo() { 806 let str = 'hello world!'; 807 let arrayBufferIn = new ArrayBuffer(12); 808 let data = new Uint8Array(arrayBufferIn); 809 for (let i = 0, j = str.length; i < j; i++) { 810 data[i] = str.charCodeAt(i); 811 } 812 let checksum = zlib.createChecksumSync() 813 let crc1 = 0; 814 let crc2 = 1; 815 await checksum.crc32(0, arrayBufferIn).then(data => { 816 console.info('crc32 success', data); 817 crc1 = data; 818 }) 819 await checksum.crc32(1, arrayBufferIn).then(data => { 820 console.info('crc32 success', data); 821 crc2 = data; 822 }) 823 await checksum.crc32Combine(crc1, crc2, 12).then((data) => { 824 console.info('crc32Combine success', data); 825 }).catch((errData: BusinessError) => { 826 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 827 }) 828} 829``` 830 831### crc64<sup>12+</sup> 832 833crc64(crc: number, buf: ArrayBuffer): Promise<number> 834 835更新CRC-64校验,使用Promise异步返回。成功时返回更新后的CRC-64校验,失败时返回错误码。 836 837**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 838 839**系统能力:** SystemCapability.BundleManager.Zlib 840 841**参数:** 842 843| 参数名 | 类型 | 必填 | 说明 | 844| ------ | ----------- | ---- | -------------------- | 845| crc | number | 是 | CRC-64校验的初始值。 | 846| buf | ArrayBuffer | 是 | 计算校验数据缓冲区。 | 847 848**返回值:** 849 850| 类型 | 说明 | 851| --------------------- | ------------------------------------- | 852| Promise<number> | Promise对象。返回更新后的CRC-64校验。 | 853 854**错误码:** 855 856以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 857 858| 错误码ID | 错误信息 | 859| -------- | --------------------------------------| 860| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 861 862**示例:** 863 864```ts 865import { zlib, BusinessError } from '@kit.BasicServicesKit'; 866 867let str = 'hello world!'; 868let arrayBufferIn = new ArrayBuffer(12); 869let data = new Uint8Array(arrayBufferIn); 870 871for (let i = 0, j = str.length; i < j; i++) { 872 data[i] = str.charCodeAt(i); 873} 874 875let checksum = zlib.createChecksumSync() 876 877checksum.crc64(0, arrayBufferIn).then((data) => { 878 console.info('crc64 success', data); 879}).catch((errData: BusinessError) => { 880 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 881}) 882``` 883 884### getCrcTable<sup>12+</sup> 885 886getCrcTable(): Promise<Array<number>> 887 888输出CRC-32校验表,使用Promise异步返回。成功时返回CRC-32校验表。 889 890**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 891 892**系统能力:** SystemCapability.BundleManager.Zlib 893 894**返回值:** 895 896| 类型 | 说明 | 897| ---------------------------------- | ------------------------------- | 898| Promise<Array<number>> | Promise对象。返回CRC-32校验表。 | 899 900**示例:** 901 902```ts 903import { zlib, BusinessError } from '@kit.BasicServicesKit'; 904 905let checksum = zlib.createChecksumSync() 906 907checksum.getCrcTable().then((data) => { 908 console.info('getCrcTable success'); 909}).catch((errData: BusinessError) => { 910 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 911}) 912``` 913 914### getCrc64Table<sup>12+</sup> 915 916getCrc64Table(): Promise<Array<number>> 917 918输出CRC-64校验表,使用Promise异步返回。成功时返回CRC-64校验表。 919 920**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 921 922**系统能力:** SystemCapability.BundleManager.Zlib 923 924**返回值:** 925 926| 类型 | 说明 | 927| ---------------------------------- | ------------------------------- | 928| Promise<Array<number>> | Promise对象。返回CRC-64校验表。 | 929 930**示例:** 931 932```ts 933import { zlib, BusinessError } from '@kit.BasicServicesKit'; 934 935let checksum = zlib.createChecksumSync() 936 937checksum.getCrc64Table().then((data) => { 938 console.info('getCrc64Table success'); 939}).catch((errData: BusinessError) => { 940 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 941}) 942``` 943 944## zlib.createZip<sup>12+</sup> 945 946createZip(): Promise<Zip> 947 948创建压缩解压缩对象实例,使用Promise异步返回,成功时返回压缩解压缩对象实例。 949 950**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 951 952**系统能力:** SystemCapability.BundleManager.Zlib 953 954**返回值:** 955 956| 类型 | 说明 | 957| ---------------------------- | ------------------------------------- | 958| Promise<[Zip](#zip12)> | Promise对象。返回压缩解压缩对象实例。 | 959 960**示例:** 961 962```ts 963import { zlib, BusinessError } from '@kit.BasicServicesKit'; 964 965zlib.createZip().then(data => { 966 console.info('createZip success'); 967}).catch((errData: BusinessError) => { 968 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 969}) 970``` 971 972## zlib.createZipSync<sup>12+</sup> 973 974createZipSync(): Zip 975 976创建压缩解压缩对象实例,成功时返回压缩解压缩对象实例。 977 978**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 979 980**系统能力:** SystemCapability.BundleManager.Zlib 981 982**返回值:** 983 984| 类型 | 说明 | 985| ------------- | ------------------------ | 986| [Zip](#zip12) | 返回压缩解压缩对象实例。 | 987 988**示例:** 989 990```ts 991import { zlib } from '@kit.BasicServicesKit'; 992 993let zip = zlib.createZipSync(); 994``` 995 996## Zip<sup>12+</sup> 997 998压缩解压缩对象实例,支持以zlib、deflate、gzip格式对数据进行压缩与解压。 999 1000### getZStream<sup>12+</sup> 1001 1002getZStream(): Promise<ZStream> 1003 1004输出流,使用Promise异步返回。 1005 1006**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1007 1008**系统能力:** SystemCapability.BundleManager.Zlib 1009 1010**返回值:** 1011 1012| 类型 | 说明 | 1013| ------------------------------------ | ------------------------- | 1014| Promise<[ZStream](#zstream12)> | Promise对象。返回ZStream流。 | 1015 1016**示例:** 1017 1018```ts 1019import { zlib } from '@kit.BasicServicesKit'; 1020 1021let zip = zlib.createZipSync(); 1022 1023zip.getZStream().then(data => { 1024 console.info('getZStream success'); 1025}) 1026``` 1027 1028### zlibVersion<sup>12+</sup> 1029 1030zlibVersion(): Promise<string> 1031 1032获取当前链接的zlib库的版本信息,使用Promise异步返回。成功时返回当前zlib库的版本信息。 1033 1034**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1035 1036**系统能力:** SystemCapability.BundleManager.Zlib 1037 1038**返回值:** 1039 1040| 类型 | 说明 | 1041| --------------------- | --------------------------------------- | 1042| Promise<string> | Promise对象。返回当前zlib库的版本信息。 | 1043 1044**示例:** 1045 1046```ts 1047import { zlib } from '@kit.BasicServicesKit'; 1048 1049let zip = zlib.createZipSync(); 1050 1051zip.zlibVersion().then((data) => { 1052 console.info('zlibVersion success') 1053}) 1054``` 1055 1056### zlibCompileFlags<sup>12+</sup> 1057 1058zlibCompileFlags(): Promise<number> 1059 1060返回指示编译时选项的标志,使用Promise异步返回。成功时返回指示编译时选项的标志。 1061 1062**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1063 1064**系统能力:** SystemCapability.BundleManager.Zlib 1065 1066**返回值:** 1067 1068| 类型 | 说明 | 1069| --------------------- | --------------------------------------- | 1070| Promise<number> | Promise对象。返回指示编译时选项的标志。 | 1071 1072**示例:** 1073 1074```ts 1075import { zlib } from '@kit.BasicServicesKit'; 1076 1077let zip = zlib.createZipSync(); 1078 1079zip.zlibCompileFlags().then((data) => { 1080 console.info('zlibCompileFlags success') 1081}) 1082``` 1083 1084### compress<sup>12+</sup> 1085 1086compress(dest: ArrayBuffer, source: ArrayBuffer, sourceLen?: number): Promise<ZipOutputInfo> 1087 1088将源缓冲区压缩到目标缓冲区,使用Promise异步返回。成功时返回结果状态和目标缓冲区的总大小。 1089 1090**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1091 1092**系统能力:** SystemCapability.BundleManager.Zlib 1093 1094**参数:** 1095 1096| 参数名 | 类型 | 必填 | 说明 | 1097| --------- | ----------- | ---- | -------------- | 1098| dest | ArrayBuffer | 是 | 目标缓冲区。 | 1099| source | ArrayBuffer | 是 | 源数据缓冲区。 | 1100| sourceLen | number | 否 | 源数据长度。默认值为0。 | 1101 1102**返回值:** 1103 1104| 类型 | 说明 | 1105| ------------------------------------------------ | ----------------------------------------------- | 1106| Promise<[ZipOutputInfo](#zipoutputinfo12)> | Promise对象。返回结果状态和目标缓冲区的总大小。 | 1107 1108**错误码:** 1109 1110以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 1111 1112| 错误码ID | 错误信息 | 1113| -------- | ------------------------------------------------------------ | 1114| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1115| 17800007 | The input buffer is incorrect, and the output buffer is too small to accommodate the compressed or decompressed data. | 1116 1117**示例:** 1118 1119```ts 1120import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1121 1122let str = 'hello world!'; 1123let arrayBufferIn = new ArrayBuffer(str.length); 1124let byteArray = new Uint8Array(arrayBufferIn); 1125 1126for (let i = 0, j = str.length; i < j; i++) { 1127 byteArray[i] = str.charCodeAt(i) 1128} 1129 1130let arrayBufferOut = new ArrayBuffer(100); 1131let zip = zlib.createZipSync(); 1132 1133zip.compress(arrayBufferOut, arrayBufferIn, 20).then((data) => { 1134 console.info('compress success:'); 1135}).catch((errData: BusinessError) => { 1136 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1137}) 1138``` 1139 1140### compress2<sup>12+</sup> 1141 1142compress2(dest: ArrayBuffer, source: ArrayBuffer, level: CompressLevel, sourceLen?: number): Promise<ZipOutputInfo> 1143 1144将源缓冲区压缩到目标缓冲区,使用Promise异步返回。成功时返回结果状态和目标缓冲区的总大小。 1145 1146**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1147 1148**系统能力:** SystemCapability.BundleManager.Zlib 1149 1150**参数:** 1151 1152| 参数名 | 类型 | 必填 | 说明 | 1153| --------- | ------------- | ---- | --------------------------------------------- | 1154| dest | ArrayBuffer | 是 | 目标缓冲区。 | 1155| source | ArrayBuffer | 是 | 源数据缓冲区。 | 1156| level | CompressLevel | 是 | 参考[CompressLevel枚举定义](#compresslevel)。 | 1157| sourceLen | number | 否 | 源数据长度。默认值为0。 | 1158 1159**返回值:** 1160 1161| 类型 | 说明 | 1162| ------------------------------------------------ | ----------------------------------------------- | 1163| Promise<[ZipOutputInfo](#zipoutputinfo12)> | Promise对象。返回结果状态和目标缓冲区的总大小。 | 1164 1165**错误码:** 1166 1167以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 1168 1169| 错误码ID | 错误信息 | 1170| -------- | ------------------------------------------------------------ | 1171| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1172| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 1173| 17800007 | The input buffer is incorrect, and the output buffer is too small to accommodate the compressed or decompressed data. | 1174 1175**示例:** 1176 1177```ts 1178import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1179 1180let str = 'hello world!'; 1181let arrayBufferIn = new ArrayBuffer(str.length); 1182let byteArray = new Uint8Array(arrayBufferIn); 1183 1184for (let i = 0, j = str.length; i < j; i++) { 1185 byteArray[i] = str.charCodeAt(i) 1186} 1187 1188let arrayBufferOut = new ArrayBuffer(100); 1189let zip = zlib.createZipSync(); 1190 1191zip.compress2(arrayBufferOut, arrayBufferIn, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 1192 console.info('compress2 success'); 1193}).catch((errData: BusinessError) => { 1194 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1195}) 1196``` 1197 1198### uncompress<sup>12+</sup> 1199 1200uncompress(dest:ArrayBuffer, source: ArrayBuffer, sourceLen?: number): Promise<ZipOutputInfo> 1201 1202将压缩后的数据解压缩为原始的未压缩形式,使用Promise异步返回。成功时返回结果状态和目标缓冲区的总大小。 1203 1204**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1205 1206**系统能力:** SystemCapability.BundleManager.Zlib 1207 1208**参数:** 1209 1210| 参数名 | 类型 | 必填 | 说明 | 1211| --------- | ----------- | ---- | -------------- | 1212| dest | ArrayBuffer | 是 | 目标缓冲区。 | 1213| source | ArrayBuffer | 是 | 源数据缓冲区。 | 1214| sourceLen | number | 否 | 源数据长度。默认值为0。 | 1215 1216**返回值:** 1217 1218| 类型 | 说明 | 1219| ------------------------------------------------ | ----------------------------------------------- | 1220| Promise<[ZipOutputInfo](#zipoutputinfo12)> | Promise对象。返回结果状态和目标缓冲区的总大小。 | 1221 1222**错误码:** 1223 1224以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 1225 1226| 错误码ID | 错误信息 | 1227| -------- | ------------------------------------------------------------ | 1228| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1229| 17800005 | The input data is incorrect. For example, the data does not conform to the zlib compression format, the compressed data is corrupted, or the data is not compressed. | 1230| 17800007 | The input buffer is incorrect, and the output buffer is too small to accommodate the compressed or decompressed data. | 1231 1232**示例:** 1233 1234```ts 1235import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1236 1237async function demo() { 1238 let str = 'hello world!'; 1239 let arrayBufferIn = new ArrayBuffer(str.length); 1240 let byteArray = new Uint8Array(arrayBufferIn); 1241 for (let i = 0, j = str.length; i < j; i++) { 1242 byteArray[i] = str.charCodeAt(i) 1243 } 1244 let arrayBufferOut = new ArrayBuffer(100); 1245 let zip = zlib.createZipSync(); 1246 await zip.compress(arrayBufferOut, arrayBufferIn, 12).then((data) => { 1247 console.info('compress success'); 1248 }).catch((errData: BusinessError) => { 1249 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1250 }) 1251 await zip.uncompress(arrayBufferIn, arrayBufferOut, 20).then((data) => { 1252 console.info('uncompress success'); 1253 }).catch((errData: BusinessError) => { 1254 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1255 }) 1256} 1257``` 1258 1259### uncompress2<sup>12+</sup> 1260 1261uncompress2(dest: ArrayBuffer, source: ArrayBuffer, sourceLen?: number): Promise<DecompressionOutputInfo> 1262 1263将压缩后的数据解压缩为原始的未压缩形式,使用Promise异步返回。成功时返回结果状态、目标缓冲区的总大小和源数据长度。 1264 1265**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1266 1267**系统能力:** SystemCapability.BundleManager.Zlib 1268 1269**参数:** 1270 1271| 参数名 | 类型 | 必填 | 说明 | 1272| --------- | ----------- | ---- | -------------- | 1273| dest | ArrayBuffer | 是 | 目标缓冲区。 | 1274| source | ArrayBuffer | 是 | 源数据缓冲区。 | 1275| sourceLen | number | 否 | 源数据长度。默认值为0。 | 1276 1277**返回值:** 1278 1279| 类型 | 说明 | 1280| ------------------------------------------------------------ | ----------------------------------------------------------- | 1281| Promise<[DecompressionOutputInfo](#decompressionoutputinfo12)> | Promise对象。返回结果状态、目标缓冲区的总大小和源数据长度。 | 1282 1283**错误码:** 1284 1285以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 1286 1287| 错误码ID | 错误信息 | 1288| -------- | ------------------------------------------------------------ | 1289| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1290| 17800005 | The input data is incorrect. For example, the data does not conform to the zlib compression format, the compressed data is corrupted, or the data is not compressed. | 1291| 17800007 | The input buffer is incorrect, and the output buffer is too small to accommodate the compressed or decompressed data. | 1292 1293**示例:** 1294 1295```ts 1296import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1297 1298async function demo() { 1299 let str = 'hello world!'; 1300 let arrayBufferIn = new ArrayBuffer(str.length); 1301 let byteArray = new Uint8Array(arrayBufferIn); 1302 for (let i = 0, j = str.length; i < j; i++) { 1303 byteArray[i] = str.charCodeAt(i) 1304 } 1305 let arrayBufferOut = new ArrayBuffer(100); 1306 let zip = zlib.createZipSync(); 1307 await zip.compress2(arrayBufferOut, arrayBufferIn, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 1308 console.info('compress2 success'); 1309 }).catch((errData: BusinessError) => { 1310 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1311 }) 1312 await zip.uncompress2(arrayBufferIn, arrayBufferOut, 20).then((data) => { 1313 console.info('uncompress2 success'); 1314 }).catch((errData: BusinessError) => { 1315 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1316 }) 1317} 1318``` 1319 1320### compressBound<sup>12+</sup> 1321 1322compressBound(sourceLen: number): Promise<number> 1323 1324计算返回压缩大小的上限,使用Promise异步返回。成功时返回压缩大小的上限。 1325 1326**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1327 1328**系统能力:** SystemCapability.BundleManager.Zlib 1329 1330**参数:** 1331 1332| 参数名 | 类型 | 必填 | 说明 | 1333| --------- | ------ | ---- | ------------ | 1334| sourceLen | number | 是 | 源数据长度。 | 1335 1336**返回值:** 1337 1338| 类型 | 说明 | 1339| --------------------- | --------------------------------- | 1340| Promise<number> | Promise对象。返回压缩大小的上限。 | 1341 1342**错误码:** 1343 1344以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1345 1346| 错误码ID | 错误信息 | 1347| -------- | ------------------------------------------------------------ | 1348| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1349 1350**示例:** 1351 1352```ts 1353import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1354 1355let str = 'hello world!'; 1356let arrayBufferIn = new ArrayBuffer(str.length); 1357let byteArray = new Uint8Array(arrayBufferIn); 1358 1359for (let i = 0, j = str.length; i < j; i++) { 1360 byteArray[i] = str.charCodeAt(i) 1361} 1362 1363let zip = zlib.createZipSync(); 1364 1365zip.compressBound(str.length).then((data) => { 1366 console.info('compressBound success') 1367}).catch((errData: BusinessError) => { 1368 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1369}) 1370``` 1371 1372### inflateValidate<sup>12+</sup> 1373 1374inflateValidate(strm: ZStream, check: number): Promise<ReturnStatus> 1375 1376验证压缩流结构内部的校验和,使用Promise异步返回。成功时返回结果状态。 1377 1378**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1379 1380**系统能力:** SystemCapability.BundleManager.Zlib 1381 1382**参数:** 1383 1384| 参数名 | 类型 | 必填 | 说明 | 1385| ------ | ------- | ---- | ------------------------------- | 1386| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 1387| check | number | 是 | 预期的校验和。 | 1388 1389**返回值:** 1390 1391| 类型 | 说明 | 1392| ---------------------------------------------- | --------------------------- | 1393| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 1394 1395**错误码:** 1396 1397以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 1398 1399| 错误码ID | 错误信息 | 1400| -------- | ------------------------------------------------------------ | 1401| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1402| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 1403 1404**示例:** 1405 1406```ts 1407import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1408 1409async function demo() { 1410 let str = 'hello world!'; 1411 let arrayBufferIn = new ArrayBuffer(str.length); 1412 let byteArray = new Uint8Array(arrayBufferIn); 1413 for (let i = 0, j = str.length; i < j; i++) { 1414 byteArray[i] = str.charCodeAt(i) 1415 } 1416 let arrayBufferOut = new ArrayBuffer(100); 1417 let zip = zlib.createZipSync(); 1418 await zip.inflateInit({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 } 1419 ).then(data => { 1420 console.info('inflateInit success') 1421 }).catch((errData: BusinessError) => { 1422 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1423 }) 1424 await zip.inflateValidate({ availableIn: 1 }, 1).then(data => { 1425 console.info('inflateValidate success') 1426 }).catch((errData: BusinessError) => { 1427 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1428 }) 1429} 1430``` 1431 1432### inflateSyncPoint<sup>12+</sup> 1433 1434inflateSyncPoint(strm: ZStream): Promise<ReturnStatus> 1435 1436查找当前解压缩流的同步点,使用Promise异步返回。成功时返回结果状态。 1437 1438**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1439 1440**系统能力:** SystemCapability.BundleManager.Zlib 1441 1442**参数:** 1443 1444| 参数名 | 类型 | 必填 | 说明 | 1445| ------ | ------- | ---- | ------------------------------- | 1446| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 1447 1448**返回值:** 1449 1450| 类型 | 说明 | 1451| ---------------------------------------------- | --------------------------- | 1452| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 1453 1454**错误码:** 1455 1456以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 1457 1458| 错误码ID | 错误信息 | 1459| -------- | ------------------------------------------------------------ | 1460| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1461| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 1462 1463**示例:** 1464 1465```ts 1466import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1467 1468async function demo() { 1469 let str = 'hello world!'; 1470 let arrayBufferIn = new ArrayBuffer(str.length); 1471 let byteArray = new Uint8Array(arrayBufferIn); 1472 for (let i = 0, j = str.length; i < j; i++) { 1473 byteArray[i] = str.charCodeAt(i) 1474 } 1475 let arrayBufferOut = new ArrayBuffer(100); 1476 let zip = zlib.createZipSync(); 1477 await zip.inflateInit({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 } 1478 ).then(data => { 1479 console.info('inflateInit success'); 1480 }).catch((errData: BusinessError) => { 1481 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1482 }) 1483 await zip.inflateSyncPoint({ availableIn: 1 }).then(data => { 1484 console.info('inflateSyncPoint success'); 1485 }).catch((errData: BusinessError) => { 1486 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1487 }) 1488} 1489``` 1490 1491### inflateSync<sup>12+</sup> 1492 1493inflateSync(strm: ZStream): Promise<ReturnStatus> 1494 1495跳过无效的压缩数据,直到找到一个可能的完整刷新点为止,使用Promise异步返回。成功时返回结果状态。 1496 1497**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1498 1499**系统能力:** SystemCapability.BundleManager.Zlib 1500 1501**参数:** 1502 1503| 参数名 | 类型 | 必填 | 说明 | 1504| ------ | ------- | ---- | ------------------------------- | 1505| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 1506 1507**返回值:** 1508 1509| 类型 | 说明 | 1510| ---------------------------------------------- | --------------------------- | 1511| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 1512 1513**错误码:** 1514 1515以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 1516 1517| 错误码ID | 错误信息 | 1518| -------- | ------------------------------------------------------------ | 1519| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1520| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 1521| 17800005 | The input data is incorrect. For example, the data does not conform to the zlib compression format, the compressed data is corrupted, or the data is not compressed. | 1522| 17800007 | The input buffer is incorrect, and the output buffer is too small to accommodate the compressed or decompressed data. | 1523 1524**示例:** 1525 1526```ts 1527import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1528 1529async function demo() { 1530 let str = 'hello, hello!'; 1531 let arrayBufferIn = new ArrayBuffer(str.length); 1532 let byteArray = new Uint8Array(arrayBufferIn); 1533 for (let i = 0, j = str.length; i < j; i++) { 1534 byteArray[i] = str.charCodeAt(i) 1535 } 1536 let arrayBufferOut = new ArrayBuffer(100); 1537 let zip = zlib.createZipSync(); 1538 await zip.deflateInit({}, zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION).then((data) => { 1539 console.info('deflateInit success') 1540 }).catch((errData: BusinessError) => { 1541 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1542 }) 1543 await zip.deflate({ nextIn: arrayBufferIn, availableIn: 3, nextOut: arrayBufferOut, availableOut: 100 }, zlib.CompressFlushMode.FULL_FLUSH).then((data) => { 1544 console.info('deflate success') 1545 }).catch((errData: BusinessError) => { 1546 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1547 }) 1548 await zip.deflate({ availableIn: 11 }, zlib.CompressFlushMode.FINISH).then((data) => { 1549 console.info('deflate success') 1550 }).catch((errData: BusinessError) => { 1551 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1552 }) 1553 await zip.deflateEnd({}).then(data => { 1554 console.info('deflateEnd success') 1555 }).catch((errData: BusinessError) => { 1556 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1557 }) 1558 try { 1559 await zip.inflateInit({ nextIn: arrayBufferOut, availableIn: 2 }).then(data => { 1560 console.info('inflateInit2 success') 1561 }) 1562 } catch (errData) { 1563 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1564 } 1565 await zip.inflate({ nextOut: arrayBufferIn, availableOut: 28 }, zlib.CompressFlushMode.NO_FLUSH).then((data) => { 1566 console.info('inflate success') 1567 }).catch((errData: BusinessError) => { 1568 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1569 }) 1570 await zip.inflateSync({ availableIn: 26 }).then(data => { 1571 console.info('inflateSync success'); 1572 }).catch((errData: BusinessError) => { 1573 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1574 }) 1575 await zip.inflateEnd({ nextOut: arrayBufferOut }).then((data) => { 1576 console.info('inflateEnd success') 1577 }).catch((errData: BusinessError) => { 1578 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1579 }) 1580} 1581``` 1582 1583### inflateResetKeep<sup>12+</sup> 1584 1585inflateResetKeep(strm: ZStream): Promise<ReturnStatus> 1586 1587重置解压缩流的状态,以保留分配的霍夫曼解码树和预设字典,使用Promise异步返回。成功时返回结果状态。 1588 1589**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1590 1591**系统能力:** SystemCapability.BundleManager.Zlib 1592 1593**参数:** 1594 1595| 参数名 | 类型 | 必填 | 说明 | 1596| ------ | ------- | ---- | ------------------------------- | 1597| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 1598 1599**返回值:** 1600 1601| 类型 | 说明 | 1602| ---------------------------------------------- | --------------------------- | 1603| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 1604 1605**错误码:** 1606 1607以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 1608 1609| 错误码ID | 错误信息 | 1610| -------- | ------------------------------------------------------------ | 1611| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1612| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 1613 1614**示例:** 1615 1616```ts 1617import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1618 1619async function demo() { 1620 let str = 'hello world!'; 1621 let arrayBufferIn = new ArrayBuffer(str.length); 1622 let byteArray = new Uint8Array(arrayBufferIn); 1623 for (let i = 0, j = str.length; i < j; i++) { 1624 byteArray[i] = str.charCodeAt(i) 1625 } 1626 let arrayBufferOut = new ArrayBuffer(100); 1627 let zip = zlib.createZipSync(); 1628 await zip.inflateInit({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 } 1629 ).then(data => { 1630 console.info('inflateInit success'); 1631 }).catch((errData: BusinessError) => { 1632 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1633 }) 1634 await zip.inflateResetKeep({ availableIn: 1 }).then(data => { 1635 console.info('inflateResetKeep success'); 1636 }).catch((errData: BusinessError) => { 1637 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1638 }) 1639} 1640``` 1641 1642### inflateSetDictionary<sup>12+</sup> 1643 1644inflateSetDictionary(strm: ZStream, dictionary: ArrayBuffer): Promise<ReturnStatus> 1645 1646从给定的未压缩字节序列初始化解压缩字典,使用Promise异步返回。成功时返回结果状态。 1647 1648**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1649 1650**系统能力:** SystemCapability.BundleManager.Zlib 1651 1652**参数:** 1653 1654| 参数名 | 类型 | 必填 | 说明 | 1655| ---------- | ----------- | ---- | ------------------------------- | 1656| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 1657| dictionary | ArrayBuffer | 是 | 字典数据。 | 1658 1659**返回值:** 1660 1661| 类型 | 说明 | 1662| ---------------------------------------------- | --------------------------- | 1663| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 1664 1665**错误码:** 1666 1667以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 1668 1669| 错误码ID | 错误信息 | 1670| -------- | ------------------------------------------------------------ | 1671| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1672| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 1673| 17800005 | The input data is incorrect. For example, the data does not conform to the zlib compression format, the compressed data is corrupted, or the data is not compressed. | 1674 1675**示例:** 1676 1677```ts 1678import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1679 1680async function demo() { 1681 let str = 'hello, hello!'; 1682 let arrayBufferIn = new ArrayBuffer(str.length); 1683 let byteArray = new Uint8Array(arrayBufferIn); 1684 for (let i = 0, j = str.length; i < j; i++) { 1685 byteArray[i] = str.charCodeAt(i) 1686 } 1687 let arrayBufferOut = new ArrayBuffer(100); 1688 let zip = zlib.createZipSync(); 1689 let dictionary = 'hello' 1690 let dictionarybuf = new ArrayBuffer(dictionary.length); 1691 let dictionarybufdata = new Uint8Array(dictionarybuf); 1692 for (let i = 0, j = dictionary.length; i < j; i++) { 1693 dictionarybufdata[i] = str.charCodeAt(i); 1694 } 1695 await zip.deflateInit({}, zlib.CompressLevel.COMPRESS_LEVEL_BEST_COMPRESSION).then((data) => { 1696 console.info('deflateInit success') 1697 }).catch((errData: BusinessError) => { 1698 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 1699 }) 1700 await zip.deflateSetDictionary({}, dictionarybuf).then((data) => { 1701 console.info('deflateSetDictionary success') 1702 }).catch((errData: BusinessError) => { 1703 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 1704 }) 1705 await zip.deflate({ nextIn: arrayBufferIn, availableIn: 14, nextOut: arrayBufferOut, availableOut: 100 }, zlib.CompressFlushMode.FINISH).then((data) => { 1706 console.info('deflate success') 1707 }).catch((errData: BusinessError) => { 1708 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 1709 }) 1710 await zip.deflateEnd({}).then(data => { 1711 console.info('deflateEnd success') 1712 }).catch((errData: BusinessError) => { 1713 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 1714 }) 1715 try { 1716 await zip.inflateInit({ nextIn: arrayBufferOut, availableIn: 100 }).then(data => { 1717 console.info('inflateInit success') 1718 }) 1719 } catch (errData) { 1720 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 1721 } 1722 await zip.inflate({ nextOut: arrayBufferIn, availableOut: 28 }, zlib.CompressFlushMode.NO_FLUSH).then((data) => { 1723 console.info('inflate success') 1724 }).catch((errData: BusinessError) => { 1725 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 1726 }) 1727 await zip.inflateSetDictionary({}, dictionarybuf).then((data) => { 1728 console.info('inflateSetDictionary success') 1729 }).catch((errData: BusinessError) => { 1730 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 1731 }) 1732 await zip.inflateEnd({ nextOut: arrayBufferOut }).then((data) => { 1733 console.info('inflateEnd success') 1734 }).catch((errData: BusinessError) => { 1735 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 1736 }) 1737} 1738``` 1739 1740### inflateReset2<sup>12+</sup> 1741 1742inflateReset2(strm: ZStream, windowBits: number): Promise<ReturnStatus> 1743 1744从给定的未压缩字节序列初始化解压缩字典,使用Promise异步返回。成功时返回结果状态。 1745 1746**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1747 1748**系统能力:** SystemCapability.BundleManager.Zlib 1749 1750**参数:** 1751 1752| 参数名 | 类型 | 必填 | 说明 | 1753| ---------- | ------- | ---- | ------------------------------- | 1754| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 1755| windowBits | number | 是 | 控制内存窗口的大小,并指定数据的格式(zlib、gzip、raw deflate)。取值如下:<br />zlib格式:[1, 15]。<br />gzip格式:大于15。<br />raw deflate格式:[-15, -1]。 | 1756 1757**返回值:** 1758 1759| 类型 | 说明 | 1760| ---------------------------------------------- | --------------------------- | 1761| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 1762 1763**错误码:** 1764 1765以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 1766 1767| 错误码ID | 错误信息 | 1768| -------- | ------------------------------------------------------------ | 1769| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1770| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 1771 1772**示例:** 1773 1774```ts 1775import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1776 1777async function demo() { 1778 let str = 'hello world!'; 1779 let arrayBufferIn = new ArrayBuffer(str.length); 1780 let byteArray = new Uint8Array(arrayBufferIn); 1781 for (let i = 0, j = str.length; i < j; i++) { 1782 byteArray[i] = str.charCodeAt(i) 1783 } 1784 let arrayBufferOut = new ArrayBuffer(100); 1785 let zip = zlib.createZipSync(); 1786 await zip.inflateInit({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 } 1787 ).then(data => { 1788 console.info('inflateInit success'); 1789 }).catch((errData: BusinessError) => { 1790 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1791 }) 1792 await zip.inflateReset2({ availableOut: 8 }, 15).then(data => { 1793 console.info('inflateReset2 success'); 1794 }).catch((errData: BusinessError) => { 1795 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1796 }) 1797} 1798``` 1799 1800### inflateReset<sup>12+</sup> 1801 1802inflateReset(strm: ZStream): Promise<ReturnStatus> 1803 1804这个函数相当于先调用inflateEnd再调用inflateInit,但是并不会释放和重新分配内部解压缩状态,使用Promise异步返回。成功时返回结果状态。 1805 1806**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1807 1808**系统能力:** SystemCapability.BundleManager.Zlib 1809 1810**参数:** 1811 1812| 参数名 | 类型 | 必填 | 说明 | 1813| ------ | ------- | ---- | ------------------------------- | 1814| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 1815 1816**返回值:** 1817 1818| 类型 | 说明 | 1819| ---------------------------------------------- | --------------------------- | 1820| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 1821 1822**错误码:** 1823 1824以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 1825 1826| 错误码ID | 错误信息 | 1827| -------- | ------------------------------------------------------------ | 1828| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1829| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 1830 1831**示例:** 1832 1833```ts 1834import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1835 1836async function demo() { 1837 let str = 'hello world!'; 1838 let arrayBufferIn = new ArrayBuffer(str.length); 1839 let byteArray = new Uint8Array(arrayBufferIn); 1840 for (let i = 0, j = str.length; i < j; i++) { 1841 byteArray[i] = str.charCodeAt(i) 1842 } 1843 let arrayBufferOut = new ArrayBuffer(100); 1844 let zip = zlib.createZipSync(); 1845 await zip.inflateInit({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 } 1846 ).then(data => { 1847 console.info('inflateInit success'); 1848 }).catch((errData: BusinessError) => { 1849 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1850 }) 1851 await zip.inflateReset({ availableIn: 1, availableOut: 8 }).then(data => { 1852 console.info('inflateReset success'); 1853 }).catch((errData: BusinessError) => { 1854 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1855 }) 1856} 1857``` 1858 1859### inflatePrime<sup>12+</sup> 1860 1861inflatePrime(strm: ZStream, bits: number, value: number): Promise<ReturnStatus> 1862 1863从给定的未压缩字节序列初始化解压缩字典,使用Promise异步返回。成功时返回结果状态。 1864 1865**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1866 1867**系统能力:** SystemCapability.BundleManager.Zlib 1868 1869**参数:** 1870 1871| 参数名 | 类型 | 必填 | 说明 | 1872| ------ | ------- | ---- | ------------------------------- | 1873| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 1874| bits | number | 是 | 提供的位。 | 1875| value | number | 是 | 提供的值。 | 1876 1877**返回值:** 1878 1879| 类型 | 说明 | 1880| ---------------------------------------------- | --------------------------- | 1881| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 1882 1883**错误码:** 1884 1885以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 1886 1887| 错误码ID | 错误信息 | 1888| -------- | ------------------------------------------------------------ | 1889| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1890| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 1891 1892**示例:** 1893 1894```ts 1895import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1896 1897async function demo() { 1898 let str = 'hello world!'; 1899 let arrayBufferIn = new ArrayBuffer(str.length); 1900 let byteArray = new Uint8Array(arrayBufferIn); 1901 for (let i = 0, j = str.length; i < j; i++) { 1902 byteArray[i] = str.charCodeAt(i) 1903 } 1904 let arrayBufferOut = new ArrayBuffer(100); 1905 let zip = zlib.createZipSync(); 1906 await zip.inflateInit({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 } 1907 ).then(data => { 1908 console.info('inflateInit success'); 1909 }).catch((errData: BusinessError) => { 1910 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1911 }) 1912 await zip.inflatePrime({ nextOut: arrayBufferOut }, 5, 2).then(data => { 1913 console.info('inflatePrime success'); 1914 }).catch((errData: BusinessError) => { 1915 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1916 }) 1917} 1918``` 1919 1920### inflateMark<sup>12+</sup> 1921 1922inflateMark(strm: ZStream): Promise<number> 1923 1924用于标记输入数据中的位置以供随机访问,使用Promise异步返回。成功时返回位置信息。 1925 1926**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1927 1928**系统能力:** SystemCapability.BundleManager.Zlib 1929 1930**参数:** 1931 1932| 参数名 | 类型 | 必填 | 说明 | 1933| ------ | ------- | ---- | ------------------------------- | 1934| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 1935 1936**返回值:** 1937 1938| 类型 | 说明 | 1939| --------------------- | --------------------------- | 1940| Promise<number> | Promise对象。返回位置信息。 | 1941 1942**错误码:** 1943 1944以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1945 1946| 错误码ID | 错误信息 | 1947| -------- | ------------------------------------------------------------ | 1948| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 1949 1950**示例:** 1951 1952```ts 1953import { zlib, BusinessError } from '@kit.BasicServicesKit'; 1954 1955async function demo() { 1956 let str = 'hello world!'; 1957 let arrayBufferIn = new ArrayBuffer(str.length); 1958 let byteArray = new Uint8Array(arrayBufferIn); 1959 for (let i = 0, j = str.length; i < j; i++) { 1960 byteArray[i] = str.charCodeAt(i) 1961 } 1962 let arrayBufferOut = new ArrayBuffer(100); 1963 let zip = zlib.createZipSync(); 1964 await zip.inflateInit({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 } 1965 ).then(data => { 1966 console.info('inflateInit success'); 1967 }).catch((errData: BusinessError) => { 1968 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1969 }) 1970 await zip.inflateMark({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 }).then(data => { 1971 console.info('inflateMark success'); 1972 }).catch((errData: BusinessError) => { 1973 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 1974 }) 1975} 1976``` 1977 1978### inflateInit2<sup>12+</sup> 1979 1980inflateInit2(strm: ZStream, windowBits: number): Promise<ReturnStatus> 1981 1982初始化内部流状态以进行解压缩,使用Promise异步返回。成功时返回结果状态。 1983 1984**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1985 1986**系统能力:** SystemCapability.BundleManager.Zlib 1987 1988**参数:** 1989 1990| 参数名 | 类型 | 必填 | 说明 | 1991| ---------- | ------- | ---- | ------------------------------- | 1992| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 1993| windowBits | number | 是 | 控制内存窗口的大小,并指定数据的格式(zlib、gzip、raw deflate)。取值如下:<br />zlib格式:[1, 15]。<br />gzip格式:大于15。<br />raw deflate格式:[-15, -1]。 | 1994 1995**返回值:** 1996 1997| 类型 | 说明 | 1998| ---------------------------------------------- | --------------------------- | 1999| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 2000 2001**错误码:** 2002 2003以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2004 2005| 错误码ID | 错误信息 | 2006| -------- | ------------------------------------------------------------ | 2007| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2008| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 2009 2010**示例:** 2011 2012```ts 2013import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2014 2015let str = 'hello world!'; 2016let arrayBufferIn = new ArrayBuffer(str.length); 2017let byteArray = new Uint8Array(arrayBufferIn); 2018 2019for (let i = 0, j = str.length; i < j; i++) { 2020 byteArray[i] = str.charCodeAt(i) 2021} 2022 2023let arrayBufferOut = new ArrayBuffer(100); 2024let zip = zlib.createZipSync(); 2025 2026zip.inflateInit2({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 }, 28 2027).then(data => { 2028 console.info('inflateInit2 success'); 2029}).catch((errData: BusinessError) => { 2030 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2031}) 2032``` 2033 2034### inflateInit<sup>12+</sup> 2035 2036inflateInit(strm: ZStream): Promise<ReturnStatus> 2037 2038初始化内部流状态以进行解压缩,使用Promise异步返回。成功时返回结果状态。 2039 2040**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2041 2042**系统能力:** SystemCapability.BundleManager.Zlib 2043 2044**参数:** 2045 2046| 参数名 | 类型 | 必填 | 说明 | 2047| ------ | ------- | ---- | ------------------------------- | 2048| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2049 2050**返回值:** 2051 2052| 类型 | 说明 | 2053| ---------------------------------------------- | --------------------------- | 2054| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 2055 2056**错误码:** 2057 2058以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2059 2060| 错误码ID | 错误信息 | 2061| -------- | ------------------------------------------------------------ | 2062| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2063 2064**示例:** 2065 2066```ts 2067import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2068 2069let str = 'hello world!'; 2070let arrayBufferIn = new ArrayBuffer(str.length); 2071let byteArray = new Uint8Array(arrayBufferIn); 2072 2073for (let i = 0, j = str.length; i < j; i++) { 2074 byteArray[i] = str.charCodeAt(i) 2075} 2076 2077let arrayBufferOut = new ArrayBuffer(100); 2078let zip = zlib.createZipSync(); 2079 2080zip.inflateInit({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 } 2081).then(data => { 2082 console.info('inflateInit success'); 2083}).catch((errData: BusinessError) => { 2084 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2085}) 2086``` 2087 2088### inflateGetHeader<sup>12+</sup> 2089 2090inflateGetHeader(strm: ZStream, header: GzHeader): Promise<ReturnStatus> 2091 2092用于在解压缩数据前设置gzip文件头部信息,使用Promise异步返回。成功时返回结果状态。 2093 2094**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2095 2096**系统能力:** SystemCapability.BundleManager.Zlib 2097 2098**参数:** 2099 2100| 参数名 | 类型 | 必填 | 说明 | 2101| ------ | ----------------------- | ---- | -------------------------------- | 2102| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2103| header | [GzHeader](#gzheader12) | 是 | 从压缩数据流中提取的gzip头信息。 | 2104 2105**返回值:** 2106 2107| 类型 | 说明 | 2108| ---------------------------------------------- | --------------------------- | 2109| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 2110 2111**错误码:** 2112 2113以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2114 2115| 错误码ID | 错误信息 | 2116| -------- | ------------------------------------------------------------ | 2117| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2118| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 2119 2120**示例:** 2121 2122```ts 2123import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2124 2125async function demo() { 2126 let str = 'hello world!'; 2127 let arrayBufferIn = new ArrayBuffer(str.length); 2128 let byteArray = new Uint8Array(arrayBufferIn); 2129 for (let i = 0, j = str.length; i < j; i++) { 2130 byteArray[i] = str.charCodeAt(i) 2131 } 2132 let arrayBufferOut = new ArrayBuffer(100); 2133 let zip = zlib.createZipSync(); 2134 await zip.inflateInit2({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 }, 28 2135 ).then(data => { 2136 console.info('inflateInit2 success'); 2137 }).catch((errData: BusinessError) => { 2138 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2139 }) 2140 await zip.inflateGetHeader({ availableIn: 1, availableOut: 1 }, { isText: true, os: 1, time: 1, xflags: 1, extra: arrayBufferIn, extraLen: 12, name: arrayBufferIn, comment: arrayBufferOut, hcrc: true, done: true }).then(data => { 2141 console.info('inflateGetHeader success'); 2142 }).catch((errData: BusinessError) => { 2143 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2144 }) 2145} 2146``` 2147 2148### inflateGetDictionary<sup>12+</sup> 2149 2150inflateGetDictionary(strm: ZStream, dictionary: ArrayBuffer): Promise<DictionaryOutputInfo> 2151 2152获取当前解压缩流中使用的解压缩字典内容及其长度,使用Promise异步返回。成功时返回结果状态和字典的长度。 2153 2154**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2155 2156**系统能力:** SystemCapability.BundleManager.Zlib 2157 2158**参数:** 2159 2160| 参数名 | 类型 | 必填 | 说明 | 2161| ---------- | ----------- | ---- | ------------------------------- | 2162| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2163| dictionary | ArrayBuffer | 是 | 接收解压缩字典的实际内容。 | 2164 2165**返回值:** 2166 2167| 类型 | 说明 | 2168| ------------------------------------------------------------ | --------------------------------------- | 2169| Promise<[DictionaryOutputInfo](#dictionaryoutputinfo12)> | Promise对象。返回结果状态和字典的长度。 | 2170 2171**错误码:** 2172 2173以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2174 2175| 错误码ID | 错误信息 | 2176| -------- | ------------------------------------------------------------ | 2177| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2178| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 2179 2180**示例:** 2181 2182```ts 2183import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2184 2185async function demo() { 2186 let str = 'hello world!'; 2187 let arrayBufferIn = new ArrayBuffer(str.length); 2188 let byteArray = new Uint8Array(arrayBufferIn); 2189 for (let i = 0, j = str.length; i < j; i++) { 2190 byteArray[i] = str.charCodeAt(i) 2191 } 2192 let arrayBufferOut = new ArrayBuffer(100); 2193 let zip = zlib.createZipSync(); 2194 await zip.inflateInit2({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 }, 28 2195 ).then(data => { 2196 console.info('inflateInit2 success'); 2197 }).catch((errData: BusinessError) => { 2198 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2199 }) 2200 await zip.inflateGetDictionary({ nextOut: arrayBufferOut }, arrayBufferOut).then((data) => { 2201 console.info('inflateGetDictionary success:') 2202 }).catch((errData: BusinessError) => { 2203 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2204 }) 2205} 2206``` 2207 2208### inflateEnd<sup>12+</sup> 2209 2210inflateEnd(strm: ZStream): Promise<ReturnStatus> 2211 2212解压流的所有动态分配的数据结构都被释放,使用Promise异步返回。成功时返回结果状态。 2213 2214**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2215 2216**系统能力:** SystemCapability.BundleManager.Zlib 2217 2218**参数:** 2219 2220| 参数名 | 类型 | 必填 | 说明 | 2221| ------ | ------- | ---- | ------------------------------- | 2222| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2223 2224**返回值:** 2225 2226| 类型 | 说明 | 2227| ---------------------------------------------- | --------------------------- | 2228| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 2229 2230**错误码:** 2231 2232以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2233 2234| 错误码ID | 错误信息 | 2235| -------- | ------------------------------------------------------------ | 2236| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2237| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 2238 2239**示例:** 2240 2241```ts 2242import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2243 2244async function demo() { 2245 let str = 'hello world!'; 2246 let arrayBufferIn = new ArrayBuffer(str.length); 2247 let byteArray = new Uint8Array(arrayBufferIn); 2248 for (let i = 0, j = str.length; i < j; i++) { 2249 byteArray[i] = str.charCodeAt(i) 2250 } 2251 let arrayBufferOut = new ArrayBuffer(100); 2252 let zip = zlib.createZipSync(); 2253 await zip.inflateInit({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 } 2254 ).then(data => { 2255 console.info('inflateInit success'); 2256 }).catch((errData: BusinessError) => { 2257 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2258 }) 2259 await zip.inflate({ availableIn: 8, availableOut: 8 }, 0).then((data) => { 2260 console.info('inflate success') 2261 }).catch((errData: BusinessError) => { 2262 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2263 }) 2264 await zip.inflateEnd({ nextOut: arrayBufferOut }).then((data) => { 2265 console.info('inflateEnd success') 2266 }).catch((errData: BusinessError) => { 2267 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2268 }) 2269} 2270``` 2271 2272### inflateCopy<sup>12+</sup> 2273 2274inflateCopy(source: Zip): Promise<ReturnStatus> 2275 2276复制解压流,使用Promise异步返回。成功时返回结果状态。 2277 2278**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2279 2280**系统能力:** SystemCapability.BundleManager.Zlib 2281 2282**参数:** 2283 2284| 参数名 | 类型 | 必填 | 说明 | 2285| ------ | ---- | ---- | ----------------------- | 2286| source | Zip | 是 | 参考[Zip定义](#zip12)。 | 2287 2288**返回值:** 2289 2290| 类型 | 说明 | 2291| ---------------------------------------------- | --------------------------- | 2292| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 2293 2294**错误码:** 2295 2296以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2297 2298| 错误码ID | 错误信息 | 2299| -------- | ------------------------------------------------------------ | 2300| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2301| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 2302 2303**示例:** 2304 2305```ts 2306import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2307 2308async function demo() { 2309 let str = 'hello world!'; 2310 let arrayBufferIn = new ArrayBuffer(str.length); 2311 let byteArray = new Uint8Array(arrayBufferIn); 2312 for (let i = 0, j = str.length; i < j; i++) { 2313 byteArray[i] = str.charCodeAt(i) 2314 } 2315 let arrayBufferOut = new ArrayBuffer(100); 2316 let zip = zlib.createZipSync(); 2317 await zip.inflateInit({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 } 2318 ).then(data => { 2319 console.info('inflateInit success'); 2320 }).catch((errData: BusinessError) => { 2321 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2322 }) 2323 let destZip = zlib.createZipSync(); 2324 await destZip.inflateCopy(zip).then((data) => { 2325 console.info('inflateCopy success') 2326 }).catch((errData: BusinessError) => { 2327 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2328 }) 2329} 2330``` 2331 2332### inflateCodesUsed<sup>12+</sup> 2333 2334inflateCodesUsed(strm: ZStream): Promise<number> 2335 2336当前解压缩流中使用的霍夫曼编码树的数量,使用Promise异步返回。成功时返回已使用的霍夫曼编码树的数量。 2337 2338**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2339 2340**系统能力:** SystemCapability.BundleManager.Zlib 2341 2342**参数:** 2343 2344| 参数名 | 类型 | 必填 | 说明 | 2345| ------ | ------- | ---- | ------------------------------- | 2346| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2347 2348**返回值:** 2349 2350| 类型 | 说明 | 2351| --------------------- | --------------------------------------------- | 2352| Promise<number> | Promise对象。返回已使用的霍夫曼编码树的数量。 | 2353 2354**错误码:** 2355 2356以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2357 2358| 错误码ID | 错误信息 | 2359| -------- | ------------------------------------------------------------ | 2360| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2361 2362**示例:** 2363 2364```ts 2365import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2366 2367async function demo() { 2368 let str = 'hello world!'; 2369 let arrayBufferIn = new ArrayBuffer(str.length); 2370 let byteArray = new Uint8Array(arrayBufferIn); 2371 for (let i = 0, j = str.length; i < j; i++) { 2372 byteArray[i] = str.charCodeAt(i) 2373 } 2374 let arrayBufferOut = new ArrayBuffer(100); 2375 let zip = zlib.createZipSync(); 2376 await zip.inflateInit({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 } 2377 ).then(data => { 2378 console.info('inflateInit success'); 2379 }).catch((errData: BusinessError) => { 2380 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2381 }) 2382 await zip.inflateCodesUsed({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 8 }).then(data => { 2383 console.info('inflateCodesUsed success'); 2384 }).catch((errData: BusinessError) => { 2385 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2386 }) 2387} 2388``` 2389 2390### inflateBackInit<sup>12+</sup> 2391 2392inflateBackInit(strm: ZStream, windowBits: number, window: ArrayBuffer): Promise<ReturnStatus> 2393 2394使用inflateBack()函数前初始化内部流状态以进行解压缩,使用Promise异步返回。成功时返回结果状态。 2395 2396**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2397 2398**系统能力:** SystemCapability.BundleManager.Zlib 2399 2400**参数:** 2401 2402| 参数名 | 类型 | 必填 | 说明 | 2403| ---------- | ----------- | ---- | --------------------------------------------- | 2404| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2405| windowBits | number | 是 | 控制内存窗口的大小,并指定数据的格式(zlib、gzip、raw deflate)。取值如下:<br />zlib格式:[1, 15]。<br />gzip格式:大于15。<br />raw deflate格式:[-15, -1]。 | 2406| window | ArrayBuffer | 是 | 预设的窗口缓冲区。 | 2407 2408**返回值:** 2409 2410| 类型 | 说明 | 2411| ---------------------------------------------- | --------------------------- | 2412| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 2413 2414**错误码:** 2415 2416以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2417 2418| 错误码ID | 错误信息 | 2419| -------- | ------------------------------------------------------------ | 2420| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2421| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 2422 2423**示例:** 2424 2425参考[inflateBack](#inflateback12)中的示例代码。 2426 2427### inflateBackEnd<sup>12+</sup> 2428 2429inflateBackEnd(strm: ZStream): Promise<ReturnStatus> 2430 2431inflateBackInit()函数分配的所有内存都被释放,使用Promise异步返回。成功时返回结果状态。 2432 2433**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2434 2435**系统能力:** SystemCapability.BundleManager.Zlib 2436 2437**参数:** 2438 2439| 参数名 | 类型 | 必填 | 说明 | 2440| ------ | ------- | ---- | ------------------------------- | 2441| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2442 2443**返回值:** 2444 2445| 类型 | 说明 | 2446| ---------------------------------------------- | --------------------------- | 2447| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 2448 2449**错误码:** 2450 2451以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2452 2453| 错误码ID | 错误信息 | 2454| -------- | ------------------------------------------------------------ | 2455| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2456| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 2457 2458**示例:** 2459 2460参考[inflateBack](#inflateback12)中的示例代码。 2461 2462### inflateBack<sup>12+</sup> 2463 2464inflateBack(strm: ZStream, backIn: InflateBackInputCallback, inDesc: object, backOut: InflateBackOutputCallback, outDesc: object): Promise<ReturnStatus> 2465 2466实现原始解压缩,采用回调接口来处理输入和输出,使用Promise异步返回。成功时返回结果状态。 2467 2468**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2469 2470**系统能力:** SystemCapability.BundleManager.Zlib 2471 2472**参数:** 2473 2474| 参数名 | 类型 | 必填 | 说明 | 2475| ------- | ------------------------- | ---- | ------------------------------------------------------------ | 2476| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2477| backIn | InflateBackInputCallback | 是 | 一种函数,用于从末尾解压缩数据,以从输入源读取原始压缩数据。 | 2478| inDesc | object | 是 | 通用对象。 | 2479| backOut | InflateBackOutputCallback | 是 | 将解压缩的数据写入目标输出。 | 2480| outDesc | object | 是 | 通用对象。 | 2481 2482**返回值:** 2483 2484| 类型 | 说明 | 2485| ---------------------------------------------- | --------------------------- | 2486| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 2487 2488**错误码:** 2489 2490以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2491 2492| 错误码ID | 错误信息 | 2493| -------- | ------------------------------------------------------------ | 2494| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified. <br />2. Incorrect parameter types. <br />3. Parameter verification failed. | 2495| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 2496 2497**示例:** 2498 2499```ts 2500import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2501 2502async function demo() { 2503 let readIn: (inDesc: object) => ArrayBuffer = (inDesc: object): ArrayBuffer => { 2504 console.info("inDesc = ", JSON.stringify(inDesc)); 2505 let buffer = new ArrayBuffer(26) 2506 let array = new Uint8Array(buffer); 2507 array.set([31, 139, 8, 0, 0, 0, 0, 0, 0, 10, 243, 72, 205, 201, 201, 231, 2, 0, 22, 53, 150, 49, 6, 0, 0, 0]); 2508 return buffer; 2509 } 2510 2511 let writeOut: (outDesc: object, buffer: ArrayBuffer, length: number) => number = (outDesc: object, buffer: ArrayBuffer, length: number): number => { 2512 console.info("outDesc = ", outDesc); 2513 console.info("buffer = ", buffer); 2514 console.info("length = ", length); 2515 let array = new Uint8Array(buffer); 2516 let dataString = ""; 2517 for (let i = 0; i < length; i++) { 2518 dataString += String.fromCharCode(array[i]); 2519 } 2520 console.info('writeOut ', dataString); 2521 return 0; 2522 } 2523 2524 let have = 0; 2525 let first = 1; 2526 let arrayBuffer = new ArrayBuffer(26); 2527 let next = new Uint8Array(arrayBuffer); 2528 let last = 0; 2529 let index = 0; 2530 let flags = 0; 2531 let NEXT2: () => number = (): number => { 2532 let o6: object = new Object() 2533 if (!have) { 2534 arrayBuffer = readIn(o6) 2535 next = new Uint8Array(arrayBuffer); 2536 console.info('readIn next = ', next.length) 2537 have = next.length; 2538 } 2539 if (have) { 2540 have--; 2541 last = next[index]; 2542 index++; 2543 } 2544 else { 2545 last = -1; 2546 } 2547 return last; 2548 } 2549 2550 let inflateBackTest: () => void = (async () => { 2551 try { 2552 have = 0; 2553 first = 1; 2554 arrayBuffer = new ArrayBuffer(26); 2555 next = new Uint8Array(arrayBuffer); 2556 last = 0; 2557 index = 0; 2558 flags = 0; 2559 let sr = zlib.createZipSync(); 2560 let buffer = new ArrayBuffer(1024) 2561 await sr.inflateBackInit({}, 15, buffer).then((result) => { 2562 console.info('inflateBackInit Call result res', result) 2563 }) 2564 let ret = 0; 2565 for (; ;) { 2566 if (NEXT2() == -1) { 2567 ret = 0; 2568 console.info('inflateBackTest Call result NEXT2() == -1') 2569 break; 2570 } 2571 console.info('have = last = ', have, last) 2572 if (last != 31 || NEXT2() != 139 ) { 2573 ret = first ? -3 : -1; 2574 console.info('inflateBackTest Call result last != 31 || (NEXT2() != 139 && last != 157)') 2575 break; 2576 } 2577 first = 0; 2578 ret = -5; 2579 if (NEXT2() != 8) { 2580 if (last < 0) { 2581 console.info('inflateBackTest Call result 1 last == -1') 2582 break; 2583 } 2584 } 2585 flags = NEXT2(); 2586 NEXT2(); 2587 NEXT2(); 2588 NEXT2(); 2589 NEXT2(); 2590 NEXT2(); 2591 NEXT2(); 2592 if (last < 0) { 2593 console.info('inflateBackTest Call result 2 last == -1') 2594 break; 2595 } 2596 console.info('index = have = ', next[index], have) 2597 let newArrayBuffer = new ArrayBuffer(have); 2598 let newNext = new Uint8Array(newArrayBuffer); 2599 for (let i = 0; i < have; i++) { 2600 newNext[i] = next[26 - have + i]; 2601 } 2602 console.info('newArrayBuffer.length = ', newArrayBuffer.byteLength) 2603 console.info('newNext.length = ', newNext.length) 2604 let zStream: zlib.ZStream = { 2605 nextIn: newArrayBuffer, 2606 availableIn: have, 2607 }; 2608 await sr.inflateBack( 2609 zStream, 2610 readIn, 2611 { fileName: 'test.gz' }, 2612 writeOut, 2613 { fileName: 'test.gz' }).then((result) => { 2614 ret = result; 2615 console.info('inflateBack Call result res', result) 2616 }) 2617 if (ret == 1) { 2618 console.info('inflateBackTest Call result success') 2619 break; 2620 } 2621 } 2622 await sr.inflateBackEnd({}).then((result) => { 2623 console.info('inflateBackEnd Call result res', result) 2624 }) 2625 } 2626 catch (errData) { 2627 console.error(`errData is message:${errData}`); 2628 } 2629 }) 2630 inflateBackTest(); 2631} 2632``` 2633 2634### InflateBackInputCallback<sup>12+</sup> 2635 2636type InflateBackInputCallback = (inDesc: object) => ArrayBuffer 2637 2638用于输入数据的回调函数。 2639 2640**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2641 2642**系统能力:** SystemCapability.BundleManager.Zlib 2643 2644**参数:** 2645 2646| 参数名 | 类型 | 必填 | 说明 | 2647| ------ | ------ | ---- | ------------------ | 2648| inDesc | object | 是 | 用户定义数据对象。 | 2649 2650**返回值:** 2651 2652| 类型 | 说明 | 2653| ---------------------------------------------- | --------------------------- | 2654| ArrayBuffer | 从输入数据源成功读取的内容缓冲区。 | 2655 2656### InflateBackOutputCallback<sup>12+</sup> 2657 2658type InflateBackOutputCallback = (outDesc: object, buf: ArrayBuffer, length: number) => number 2659 2660用于输出数据的回调函数。 2661 2662**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2663 2664**系统能力:** SystemCapability.BundleManager.Zlib 2665 2666**参数:** 2667 2668| 参数名 | 类型 | 必填 | 说明 | 2669| ------- | ----------- | ---- | ---------------------- | 2670| outDesc | object | 是 | 用户定义数据对象。 | 2671| buf | ArrayBuffer | 是 | 用于存储要写入的数据。 | 2672| length | number | 是 | 写入输出缓冲区的长度。 | 2673 2674**返回值:** 2675 2676| 类型 | 说明 | 2677| ---------------------------------------------- | --------------------------- | 2678| number | 输出缓冲区的字节数。 | 2679 2680### inflate<sup>12+</sup> 2681 2682inflate(strm: ZStream, flush: CompressFlushMode): Promise<ReturnStatus> 2683 2684解压数据,使用Promise异步返回。成功时返回结果状态。 2685 2686**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2687 2688**系统能力:** SystemCapability.BundleManager.Zlib 2689 2690**参数:** 2691 2692| 参数名 | 类型 | 必填 | 说明 | 2693| ------ | ----------------- | ---- | --------------------------------------------------- | 2694| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2695| flush | CompressFlushMode | 是 | 参考[CompressFlushMode定义](#compressflushmode12)。 | 2696 2697**返回值:** 2698 2699| 类型 | 说明 | 2700| ---------------------------------------------- | --------------------------- | 2701| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 2702 2703**错误码:** 2704 2705以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2706 2707| 错误码ID | 错误信息 | 2708| -------- | ------------------------------------------------------------ | 2709| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2710| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 2711| 17800005 | The input data is incorrect. For example, the data does not conform to the zlib compression format, the compressed data is corrupted, or the data is not compressed. | 2712 2713**示例:** 2714 2715```ts 2716import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2717 2718async function demo() { 2719 let str = 'hello world!'; 2720 let arrayBufferIn = new ArrayBuffer(str.length); 2721 let byteArray = new Uint8Array(arrayBufferIn); 2722 for (let i = 0, j = str.length; i < j; i++) { 2723 byteArray[i] = str.charCodeAt(i) 2724 } 2725 let arrayBufferOut = new ArrayBuffer(100); 2726 let zStream: zlib.ZStream = { 2727 nextIn: arrayBufferIn, 2728 availableIn: 1, 2729 nextOut: arrayBufferOut, 2730 availableOut: 1 2731 }; 2732 let zip = zlib.createZipSync(); 2733 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 2734 console.info('deflateInit success') 2735 }).catch((errData: BusinessError) => { 2736 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2737 }) 2738 await zip.deflate({ availableOut: 8 }, zlib.CompressFlushMode.FINISH).then((data) => { 2739 console.info('deflate success') 2740 }).catch((errData: BusinessError) => { 2741 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2742 }) 2743 await zip.deflateEnd({ nextOut: arrayBufferOut }).then(data => { 2744 console.info('deflateEnd success') 2745 }).catch((errData: BusinessError) => { 2746 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2747 }) 2748 await zip.inflateInit({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 } 2749 ).then(data => { 2750 console.info('inflateInit success'); 2751 }).catch((errData: BusinessError) => { 2752 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2753 }) 2754 await zip.inflate({ availableIn: 8, availableOut: 8 }, 0).then((data) => { 2755 console.info('inflate success') 2756 }).catch((errData: BusinessError) => { 2757 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2758 }) 2759 await zip.inflateEnd({ nextOut: arrayBufferOut }).then((data) => { 2760 console.info('inflateEnd success') 2761 }).catch((errData: BusinessError) => { 2762 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2763 }) 2764} 2765``` 2766 2767### deflateInit<sup>12+</sup> 2768 2769deflateInit(strm: ZStream, level: CompressLevel): Promise<ReturnStatus> 2770 2771初始化内部流状态以进行压缩,使用Promise异步返回。成功时返回结果状态。 2772 2773**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2774 2775**系统能力:** SystemCapability.BundleManager.Zlib 2776 2777**参数:** 2778 2779| 参数名 | 类型 | 必填 | 说明 | 2780| ------ | ------------- | ---- | --------------------------------------------- | 2781| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2782| level | CompressLevel | 是 | 参考[CompressLevel枚举定义](#compresslevel)。 | 2783 2784**返回值:** 2785 2786| 类型 | 说明 | 2787| ---------------------------------------------- | --------------------------- | 2788| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 2789 2790**错误码:** 2791 2792以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2793 2794| 错误码ID | 错误信息 | 2795| -------- | ------------------------------------------------------------ | 2796| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2797| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 2798 2799**示例:** 2800 2801```ts 2802import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2803 2804async function demo() { 2805 let str = 'hello world!'; 2806 let arrayBufferIn = new ArrayBuffer(str.length); 2807 let byteArray = new Uint8Array(arrayBufferIn); 2808 for (let i = 0, j = str.length; i < j; i++) { 2809 byteArray[i] = str.charCodeAt(i) 2810 } 2811 let arrayBufferOut = new ArrayBuffer(100); 2812 let zStream: zlib.ZStream = { 2813 nextIn: arrayBufferIn, 2814 availableIn: 1, 2815 nextOut: arrayBufferOut, 2816 availableOut: 1 2817 }; 2818 let zip = zlib.createZipSync(); 2819 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 2820 console.info('deflateInit success') 2821 }).catch((errData: BusinessError) => { 2822 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2823 }) 2824} 2825``` 2826 2827### deflateInit2<sup>12+</sup> 2828 2829deflateInit2(strm: ZStream, level: CompressLevel, method: CompressMethod, windowBits: number, memLevel: MemLevel, strategy: CompressStrategy): Promise<ReturnStatus> 2830 2831初始化内部流状态以进行压缩,使用Promise异步返回。成功时返回结果状态。 2832 2833**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2834 2835**系统能力:** SystemCapability.BundleManager.Zlib 2836 2837**参数:** 2838 2839| 参数名 | 类型 | 必填 | 说明 | 2840| ---------- | ---------------- | ---- | --------------------------------------------------- | 2841| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2842| level | CompressLevel | 是 | 参考[CompressLevel枚举定义](#compresslevel)。 | 2843| method | CompressMethod | 是 | 参考[CompressMethod枚举定义](#compressmethod12)。 | 2844| windowBits | number | 是 | 控制内存窗口的大小,并指定数据的格式(zlib、gzip、raw deflate)。取值如下:<br />zlib格式:[1, 15]。<br />gzip格式:大于15。<br />raw deflate格式:[-15, -1]。 | 2845| memLevel | MemLevel | 是 | 参考[MemLevel枚举定义](#memlevel)。 | 2846| strategy | CompressStrategy | 是 | 参考[CompressStrategy枚举定义](#compressstrategy)。 | 2847 2848**返回值:** 2849 2850| 类型 | 说明 | 2851| ---------------------------------------------- | --------------------------- | 2852| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 2853 2854**错误码:** 2855 2856以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2857 2858| 错误码ID | 错误信息 | 2859| -------- | ------------------------------------------------------------ | 2860| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2861| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 2862 2863**示例:** 2864 2865```ts 2866import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2867 2868async function demo() { 2869 let str = 'hello world!'; 2870 let arrayBufferIn = new ArrayBuffer(str.length); 2871 let byteArray = new Uint8Array(arrayBufferIn); 2872 for (let i = 0, j = str.length; i < j; i++) { 2873 byteArray[i] = str.charCodeAt(i) 2874 } 2875 let arrayBufferOut = new ArrayBuffer(100); 2876 let zStream: zlib.ZStream = { 2877 nextIn: arrayBufferIn, 2878 availableIn: 1, 2879 nextOut: arrayBufferOut, 2880 availableOut: 1 2881 }; 2882 let zip = zlib.createZipSync() 2883 await zip.deflateInit2(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED, zlib.CompressMethod.DEFLATED, 28, 2884 zlib.MemLevel.MEM_LEVEL_DEFAULT, zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY).then((data) => { 2885 console.info('deflateInit2 success'); 2886 }).catch((errData: BusinessError) => { 2887 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2888 }) 2889} 2890``` 2891 2892### deflate<sup>12+</sup> 2893 2894deflate(strm: ZStream, flush: CompressFlushMode): Promise<ReturnStatus> 2895 2896压缩数据,使用Promise异步返回。成功时返回结果状态。 2897 2898**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2899 2900**系统能力:** SystemCapability.BundleManager.Zlib 2901 2902**参数:** 2903 2904| 参数名 | 类型 | 必填 | 说明 | 2905| ------ | ----------------- | ---- | --------------------------------------------------- | 2906| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2907| flush | CompressFlushMode | 是 | 参考[CompressFlushMode定义](#compressflushmode12)。 | 2908 2909**返回值:** 2910 2911| 类型 | 说明 | 2912| ---------------------------------------------- | --------------------------- | 2913| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 2914 2915**错误码:** 2916 2917以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2918 2919| 错误码ID | 错误信息 | 2920| -------- | ------------------------------------------------------------ | 2921| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2922| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 2923| 17800007 | The input buffer is incorrect, and the output buffer is too small to accommodate the compressed or decompressed data. | 2924 2925**示例:** 2926 2927```ts 2928import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2929 2930async function demo() { 2931 let str = 'hello world!'; 2932 let arrayBufferIn = new ArrayBuffer(str.length); 2933 let byteArray = new Uint8Array(arrayBufferIn); 2934 for (let i = 0, j = str.length; i < j; i++) { 2935 byteArray[i] = str.charCodeAt(i) 2936 } 2937 let arrayBufferOut = new ArrayBuffer(100); 2938 let zStream: zlib.ZStream = { 2939 nextIn: arrayBufferIn, 2940 availableIn: 1, 2941 nextOut: arrayBufferOut, 2942 availableOut: 1 2943 }; 2944 let zip = zlib.createZipSync(); 2945 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 2946 console.info('deflateInit success') 2947 }).catch((errData: BusinessError) => { 2948 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2949 }) 2950 await zip.deflate({ availableOut: 8 }, zlib.CompressFlushMode.FINISH).then((data) => { 2951 console.info('deflate success') 2952 }).catch((errData: BusinessError) => { 2953 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2954 }) 2955} 2956``` 2957 2958### deflateEnd<sup>12+</sup> 2959 2960deflateEnd(strm: ZStream): Promise<ReturnStatus> 2961 2962压缩流的所有动态分配的数据结构都被释放,使用Promise异步返回。成功时返回结果状态。 2963 2964**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2965 2966**系统能力:** SystemCapability.BundleManager.Zlib 2967 2968**参数:** 2969 2970| 参数名 | 类型 | 必填 | 说明 | 2971| ------ | ------- | ---- | ------------------------------- | 2972| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2973 2974**返回值:** 2975 2976| 类型 | 说明 | 2977| ---------------------------------------------- | --------------------------- | 2978| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 2979 2980**错误码:** 2981 2982以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2983 2984| 错误码ID | 错误信息 | 2985| -------- | ------------------------------------------------------------ | 2986| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2987| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 2988 2989**示例:** 2990 2991```ts 2992import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2993 2994async function demo() { 2995 let str = 'hello world!'; 2996 let arrayBufferIn = new ArrayBuffer(str.length); 2997 let byteArray = new Uint8Array(arrayBufferIn); 2998 for (let i = 0, j = str.length; i < j; i++) { 2999 byteArray[i] = str.charCodeAt(i) 3000 } 3001 let arrayBufferOut = new ArrayBuffer(100); 3002 let zStream: zlib.ZStream = { 3003 nextIn: arrayBufferIn, 3004 availableIn: 1, 3005 nextOut: arrayBufferOut, 3006 availableOut: 1 3007 }; 3008 let zip = zlib.createZipSync(); 3009 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3010 console.info('deflateInit success') 3011 }).catch((errData: BusinessError) => { 3012 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 3013 }) 3014 await zip.deflate({ availableOut: 8 }, zlib.CompressFlushMode.FINISH).then((data) => { 3015 console.info('deflate success') 3016 }).catch((errData: BusinessError) => { 3017 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 3018 }) 3019 await zip.deflateEnd({ nextOut: arrayBufferOut }).then(data => { 3020 console.info('deflateEnd success') 3021 }).catch((errData: BusinessError) => { 3022 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3023 }) 3024} 3025``` 3026 3027### deflateBound<sup>12+</sup> 3028 3029deflateBound(strm: ZStream, sourceLength: number): Promise<number> 3030 3031计算压缩大小的上限,使用Promise异步返回。成功时返回压缩大小的上限。 3032 3033**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3034 3035**系统能力:** SystemCapability.BundleManager.Zlib 3036 3037**参数:** 3038 3039| 参数名 | 类型 | 必填 | 说明 | 3040| --------- | ------- | ---- | ------------------------------- | 3041| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3042| sourceLength | number | 是 | 源数据长度。 | 3043 3044**返回值:** 3045 3046| 类型 | 说明 | 3047| --------------------- | --------------------------------- | 3048| Promise<number> | Promise对象。返回压缩大小的上限。 | 3049 3050**错误码:** 3051 3052以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3053 3054| 错误码ID | 错误信息 | 3055| -------- | ------------------------------------------------------------ | 3056| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3057 3058**示例:** 3059 3060```ts 3061import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3062 3063async function demo() { 3064 let str = 'hello world!'; 3065 let arrayBufferIn = new ArrayBuffer(str.length); 3066 let byteArray = new Uint8Array(arrayBufferIn); 3067 for (let i = 0, j = str.length; i < j; i++) { 3068 byteArray[i] = str.charCodeAt(i) 3069 } 3070 let arrayBufferOut = new ArrayBuffer(100); 3071 let zStream: zlib.ZStream = { 3072 nextIn: arrayBufferIn, 3073 availableIn: 1, 3074 nextOut: arrayBufferOut, 3075 availableOut: 1 3076 }; 3077 let zip = zlib.createZipSync(); 3078 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3079 console.info('deflateInit success') 3080 }).catch((errData: BusinessError) => { 3081 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3082 }) 3083 await zip.deflateBound({ nextOut: arrayBufferOut }, 12).then((data) => { 3084 console.info('deflateBound success') 3085 }).catch((errData: BusinessError) => { 3086 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3087 }) 3088} 3089``` 3090 3091### deflateSetHeader<sup>12+</sup> 3092 3093deflateSetHeader(strm: ZStream, head: GzHeader): Promise<ReturnStatus> 3094 3095当deflateInit2()请求gzip流时,提供gzip标头信息,使用Promise异步返回。成功时返回结果状态。 3096 3097**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3098 3099**系统能力:** SystemCapability.BundleManager.Zlib 3100 3101**参数:** 3102 3103| 参数名 | 类型 | 必填 | 说明 | 3104| ------ | ----------------------- | ---- | -------------------------------- | 3105| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3106| head | [GzHeader](#gzheader12) | 是 | 从压缩数据流中提取的gzip头信息。 | 3107 3108**返回值:** 3109 3110| 类型 | 说明 | 3111| ---------------------------------------------- | --------------------------- | 3112| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 3113 3114**错误码:** 3115 3116以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3117 3118| 错误码ID | 错误信息 | 3119| -------- | ------------------------------------------------------------ | 3120| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3121| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 3122 3123**示例:** 3124 3125```ts 3126import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3127 3128async function demo() { 3129 let str = 'hello world!'; 3130 let arrayBufferIn = new ArrayBuffer(str.length); 3131 let byteArray = new Uint8Array(arrayBufferIn); 3132 for (let i = 0, j = str.length; i < j; i++) { 3133 byteArray[i] = str.charCodeAt(i) 3134 } 3135 let arrayBufferOut = new ArrayBuffer(100); 3136 let zStream: zlib.ZStream = { 3137 nextIn: arrayBufferIn, 3138 availableIn: 1, 3139 nextOut: arrayBufferOut, 3140 availableOut: 1 3141 }; 3142 let zip = zlib.createZipSync() 3143 await zip.deflateInit2(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED, zlib.CompressMethod.DEFLATED, 28, 3144 zlib.MemLevel.MEM_LEVEL_DEFAULT, zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY).then((data) => { 3145 console.info('deflateInit2 success'); 3146 }).catch((errData: BusinessError) => { 3147 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 3148 }) 3149 await zip.deflateSetHeader({ nextIn: arrayBufferIn, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 }, { isText: true, os: 1, time: 1, xflags: 1, extra: arrayBufferIn, extraLen: 12, name: arrayBufferIn, comment: arrayBufferOut, hcrc: true, done: true }).then((data) => { 3150 console.info('deflateSetHeader success'); 3151 }).catch((errData: BusinessError) => { 3152 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 3153 }) 3154} 3155``` 3156 3157### deflateCopy<sup>12+</sup> 3158 3159deflateCopy(source: Zip): Promise<ReturnStatus> 3160 3161复制压缩流,使用Promise异步返回。成功时返回结果状态。 3162 3163**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3164 3165**系统能力:** SystemCapability.BundleManager.Zlib 3166 3167**参数:** 3168 3169| 参数名 | 类型 | 必填 | 说明 | 3170| ------ | ---- | ---- | ----------------------- | 3171| source | Zip | 是 | 参考[Zip定义](#zip12)。 | 3172 3173**返回值:** 3174 3175| 类型 | 说明 | 3176| ---------------------------------------------- | --------------------------- | 3177| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 3178 3179**错误码:** 3180 3181以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3182 3183| 错误码ID | 错误信息 | 3184| -------- | ------------------------------------------------------------ | 3185| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3186| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 3187 3188**示例:** 3189 3190```ts 3191import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3192 3193async function demo() { 3194 let str = 'hello world!'; 3195 let arrayBufferIn = new ArrayBuffer(str.length); 3196 let byteArray = new Uint8Array(arrayBufferIn); 3197 for (let i = 0, j = str.length; i < j; i++) { 3198 byteArray[i] = str.charCodeAt(i) 3199 } 3200 let arrayBufferOut = new ArrayBuffer(100); 3201 let zStream: zlib.ZStream = { 3202 nextIn: arrayBufferIn, 3203 availableIn: 1, 3204 nextOut: arrayBufferOut, 3205 availableOut: 1 3206 }; 3207 let zip = zlib.createZipSync(); 3208 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3209 console.info('deflateInit success') 3210 }).catch((errData: BusinessError) => { 3211 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3212 }) 3213 await zip.deflateCopy(zip).then((data) => { 3214 console.info('deflateCopy success') 3215 }).catch((errData: BusinessError) => { 3216 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3217 }) 3218} 3219``` 3220 3221### deflateSetDictionary<sup>12+</sup> 3222 3223deflateSetDictionary(strm: ZStream, dictionary: ArrayBuffer): Promise<ReturnStatus> 3224 3225从给定的字节序列初始化压缩字典,使用Promise异步返回。成功时返回结果状态。 3226 3227**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3228 3229**系统能力:** SystemCapability.BundleManager.Zlib 3230 3231**参数:** 3232 3233| 参数名 | 类型 | 必填 | 说明 | 3234| ---------- | ----------- | ---- | ------------------------------- | 3235| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3236| dictionary | ArrayBuffer | 是 | 字典数据。 | 3237 3238**返回值:** 3239 3240| 类型 | 说明 | 3241| ---------------------------------------------- | --------------------------- | 3242| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 3243 3244**错误码:** 3245 3246以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3247 3248| 错误码ID | 错误信息 | 3249| -------- | ------------------------------------------------------------ | 3250| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3251| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 3252 3253**示例:** 3254 3255```ts 3256import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3257 3258async function demo() { 3259 let str = 'hello world!'; 3260 let arrayBufferIn = new ArrayBuffer(str.length); 3261 let byteArray = new Uint8Array(arrayBufferIn); 3262 for (let i = 0, j = str.length; i < j; i++) { 3263 byteArray[i] = str.charCodeAt(i) 3264 } 3265 let arrayBufferOut = new ArrayBuffer(100); 3266 let zStream: zlib.ZStream = { 3267 nextIn: arrayBufferIn, 3268 availableIn: 1, 3269 nextOut: arrayBufferOut, 3270 availableOut: 1 3271 }; 3272 let zip = zlib.createZipSync(); 3273 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3274 console.info('deflateInit success') 3275 }).catch((errData: BusinessError) => { 3276 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3277 }) 3278 await zip.deflateSetDictionary({ nextOut: arrayBufferOut }, arrayBufferOut).then((data) => { 3279 console.info('deflateSetDictionary success') 3280 }).catch((errData: BusinessError) => { 3281 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3282 }) 3283} 3284``` 3285 3286### deflateGetDictionary<sup>12+</sup> 3287 3288deflateGetDictionary(strm: ZStream, dictionary: ArrayBuffer): Promise<DictionaryOutputInfo> 3289 3290获取当前解压缩流中使用的解压缩字典内容及其长度,使用Promise异步返回。成功时返回结果状态和字典的长度。 3291 3292**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3293 3294**系统能力:** SystemCapability.BundleManager.Zlib 3295 3296**参数:** 3297 3298| 参数名 | 类型 | 必填 | 说明 | 3299| ---------- | ----------- | ---- | ------------------------------- | 3300| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3301| dictionary | ArrayBuffer | 是 | 接收解压缩字典的实际内容。 | 3302 3303**返回值:** 3304 3305| 类型 | 说明 | 3306| ------------------------------------------------------------ | --------------------------------------- | 3307| Promise<[DictionaryOutputInfo](#dictionaryoutputinfo12)> | Promise对象。返回结果状态和字典的长度。 | 3308 3309**错误码:** 3310 3311以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3312 3313| 错误码ID | 错误信息 | 3314| -------- | ------------------------------------------------------------ | 3315| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3316| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 3317 3318**示例:** 3319 3320```ts 3321import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3322 3323async function demo() { 3324 let str = 'hello world!'; 3325 let arrayBufferIn = new ArrayBuffer(str.length); 3326 let byteArray = new Uint8Array(arrayBufferIn); 3327 for (let i = 0, j = str.length; i < j; i++) { 3328 byteArray[i] = str.charCodeAt(i) 3329 } 3330 let arrayBufferOut = new ArrayBuffer(100); 3331 let zStream: zlib.ZStream = { 3332 nextIn: arrayBufferIn, 3333 availableIn: 1, 3334 nextOut: arrayBufferOut, 3335 availableOut: 1 3336 }; 3337 let zip = zlib.createZipSync(); 3338 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3339 console.info('deflateInit success') 3340 }).catch((errData: BusinessError) => { 3341 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3342 }) 3343 await zip.deflateSetDictionary({ nextOut: arrayBufferOut }, arrayBufferOut).then((data) => { 3344 console.info('deflateSetDictionary success') 3345 }).catch((errData: BusinessError) => { 3346 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3347 }) 3348 await zip.deflateGetDictionary({ nextOut: arrayBufferOut }, arrayBufferOut).then((data) => { 3349 console.info('deflateGetDictionary success') 3350 }).catch((errData: BusinessError) => { 3351 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3352 }) 3353} 3354``` 3355 3356### deflateTune<sup>12+</sup> 3357 3358deflateTune(strm: ZStream, goodLength: number, maxLazy: number, niceLength: number, maxChain: number): Promise<ReturnStatus> 3359 3360微调deflate的内部压缩参数,使用Promise异步返回。成功时返回结果状态。 3361 3362**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3363 3364**系统能力:** SystemCapability.BundleManager.Zlib 3365 3366**参数:** 3367 3368| 参数名 | 类型 | 必填 | 说明 | 3369| ---------- | ------- | ---- | ------------------------------- | 3370| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3371| goodLength | number | 是 | 匹配的长度阈值。 | 3372| maxLazy | number | 是 | 压缩算法在构建哈夫曼树时的延迟匹配策略,取值范围为0到4的整数。1到4,值越大,算法越‘懒’,匹配过程越慢,但可能生成更优的压缩结果。0:禁用懒惰匹配,算法会尽快构建哈夫曼树,压缩速度快,但压缩率低。 | 3373| niceLength | number | 是 | 适合的延迟长度阈值。 | 3374| maxChain | number | 是 | 最大链条长度。 | 3375 3376**返回值:** 3377 3378| 类型 | 说明 | 3379| ---------------------------------------------- | --------------------------- | 3380| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 3381 3382**错误码:** 3383 3384以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3385 3386| 错误码ID | 错误信息 | 3387| -------- | ------------------------------------------------------------ | 3388| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3389| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 3390 3391**示例:** 3392 3393```ts 3394import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3395 3396async function demo() { 3397 let str = 'hello world!'; 3398 let arrayBufferIn = new ArrayBuffer(str.length); 3399 let byteArray = new Uint8Array(arrayBufferIn); 3400 for (let i = 0, j = str.length; i < j; i++) { 3401 byteArray[i] = str.charCodeAt(i) 3402 } 3403 let arrayBufferOut = new ArrayBuffer(100); 3404 let zStream: zlib.ZStream = { 3405 nextIn: arrayBufferIn, 3406 availableIn: 1, 3407 nextOut: arrayBufferOut, 3408 availableOut: 1 3409 }; 3410 let zip = zlib.createZipSync(); 3411 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3412 console.info('deflateInit success') 3413 }).catch((errData: BusinessError) => { 3414 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3415 }) 3416 await zip.deflateTune({ nextOut: arrayBufferOut }, 2, 2, 2, 2).then((data) => { 3417 console.info('deflateTune success:') 3418 }).catch((errData: BusinessError) => { 3419 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3420 }) 3421} 3422``` 3423 3424### deflateReset<sup>12+</sup> 3425 3426deflateReset(strm: ZStream): Promise<ReturnStatus> 3427 3428这个函数相当于先调用deflateEnd再调用deflateInit,但是并不会释放和重新分配内部解压缩状态,使用Promise异步返回。成功时返回结果状态。 3429 3430**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3431 3432**系统能力:** SystemCapability.BundleManager.Zlib 3433 3434**参数:** 3435 3436| 参数名 | 类型 | 必填 | 说明 | 3437| ------ | ------- | ---- | ------------------------------- | 3438| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3439 3440**返回值:** 3441 3442| 类型 | 说明 | 3443| ---------------------------------------------- | --------------------------- | 3444| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 3445 3446**错误码:** 3447 3448以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3449 3450| 错误码ID | 错误信息 | 3451| -------- | ------------------------------------------------------------ | 3452| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3453| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 3454 3455**示例:** 3456 3457```ts 3458import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3459 3460async function demo() { 3461 let str = 'hello world!'; 3462 let arrayBufferIn = new ArrayBuffer(str.length); 3463 let byteArray = new Uint8Array(arrayBufferIn); 3464 for (let i = 0, j = str.length; i < j; i++) { 3465 byteArray[i] = str.charCodeAt(i) 3466 } 3467 let arrayBufferOut = new ArrayBuffer(100); 3468 let zStream: zlib.ZStream = { 3469 nextIn: arrayBufferIn, 3470 availableIn: 1, 3471 nextOut: arrayBufferOut, 3472 availableOut: 1 3473 }; 3474 let zip = zlib.createZipSync(); 3475 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3476 console.info('deflateInit success') 3477 }).catch((errData: BusinessError) => { 3478 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3479 }) 3480 await zip.deflateReset({ nextOut: arrayBufferOut }).then((data) => { 3481 console.info('deflateReset success') 3482 }).catch((errData: BusinessError) => { 3483 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3484 }) 3485} 3486``` 3487 3488### deflateResetKeep<sup>12+</sup> 3489 3490deflateResetKeep(strm: ZStream): Promise<ReturnStatus> 3491 3492重置初始化的deflate压缩流,但保留其设置的压缩参数和字典,使用Promise异步返回。成功时返回结果状态。 3493 3494**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3495 3496**系统能力:** SystemCapability.BundleManager.Zlib 3497 3498**参数:** 3499 3500| 参数名 | 类型 | 必填 | 说明 | 3501| ------ | ------- | ---- | ------------------------------- | 3502| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3503 3504**返回值:** 3505 3506| 类型 | 说明 | 3507| ---------------------------------------------- | --------------------------- | 3508| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 3509 3510**错误码:** 3511 3512以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3513 3514| 错误码ID | 错误信息 | 3515| -------- | ------------------------------------------------------------ | 3516| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3517| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 3518 3519**示例:** 3520 3521```ts 3522import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3523 3524async function demo() { 3525 let str = 'hello world!'; 3526 let arrayBufferIn = new ArrayBuffer(str.length); 3527 let byteArray = new Uint8Array(arrayBufferIn); 3528 for (let i = 0, j = str.length; i < j; i++) { 3529 byteArray[i] = str.charCodeAt(i) 3530 } 3531 let arrayBufferOut = new ArrayBuffer(100); 3532 let zStream: zlib.ZStream = { 3533 nextIn: arrayBufferIn, 3534 availableIn: 1, 3535 nextOut: arrayBufferOut, 3536 availableOut: 1 3537 }; 3538 let zip = zlib.createZipSync(); 3539 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3540 console.info('deflateInit success') 3541 }).catch((errData: BusinessError) => { 3542 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3543 }) 3544 await zip.deflateResetKeep({ nextOut: arrayBufferOut }).then((data) => { 3545 console.info('deflateResetKeep success') 3546 }).catch((errData: BusinessError) => { 3547 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3548 }) 3549} 3550``` 3551 3552### deflatePending<sup>12+</sup> 3553 3554deflatePending(strm: ZStream): Promise<DeflatePendingOutputInfo> 3555 3556返回已生成但尚未在可用输出中提供的输出的字节数和位数,使用Promise异步返回。成功时返回结果状态、输出位数和输出字节数。 3557 3558**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3559 3560**系统能力:** SystemCapability.BundleManager.Zlib 3561 3562**参数:** 3563 3564| 参数名 | 类型 | 必填 | 说明 | 3565| ------ | ------- | ---- | ------------------------------- | 3566| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3567 3568**返回值:** 3569 3570| 类型 | 说明 | 3571| ------------------------------------------------------------ | ------------------------------------------------- | 3572| Promise<[DeflatePendingOutputInfo](#deflatependingoutputinfo12)> | Promise对象。返回结果状态、输出位数和输出字节数。 | 3573 3574**错误码:** 3575 3576以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3577 3578| 错误码ID | 错误信息 | 3579| -------- | ------------------------------------------------------------ | 3580| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3581| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 3582 3583**示例:** 3584 3585```ts 3586import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3587 3588async function demo() { 3589 let str = 'hello world!'; 3590 let arrayBufferIn = new ArrayBuffer(str.length); 3591 let byteArray = new Uint8Array(arrayBufferIn); 3592 for (let i = 0, j = str.length; i < j; i++) { 3593 byteArray[i] = str.charCodeAt(i) 3594 } 3595 let arrayBufferOut = new ArrayBuffer(100); 3596 let zStream: zlib.ZStream = { 3597 nextIn: arrayBufferIn, 3598 availableIn: 1, 3599 nextOut: arrayBufferOut, 3600 availableOut: 1 3601 }; 3602 let zip = zlib.createZipSync(); 3603 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3604 console.info('deflateInit success') 3605 }).catch((errData: BusinessError) => { 3606 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3607 }) 3608 await zip.deflatePending({ nextOut: arrayBufferOut }).then((data) => { 3609 console.info('deflatePending success') 3610 }).catch((errData: BusinessError) => { 3611 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3612 }) 3613} 3614``` 3615 3616### deflateParams<sup>12+</sup> 3617 3618deflateParams(strm: ZStream, level: CompressLevel, strategy: CompressStrategy): Promise<ReturnStatus> 3619 3620动态更新压缩级别和压缩策略,使用Promise异步返回。成功时返回结果状态。 3621 3622**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3623 3624**系统能力:** SystemCapability.BundleManager.Zlib 3625 3626**参数:** 3627 3628| 参数名 | 类型 | 必填 | 说明 | 3629| -------- | ---------------- | ---- | --------------------------------------------------- | 3630| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3631| level | CompressLevel | 是 | 参考[CompressLevel枚举定义](#compresslevel)。 | 3632| strategy | CompressStrategy | 是 | 参考[CompressStrategy枚举定义](#compressstrategy)。 | 3633 3634**返回值:** 3635 3636| 类型 | 说明 | 3637| ---------------------------------------------- | --------------------------- | 3638| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 3639 3640**错误码:** 3641 3642以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3643 3644| 错误码ID | 错误信息 | 3645| -------- | ------------------------------------------------------------ | 3646| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3647| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 3648 3649**示例:** 3650 3651```ts 3652import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3653 3654async function demo() { 3655 let str = 'hello world!'; 3656 let arrayBufferIn = new ArrayBuffer(str.length); 3657 let byteArray = new Uint8Array(arrayBufferIn); 3658 for (let i = 0, j = str.length; i < j; i++) { 3659 byteArray[i] = str.charCodeAt(i) 3660 } 3661 let arrayBufferOut = new ArrayBuffer(100); 3662 let zStream: zlib.ZStream = { 3663 nextIn: arrayBufferIn, 3664 availableIn: 1, 3665 nextOut: arrayBufferOut, 3666 availableOut: 1 3667 }; 3668 let zip = zlib.createZipSync() 3669 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3670 console.info('deflateInit success') 3671 }).catch((errData: BusinessError) => { 3672 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3673 }) 3674 await zip.deflateParams(zStream, zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY).then((data) => { 3675 console.info('deflateParams success') 3676 }).catch((errData: BusinessError) => { 3677 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3678 }) 3679} 3680``` 3681 3682### deflatePrime<sup>12+</sup> 3683 3684deflatePrime(strm: ZStream, bits: number, value: number): Promise<ReturnStatus> 3685 3686在压缩流中插入位和值,使用Promise异步返回。成功时返回结果状态。 3687 3688**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3689 3690**系统能力:** SystemCapability.BundleManager.Zlib 3691 3692**参数:** 3693 3694| 参数名 | 类型 | 必填 | 说明 | 3695| ------ | ------- | ---- | ------------------------------- | 3696| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3697| bits | number | 是 | 要插入的位数,取值范围在0~16。 | 3698| value | number | 是 | 与位数相对应的位值。 | 3699 3700**返回值:** 3701 3702| 类型 | 说明 | 3703| ---------------------------------------------- | --------------------------- | 3704| Promise<[ReturnStatus](#returnstatus12)> | Promise对象。返回结果状态。 | 3705 3706**错误码:** 3707 3708以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3709 3710| 错误码ID | 错误信息 | 3711| -------- | ------------------------------------------------------------ | 3712| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3713| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 3714 3715**示例:** 3716 3717```ts 3718import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3719 3720async function demo() { 3721 let str = 'hello world!'; 3722 let arrayBufferIn = new ArrayBuffer(str.length); 3723 let byteArray = new Uint8Array(arrayBufferIn); 3724 for (let i = 0, j = str.length; i < j; i++) { 3725 byteArray[i] = str.charCodeAt(i) 3726 } 3727 let arrayBufferOut = new ArrayBuffer(100); 3728 let zStream: zlib.ZStream = { 3729 nextIn: arrayBufferIn, 3730 availableIn: 1, 3731 nextOut: arrayBufferOut, 3732 availableOut: 1 3733 }; 3734 let zip = zlib.createZipSync(); 3735 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3736 console.info('deflateInit success') 3737 }).catch((errData: BusinessError) => { 3738 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3739 }) 3740 await zip.deflatePrime({ nextOut: arrayBufferOut }, 5, 2).then((data) => { 3741 console.info('deflatePrime success') 3742 }).catch((errData: BusinessError) => { 3743 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3744 }) 3745} 3746``` 3747 3748## Options 3749 3750**系统能力:** SystemCapability.BundleManager.Zlib 3751 3752| 名称 | 类型 | 只读 | 可选 | 说明 | 3753| -------- | ---------------- | ---- | ---------------------------------------------------------- | ---- | 3754| level | [CompressLevel](#compresslevel) | 否 | 是 | 参考[CompressLevel枚举定义](#compresslevel)。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 3755| memLevel | [MemLevel](#memlevel) | 否 | 是 | 参考[MemLevel枚举定义](#memlevel)。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 3756| strategy | [CompressStrategy](#compressstrategy) | 否 | 是 | 参考[CompressStrategy枚举定义](#compressstrategy)。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 3757| parallel<sup>18+</sup> | [ParallelStrategy](#parallelstrategy18) | 否 | 是 | 参考[ParallelStrategy枚举定义](#parallelstrategy18)。<br>**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 | 3758 3759## CompressLevel 3760 3761**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3762 3763**系统能力:** SystemCapability.BundleManager.Zlib 3764 3765| 名称 | 值 | 说明 | 3766| ---------------------------------- | ---- | ----------------- | 3767| COMPRESS_LEVEL_NO_COMPRESSION | 0 | 压缩率为0压缩等级。 | 3768| COMPRESS_LEVEL_BEST_SPEED | 1 | 最佳速度压缩等级。 | 3769| COMPRESS_LEVEL_BEST_COMPRESSION | 9 | 最佳压缩等级。 | 3770| COMPRESS_LEVEL_DEFAULT_COMPRESSION | -1 | 默认压缩等级。 | 3771 3772## MemLevel 3773 3774**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3775 3776**系统能力:** SystemCapability.BundleManager.Zlib 3777 3778| 名称 | 值 | 说明 | 3779| ----------------- | ---- | -------------------------------- | 3780| MEM_LEVEL_MIN | 1 | zlib接口在压缩过程中最小使用内存。 | 3781| MEM_LEVEL_MAX | 9 | zlib接口在压缩过程中最大使用内存。 | 3782| MEM_LEVEL_DEFAULT | 8 | zlib接口在压缩过程中默认使用内存。 | 3783 3784## CompressStrategy 3785 3786**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3787 3788**系统能力:** SystemCapability.BundleManager.Zlib 3789 3790| 名称 | 值 | 说明 | 3791| ---------------------------------- | ---- | ------------------------ | 3792| COMPRESS_STRATEGY_DEFAULT_STRATEGY | 0 | 常规数据策略。 | 3793| COMPRESS_STRATEGY_FILTERED | 1 | 过滤器产生的数据压缩策略。 | 3794| COMPRESS_STRATEGY_HUFFMAN_ONLY | 2 | 霍夫曼编码格式压缩策略。 | 3795| COMPRESS_STRATEGY_RLE | 3 | 游标编码压缩策略。 | 3796| COMPRESS_STRATEGY_FIXED | 4 | 固定的压缩策略。 | 3797 3798## ParallelStrategy<sup>18+</sup> 3799 3800**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 3801 3802**系统能力:** SystemCapability.BundleManager.Zlib 3803 3804| 名称 | 值 | 说明 | 3805| ---------------------------------------- | ---- | ------------------------ | 3806| PARALLEL_STRATEGY_SEQUENTIAL | 0 | 默认值,串行压缩/解压策略。| 3807| PARALLEL_STRATEGY_PARALLEL_DECOMPRESSION | 1 | 并行解压策略。 | 3808 3809## ErrorCode<sup>(deprecated)<sup> 3810 3811> **说明:** 3812> 3813> 本模块首批接口从API version 7 开始支持。 3814> 3815> 从API Version 9开始,该模块不再维护。 3816 3817**系统能力:** SystemCapability.BundleManager.Zlib 3818 3819| 名称 | 值 | 说明 | 3820| ---------------- | ---- | ------------ | 3821| ERROR_CODE_OK | 0 | 函数调用成功。 | 3822| ERROR_CODE_ERRNO | -1 | 函数调用失败。 | 3823 3824## CompressFlushMode<sup>12+</sup> 3825 3826**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3827 3828**系统能力:** SystemCapability.BundleManager.Zlib 3829 3830| 名称 | 值 | 说明 | 3831| ------------- | ---- | -------------------------------------------- | 3832| NO_FLUSH | 0 | 默认值,表示正常操作。 | 3833| PARTIAL_FLUSH | 1 | 在流中生成部分刷新点。 | 3834| SYNC_FLUSH | 2 | 在保持压缩流状态的同时强制输出所有压缩数据。 | 3835| FULL_FLUSH | 3 | 重置压缩状态。 | 3836| FINISH | 4 | 压缩或解压缩过程结束。 | 3837| BLOCK | 5 | 允许更精确的控制。 | 3838| TREES | 6 | 实施过程中有特殊目的。 | 3839 3840## CompressMethod<sup>12+</sup> 3841 3842**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3843 3844**系统能力:** SystemCapability.BundleManager.Zlib 3845 3846| 名称 | 值 | 说明 | 3847| -------- | ---- | ---------- | 3848| DEFLATED | 8 | 压缩方法。 | 3849 3850## ReturnStatus<sup>12+</sup> 3851 3852**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3853 3854**系统能力:** SystemCapability.BundleManager.Zlib 3855 3856| 名称 | 值 | 说明 | 3857| ---------- | ---- | ---------------------------------------------- | 3858| OK | 0 | 函数调用成功。 | 3859| STREAM_END | 1 | 函数调用成功,表示已处理了整个数据。 | 3860| NEED_DICT | 2 | 函数调用成功,表示需要预设字典才能继续解压缩。 | 3861 3862## ZStream<sup>12+</sup> 3863 3864**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3865 3866**系统能力:** SystemCapability.BundleManager.Zlib 3867 3868| 名称 | 类型 | 只读 | 可选 | 说明 | 3869| ------------ | ----------- | ---- | ---- | ------------------------------------------------------------ | 3870| nextIn | ArrayBuffer | 否 | 是 | 需要压缩的输入字节。 | 3871| availableIn | number | 否 | 是 | nextIn可用的字节数。 | 3872| totalIn | number | 否 | 是 | 到目前为止读取的输入字节总数。 | 3873| nextOut | ArrayBuffer | 否 | 是 | 压缩后的输出字节。 | 3874| availableOut | number | 否 | 是 | nextOut的剩余可用字节数。 | 3875| totalOut | number | 否 | 是 | 到目前为止输出字节总数。 | 3876| dataType | number | 否 | 是 | 关于数据类型的最佳猜测:deflate的二进制或文本,或inflate的解码状态。 | 3877| adler | number | 否 | 是 | 未压缩数据的Adler-32或CRC-32值。 | 3878 3879## ZipOutputInfo<sup>12+</sup> 3880 3881**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3882 3883**系统能力:** SystemCapability.BundleManager.Zlib 3884 3885| 名称 | 类型 | 只读 | 可选 | 说明 | 3886| ------- | ------------ | ---- | ---- | --------------------------------------------- | 3887| status | ReturnStatus | 否 | 否 | 参考[ReturnStatus枚举定义](#returnstatus12)。 | 3888| destLen | number | 否 | 否 | 目标缓冲区的总长度。 | 3889 3890## DictionaryOutputInfo<sup>12+</sup> 3891 3892**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3893 3894**系统能力:** SystemCapability.BundleManager.Zlib 3895 3896| 名称 | 类型 | 只读 | 可选 | 说明 | 3897| ---------------- | ------------ | ---- | ---- | --------------------------------------------- | 3898| status | ReturnStatus | 否 | 否 | 参考[ReturnStatus枚举定义](#returnstatus12)。 | 3899| dictionaryLength | number | 否 | 否 | 字典的长度。 | 3900 3901## DecompressionOutputInfo<sup>12+</sup> 3902 3903**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3904 3905**系统能力:** SystemCapability.BundleManager.Zlib 3906 3907| 名称 | 类型 | 只读 | 可选 | 说明 | 3908| ------------ | ------------ | ---- | ---- | --------------------------------------------- | 3909| status | ReturnStatus | 否 | 否 | 参考[ReturnStatus枚举定义](#returnstatus12)。 | 3910| destLength | number | 否 | 否 | 目标缓冲区的长度。 | 3911| sourceLength | number | 否 | 否 | 源缓冲区的长度。 | 3912 3913## DeflatePendingOutputInfo<sup>12+</sup> 3914 3915**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3916 3917**系统能力:** SystemCapability.BundleManager.Zlib 3918 3919| 名称 | 类型 | 只读 | 可选 | 说明 | 3920| ------- | ------------ | ---- | ---- | --------------------------------------------- | 3921| status | ReturnStatus | 否 | 否 | 参考[ReturnStatus枚举定义](#returnstatus12)。 | 3922| pending | number | 否 | 否 | 已生成的输出字节数。 | 3923| bits | number | 否 | 否 | 已生成的输出位数。 | 3924 3925## GzHeader<sup>12+</sup> 3926 3927**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3928 3929**系统能力:** SystemCapability.BundleManager.Zlib 3930 3931| 名称 | 类型 | 只读 | 可选 | 说明 | 3932| -------- | ----------- | ---- | ---- | ------------------------------------ | 3933| isText | boolean | 否 | 是 | 如果压缩数据被认为是文本,则为True。 | 3934| os | number | 否 | 是 | 操作系统。 | 3935| time | number | 否 | 是 | 修改时间。 | 3936| xflags | number | 否 | 是 | 额外标志。 | 3937| extra | ArrayBuffer | 否 | 是 | 额外字段。 | 3938| extraLen | number | 否 | 是 | 额外字段的长度。 | 3939| name | ArrayBuffer | 否 | 是 | 文件名。 | 3940| comment | ArrayBuffer | 否 | 是 | 注释。 | 3941| hcrc | boolean | 否 | 是 | 如果存在crc标头,则为True。 | 3942| done | boolean | 否 | 是 | 读取gzip标头后为True。 | 3943 3944## zlib.createGZip<sup>12+</sup> 3945 3946createGZip(): Promise<GZip> 3947 3948创建GZip对象,使用Promise异步返回。成功时返回Gzip对象实例。 3949 3950**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3951 3952**系统能力:** SystemCapability.BundleManager.Zlib 3953 3954**返回值:** 3955 3956| 类型 | 说明 | 3957| ------------------------------ | ------------------------------- | 3958| Promise<[GZip](#gzip12)> | Promise对象。返回GZip对象实例。 | 3959 3960**示例:** 3961 3962```ts 3963import { zlib } from '@kit.BasicServicesKit'; 3964 3965zlib.createGZip().then((data) => { 3966 console.info('createGZip success'); 3967}) 3968``` 3969 3970## zlib.createGZipSync<sup>12+</sup> 3971 3972createGZipSync(): GZip 3973 3974创建GZip对象。成功时返回GZip对象实例。 3975 3976**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3977 3978**系统能力:** SystemCapability.BundleManager.Zlib 3979 3980**返回值:** 3981 3982| 类型 | 说明 | 3983| --------------- | -------------- | 3984| [GZip](#gzip12) | GZip对象实例。 | 3985 3986**示例:** 3987 3988```ts 3989import { zlib } from '@kit.BasicServicesKit'; 3990 3991let gzip = zlib.createGZipSync(); 3992``` 3993 3994## GZip<sup>12+</sup> 3995 3996Gzip相关接口。 3997 3998### gzdopen<sup>12+</sup> 3999 4000gzdopen(fd: number, mode: string): Promise<void> 4001 4002将gzFile与文件描述符fd相关联,打开文件,用于进行读取并解压缩,或者压缩并写入。 4003 4004**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4005 4006**系统能力:** SystemCapability.BundleManager.Zlib 4007 4008**参数:** 4009 4010| 参数名 | 类型 | 必填 | 说明 | 4011| ------ | ------ | ---- | ------------------------------------------------------------ | 4012| fd | number | 是 | 文件描述符。通常情况下,通过系统调用“open”或其他方法获得的。 | 4013| mode | string | 是 | 用于指定访问模式。 | 4014 4015**返回值:** 4016 4017| 类型 | 说明 | 4018| ------------------- | ----------------------- | 4019| Promise<void> | Promise对象,无返回值。 | 4020 4021**错误码:** 4022 4023以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 4024 4025| 错误码ID | 错误信息 | 4026| -------- | ------------------------------------------------------------ | 4027| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4028| 17800002 | No such file or access mode error. | 4029 4030**示例:** 4031 4032```ts 4033import { zlib } from '@kit.BasicServicesKit'; 4034import { fileIo as fs } from '@kit.CoreFileKit'; 4035 4036async function gzdopenDemo(pathDir: string) { 4037 fs.mkdirSync(pathDir + "/gzdopen"); 4038 let path = pathDir + "/gzdopen/test.gz"; 4039 let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 4040 let gzip = zlib.createGZipSync(); 4041 await gzip.gzdopen(file.fd, "wb"); 4042 await gzip.gzclose(); 4043} 4044 4045@Entry 4046@Component 4047struct Index { 4048 build() { 4049 Row() { 4050 Column() { 4051 Button('test gzip interface') 4052 .type(ButtonType.Capsule) 4053 .height(60) 4054 .width(200) 4055 .onClick(() => { 4056 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4057 if (typeof pathDir === 'string') { 4058 gzdopenDemo(pathDir); 4059 } 4060 }) 4061 } 4062 .width('100%') 4063 } 4064 .height('100%') 4065 } 4066} 4067``` 4068 4069### gzbuffer<sup>12+</sup> 4070 4071gzbuffer(size: number):Promise<number> 4072 4073为当前库函数设置内部缓冲区尺寸。 4074 4075**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4076 4077**系统能力:** SystemCapability.BundleManager.Zlib 4078 4079**参数:** 4080 4081| 参数名 | 类型 | 必填 | 说明 | 4082| ------ | ------ | ---- | -------------------------- | 4083| size | number | 是 | 需要设置的内部缓冲区尺寸。 | 4084 4085**返回值:** 4086 4087| 类型 | 说明 | 4088| --------------------- | ---------------------------- | 4089| Promise<number> | Promise对象,成功时,返回0。 | 4090 4091**错误码:** 4092 4093以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 4094 4095| 错误码ID | 错误信息 | 4096| -------- | ------------------------------------------------------------ | 4097| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4098| 17800009 | Internal structure error. | 4099 4100**示例:** 4101 4102```ts 4103import { fileIo as fs } from '@kit.CoreFileKit'; 4104import { zlib } from '@kit.BasicServicesKit'; 4105 4106async function gzbufferDemo(pathDir: string) { 4107 fs.mkdirSync(pathDir + "/gzbuffer"); 4108 let path = pathDir + "/gzbuffer/test.gz"; 4109 let gzip = zlib.createGZipSync(); 4110 await gzip.gzopen(path, "wb"); 4111 await gzip.gzclose(); 4112 await gzip.gzopen(path, "rb"); 4113 let result = await gzip.gzbuffer(648); 4114 await gzip.gzclose(); 4115} 4116 4117@Entry 4118@Component 4119struct Index { 4120 build() { 4121 Row() { 4122 Column() { 4123 Button('test gzip interface') 4124 .type(ButtonType.Capsule) 4125 .height(60) 4126 .width(200) 4127 .onClick(() => { 4128 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4129 if (typeof pathDir === 'string') { 4130 gzbufferDemo(pathDir); 4131 } 4132 }) 4133 } 4134 .width('100%') 4135 } 4136 .height('100%') 4137 } 4138} 4139``` 4140 4141### gzopen<sup>12+</sup> 4142 4143gzopen(path: string, mode: string): Promise<void> 4144 4145打开位于指定路径的gzip(.gz)文件,用于进行读取并解压缩,或者压缩并写入。 4146 4147**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4148 4149**系统能力:** SystemCapability.BundleManager.Zlib 4150 4151**参数:** 4152 4153| 参数名 | 类型 | 必填 | 说明 | 4154| ------ | ------ | ---- | -------------------- | 4155| path | string | 是 | 需要打开的文件路径。 | 4156| mode | string | 是 | 指定文件打开方法。 | 4157 4158**返回值:** 4159 4160| 类型 | 说明 | 4161| ------------------- | ----------------------- | 4162| Promise<void> | Promise对象,无返回值。 | 4163 4164**错误码:** 4165 4166以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 4167 4168| 错误码ID | 错误信息 | 4169| -------- | ------------------------------------------------------------ | 4170| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4171| 17800002 | No such file or access mode error. | 4172 4173**示例:** 4174 4175```ts 4176import { zlib } from '@kit.BasicServicesKit'; 4177import { fileIo as fs } from '@kit.CoreFileKit'; 4178 4179async function gzopenDemo(pathDir: string) { 4180 fs.mkdirSync(pathDir + "/gzopen"); 4181 let path = pathDir + "/gzopen/test.gz"; 4182 let gzip = zlib.createGZipSync(); 4183 await gzip.gzopen(path, "wb"); 4184 await gzip.gzclose(); 4185} 4186 4187@Entry 4188@Component 4189struct Index { 4190 build() { 4191 Row() { 4192 Column() { 4193 Button('test gzip interface') 4194 .type(ButtonType.Capsule) 4195 .height(60) 4196 .width(200) 4197 .onClick(() => { 4198 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4199 if (typeof pathDir === 'string') { 4200 gzopenDemo(pathDir); 4201 } 4202 }) 4203 } 4204 .width('100%') 4205 } 4206 .height('100%') 4207 } 4208} 4209``` 4210 4211### gzeof<sup>12+</sup> 4212 4213gzeof(): Promise<number> 4214 4215检查gzip压缩文件的读取位置是否已到达文件的末尾。 4216 4217**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4218 4219**系统能力:** SystemCapability.BundleManager.Zlib 4220 4221**返回值:** 4222 4223| 类型 | 说明 | 4224| --------------------- | ------------------------------------------------------------ | 4225| Promise<number> | Promise对象,如果在读取时设置了文件的文件结束指示符,则返回1。 | 4226 4227**示例:** 4228 4229```ts 4230import { zlib } from '@kit.BasicServicesKit'; 4231import { fileIo as fs } from '@kit.CoreFileKit'; 4232 4233async function gzeofDemo(pathDir: string) { 4234 fs.mkdirSync(pathDir + "/gzeof"); 4235 let path = pathDir + "/gzeof/test.gz"; 4236 let gzip = zlib.createGZipSync(); 4237 await gzip.gzopen(path, "wb"); 4238 let writeBufferWithData = new ArrayBuffer(16); 4239 let uint8View = new Uint8Array(writeBufferWithData); 4240 for (let i = 0; i < uint8View.length; i++) { 4241 uint8View[i] = i; 4242 } 4243 let writeNum = await gzip.gzwrite(writeBufferWithData, 16) 4244 await gzip.gzclose(); 4245 await gzip.gzopen(path, "rb"); 4246 let readBufferWithData = new ArrayBuffer(20); 4247 let readNum = await gzip.gzread(readBufferWithData); 4248 let eofNum = await gzip.gzeof(); 4249 await gzip.gzclose(); 4250} 4251 4252@Entry 4253@Component 4254struct Index { 4255 build() { 4256 Row() { 4257 Column() { 4258 Button('test gzip interface') 4259 .type(ButtonType.Capsule) 4260 .height(60) 4261 .width(200) 4262 .onClick(() => { 4263 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4264 if (typeof pathDir === 'string') { 4265 gzeofDemo(pathDir); 4266 } 4267 }) 4268 } 4269 .width('100%') 4270 } 4271 .height('100%') 4272 } 4273} 4274``` 4275 4276### gzdirect<sup>12+</sup> 4277 4278gzdirect(): Promise<number> 4279 4280检查指定的gzip文件句柄文件是否直接访问原始未压缩数据,重新分配缓冲区。 4281 4282**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4283 4284**系统能力:** SystemCapability.BundleManager.Zlib 4285 4286**返回值:** 4287 4288| 类型 | 说明 | 4289| --------------------- | -------------------------------------------------- | 4290| Promise<number> | Promise对象,如果直接访问原始未压缩数据,则返回1。 | 4291 4292**示例:** 4293 4294```ts 4295import { zlib } from '@kit.BasicServicesKit'; 4296import { fileIo as fs } from '@kit.CoreFileKit'; 4297 4298async function gzdirectDemo(pathDir: string) { 4299 fs.mkdirSync(pathDir + "/gzdirect"); 4300 let path = pathDir + "/gzdirect/test.gz"; 4301 let gzip = zlib.createGZipSync(); 4302 await gzip.gzopen(path, "wb"); 4303 let directNum = await gzip.gzdirect(); 4304 await gzip.gzclose(); 4305} 4306 4307@Entry 4308@Component 4309struct Index { 4310 build() { 4311 Row() { 4312 Column() { 4313 Button('test gzip interface') 4314 .type(ButtonType.Capsule) 4315 .height(60) 4316 .width(200) 4317 .onClick(() => { 4318 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4319 if (typeof pathDir === 'string') { 4320 gzdirectDemo(pathDir); 4321 } 4322 }) 4323 } 4324 .width('100%') 4325 } 4326 .height('100%') 4327 } 4328} 4329``` 4330 4331### gzclose<sup>12+</sup> 4332 4333gzclose(): Promise<ReturnStatus> 4334 4335清除文件的所有挂起输出,如有必要,关闭文件和释放(解)压缩状态。 4336 4337**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4338 4339**系统能力:** SystemCapability.BundleManager.Zlib 4340 4341**返回值:** 4342 4343| 类型 | 说明 | 4344| ---------------------------------------------- | --------------------------- | 4345| Promise<[ReturnStatus](#returnstatus12)> | Promise对象,返回结果状态。 | 4346 4347**错误码:** 4348 4349以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 4350 4351| 错误码ID | 错误信息 | 4352| -------- | ------------------------- | 4353| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 4354| 17800006 | Memory allocation failed. | 4355 4356**示例:** 4357 4358```ts 4359import { zlib } from '@kit.BasicServicesKit'; 4360import { fileIo as fs } from '@kit.CoreFileKit'; 4361 4362async function gzcloseDemo(pathDir: string) { 4363 fs.mkdirSync(pathDir + "/gzclose"); 4364 let path = pathDir + "/gzclose/test.gz"; 4365 let gzip = zlib.createGZipSync(); 4366 await gzip.gzopen(path, "wb"); 4367 await gzip.gzclose(); 4368} 4369 4370@Entry 4371@Component 4372struct Index { 4373 build() { 4374 Row() { 4375 Column() { 4376 Button('test gzip interface') 4377 .type(ButtonType.Capsule) 4378 .height(60) 4379 .width(200) 4380 .onClick(() => { 4381 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4382 if (typeof pathDir === 'string') { 4383 gzcloseDemo(pathDir); 4384 } 4385 }) 4386 } 4387 .width('100%') 4388 } 4389 .height('100%') 4390 } 4391} 4392``` 4393 4394### gzclearerr<sup>12+</sup> 4395 4396gzclearerr(): Promise<void> 4397 4398清除文件的错误和文件结束标志。 4399 4400**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4401 4402**系统能力:** SystemCapability.BundleManager.Zlib 4403 4404**返回值:** 4405 4406| 类型 | 说明 | 4407| ------------------- | ----------------------- | 4408| Promise<void> | Promise对象,无返回值。 | 4409 4410**示例:** 4411 4412```ts 4413import { zlib } from '@kit.BasicServicesKit'; 4414import { fileIo as fs } from '@kit.CoreFileKit'; 4415 4416async function gzclearerrDemo(pathDir: string) { 4417 fs.mkdirSync(pathDir + "/gzclearerr"); 4418 let path = pathDir + "/gzclearerr/test.gz"; 4419 let gzip = zlib.createGZipSync(); 4420 await gzip.gzopen(path, "wb"); 4421 let writeBufferWithData = new ArrayBuffer(16); 4422 let uint8View = new Uint8Array(writeBufferWithData); 4423 for (let i = 0; i < uint8View.length; i++) { 4424 uint8View[i] = i; 4425 } 4426 let writeNum = await gzip.gzwrite(writeBufferWithData, 16) 4427 await gzip.gzclose(); 4428 await gzip.gzopen(path, "rb"); 4429 let readBufferWithData = new ArrayBuffer(20); 4430 let readNum = await gzip.gzread(readBufferWithData); 4431 let eofNum = await gzip.gzeof(); 4432 await gzip.gzclearerr(); 4433 let eofNumClear = await gzip.gzeof(); 4434 await gzip.gzclose(); 4435} 4436 4437@Entry 4438@Component 4439struct Index { 4440 build() { 4441 Row() { 4442 Column() { 4443 Button('test gzip interface') 4444 .type(ButtonType.Capsule) 4445 .height(60) 4446 .width(200) 4447 .onClick(() => { 4448 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4449 if (typeof pathDir === 'string') { 4450 gzclearerrDemo(pathDir); 4451 } 4452 }) 4453 } 4454 .width('100%') 4455 } 4456 .height('100%') 4457 } 4458} 4459``` 4460 4461### gzerror<sup>12+</sup> 4462 4463gzerror(): Promise<GzErrorOutputInfo> 4464 4465文件上发生的最后一个错误的错误消息。 4466 4467**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4468 4469**系统能力:** SystemCapability.BundleManager.Zlib 4470 4471**返回值:** 4472 4473| 类型 | 说明 | 4474| -------------------------------------------------------- | --------------------------------------------------------- | 4475| Promise<[GzErrorOutputInfo](#gzerroroutputinfo12)> | Promise对象,返回结果状态和出现的最后一个状态的状态消息。 | 4476 4477**错误码:** 4478 4479以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 4480 4481| 错误码ID | 错误信息 | 4482| -------- | -------------- | 4483| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 4484 4485**示例:** 4486 4487```ts 4488import { zlib } from '@kit.BasicServicesKit'; 4489import { fileIo as fs } from '@kit.CoreFileKit'; 4490 4491async function gzerrorDemo(pathDir: string) { 4492 fs.mkdirSync(pathDir + "/gzerror"); 4493 let path = pathDir + "/gzerror/test.gz"; 4494 let gzip = zlib.createGZipSync(); 4495 await gzip.gzopen(path, "wb"); 4496 let writeBufferWithData = new ArrayBuffer(16); 4497 let uint8View = new Uint8Array(writeBufferWithData); 4498 for (let i = 0; i < uint8View.length; i++) { 4499 uint8View[i] = i; 4500 } 4501 try { 4502 await gzip.gzwrite(writeBufferWithData, -1); 4503 } catch (errData) { 4504 await gzip.gzerror().then((GzErrorOutputInfo) => { 4505 console.info('errCode', GzErrorOutputInfo.status); 4506 console.info('errMsg', GzErrorOutputInfo.statusMsg); 4507 }) 4508 } 4509 await gzip.gzclose(); 4510} 4511 4512@Entry 4513@Component 4514struct Index { 4515 build() { 4516 Row() { 4517 Column() { 4518 Button('test gzip interface') 4519 .type(ButtonType.Capsule) 4520 .height(60) 4521 .width(200) 4522 .onClick(() => { 4523 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4524 if (typeof pathDir === 'string') { 4525 gzerrorDemo(pathDir); 4526 } 4527 }) 4528 } 4529 .width('100%') 4530 } 4531 .height('100%') 4532 } 4533} 4534``` 4535 4536### gzgetc<sup>12+</sup> 4537 4538gzgetc(): Promise<number> 4539 4540从文件中读取并解压缩一个字节。 4541 4542**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4543 4544**系统能力:** SystemCapability.BundleManager.Zlib 4545 4546**返回值:** 4547 4548| 类型 | 说明 | 4549| --------------------- | ------------------------------------ | 4550| Promise<number> | Promise对象,返回读取字符的ASCII值。 | 4551 4552**错误码:** 4553 4554以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 4555 4556| 错误码ID | 错误信息 | 4557| -------- | ------------------------- | 4558| 17800009 | Internal structure error. | 4559 4560**示例:** 4561 4562```ts 4563import { zlib } from '@kit.BasicServicesKit'; 4564import { fileIo as fs } from '@kit.CoreFileKit'; 4565 4566async function gzgetcDemo(pathDir: string) { 4567 fs.mkdirSync(pathDir + "/gzgetc"); 4568 let path = pathDir + "/gzgetc/test.gz"; 4569 let gzip = zlib.createGZipSync(); 4570 await gzip.gzopen(path, "wb"); 4571 await gzip.gzputc(1); 4572 await gzip.gzclose(); 4573 await gzip.gzopen(path, "rb"); 4574 let resulit = await gzip.gzgetc(); 4575 await gzip.gzclose(); 4576} 4577 4578@Entry 4579@Component 4580struct Index { 4581 build() { 4582 Row() { 4583 Column() { 4584 Button('test gzip interface') 4585 .type(ButtonType.Capsule) 4586 .height(60) 4587 .width(200) 4588 .onClick(() => { 4589 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4590 if (typeof pathDir === 'string') { 4591 gzgetcDemo(pathDir); 4592 } 4593 }) 4594 } 4595 .width('100%') 4596 } 4597 .height('100%') 4598 } 4599} 4600``` 4601 4602### gzflush<sup>12+</sup> 4603 4604gzflush(flush: CompressFlushMode): Promise<ReturnStatus> 4605 4606将所有挂起的输出刷新到文件中。 4607 4608**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4609 4610**系统能力:** SystemCapability.BundleManager.Zlib 4611 4612**参数:** 4613 4614| 参数名 | 类型 | 必填 | 说明 | 4615| ------ | ----------------- | ---- | ------------------------------------------------------------ | 4616| flush | CompressFlushMode | 是 | 控制刷新操作的行为,参考[CompressFlushMode枚举](#compressflushmode12)的定义。 | 4617 4618**返回值:** 4619 4620| 类型 | 说明 | 4621| ---------------------------------------------- | --------------------------- | 4622| Promise<[ReturnStatus](#returnstatus12)> | Promise对象,返回结果状态。 | 4623 4624**错误码:** 4625 4626以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 4627 4628| 错误码ID | 错误信息 | 4629| -------- | ------------------------------------------------------------ | 4630| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4631| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 4632 4633**示例:** 4634 4635```ts 4636import { zlib } from '@kit.BasicServicesKit'; 4637import { fileIo as fs } from '@kit.CoreFileKit'; 4638 4639async function gzflushDemo(pathDir: string) { 4640 fs.mkdirSync(pathDir + "/gzflush"); 4641 let path = pathDir + "/gzflush/test.gz"; 4642 let gzip = zlib.createGZipSync(); 4643 await gzip.gzopen(path, "wb"); 4644 let flushNum = await gzip.gzflush(zlib.CompressFlushMode.NO_FLUSH); 4645 await gzip.gzclose(); 4646} 4647 4648@Entry 4649@Component 4650struct Index { 4651 build() { 4652 Row() { 4653 Column() { 4654 Button('test gzip interface') 4655 .type(ButtonType.Capsule) 4656 .height(60) 4657 .width(200) 4658 .onClick(() => { 4659 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4660 if (typeof pathDir === 'string') { 4661 gzflushDemo(pathDir); 4662 } 4663 }) 4664 } 4665 .width('100%') 4666 } 4667 .height('100%') 4668 } 4669} 4670``` 4671 4672### gzfwrite<sup>12+</sup> 4673 4674gzfwrite(buf: ArrayBuffer, size: number, nitems: number): Promise<number> 4675 4676将大小为size,数量为nitems的数据块从buf压缩并写入文件。 4677 4678**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4679 4680**系统能力:** SystemCapability.BundleManager.Zlib 4681 4682**参数:** 4683 4684| 参数名 | 类型 | 必填 | 说明 | 4685| ------ | ----------- | ---- | ---------------------- | 4686| buf | ArrayBuffer | 是 | 要将数据写入的缓冲区。 | 4687| size | number | 是 | 单个数据块中的字节数。 | 4688| nitems | number | 是 | 要写入的数据块数。 | 4689 4690**返回值:** 4691 4692| 类型 | 说明 | 4693| --------------------- | --------------------------------------------------- | 4694| Promise<number> | Promise对象,返回写入大小为size的完整数据块的数目。 | 4695 4696**错误码:** 4697 4698以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 4699 4700| 错误码ID | 错误信息 | 4701| -------- | ------------------------------------------------------------ | 4702| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4703| 17800009 | Internal structure error. | 4704 4705**示例:** 4706 4707```ts 4708import { zlib } from '@kit.BasicServicesKit'; 4709import { fileIo as fs } from '@kit.CoreFileKit'; 4710 4711async function gzfwriteDemo(pathDir: string) { 4712 fs.mkdirSync(pathDir + "/gzfwrite"); 4713 let path = pathDir + "/gzfwrite/test.gz"; 4714 let gzip = zlib.createGZipSync(); 4715 await gzip.gzopen(path, "wb"); 4716 let bufferWithData = new ArrayBuffer(16); 4717 let uint8View = new Uint8Array(bufferWithData); 4718 for (let i = 0; i < uint8View.length; i++) { 4719 uint8View[i] = i; 4720 } 4721 let resulit = await gzip.gzfwrite(bufferWithData, 8, 2) 4722 await gzip.gzclose(); 4723} 4724 4725@Entry 4726@Component 4727struct Index { 4728 build() { 4729 Row() { 4730 Column() { 4731 Button('test gzip interface') 4732 .type(ButtonType.Capsule) 4733 .height(60) 4734 .width(200) 4735 .onClick(() => { 4736 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4737 if (typeof pathDir === 'string') { 4738 gzfwriteDemo(pathDir); 4739 } 4740 }) 4741 } 4742 .width('100%') 4743 } 4744 .height('100%') 4745 } 4746} 4747``` 4748 4749### gzfread<sup>12+</sup> 4750 4751gzfread(buf: ArrayBuffer, size: number, nitems: number): Promise<number> 4752 4753从gzip压缩文件中解压缩并读取数据。 4754 4755**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4756 4757**系统能力:** SystemCapability.BundleManager.Zlib 4758 4759**参数:** 4760 4761| 参数名 | 类型 | 必填 | 说明 | 4762| ------ | ----------- | ---- | ------------------------------ | 4763| buf | ArrayBuffer | 是 | 用于存储读取结果的目标缓冲区。 | 4764| size | number | 是 | 单个数据块中的字节数。 | 4765| nitems | number | 是 | 要写入的数据块数。 | 4766 4767**返回值:** 4768 4769| 类型 | 说明 | 4770| --------------------- | --------------------------------------------------- | 4771| Promise<number> | Promise对象,返回读取大小为size的完整数据块的数目。 | 4772 4773**错误码:** 4774 4775以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 4776 4777| 错误码ID | 错误信息 | 4778| -------- | ------------------------------------------------------------ | 4779| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4780| 17800009 | Internal structure error. | 4781 4782**示例:** 4783 4784```ts 4785import { zlib } from '@kit.BasicServicesKit'; 4786import { fileIo as fs } from '@kit.CoreFileKit'; 4787 4788async function gzfreadDemo(pathDir: string) { 4789 fs.mkdirSync(pathDir + "/gzfread"); 4790 let path = pathDir + "/gzfread/test.gz"; 4791 let gzip = zlib.createGZipSync(); 4792 await gzip.gzopen(path, "wb"); 4793 let writeBuffer = new ArrayBuffer(16); 4794 let uint8View = new Uint8Array(writeBuffer); 4795 for (let i = 0; i < uint8View.length; i++) { 4796 uint8View[i] = i; 4797 } 4798 await gzip.gzfwrite(writeBuffer, 8, 2); 4799 await gzip.gzclose(); 4800 await gzip.gzopen(path, "rb"); 4801 let readBuffer = new ArrayBuffer(16); 4802 let result = await gzip.gzfread(readBuffer, 8, 2); 4803 await gzip.gzclose(); 4804} 4805 4806@Entry 4807@Component 4808struct Index { 4809 build() { 4810 Row() { 4811 Column() { 4812 Button('test gzip interface') 4813 .type(ButtonType.Capsule) 4814 .height(60) 4815 .width(200) 4816 .onClick(() => { 4817 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4818 if (typeof pathDir === 'string') { 4819 gzfreadDemo(pathDir); 4820 } 4821 }) 4822 } 4823 .width('100%') 4824 } 4825 .height('100%') 4826 } 4827} 4828``` 4829 4830### gzclosew<sup>12+</sup> 4831 4832gzclosew(): Promise<ReturnStatus> 4833 4834与gzclose()功能相同,仅适用于写入或追加时。 4835 4836**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4837 4838**系统能力:** SystemCapability.BundleManager.Zlib 4839 4840**返回值:** 4841 4842| 类型 | 说明 | 4843| ---------------------------------------------- | --------------------------- | 4844| Promise<[ReturnStatus](#returnstatus12)> | Promise对象,返回结果状态。 | 4845 4846**错误码:** 4847 4848以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 4849 4850| 错误码ID | 错误信息 | 4851| -------- | ------------------------- | 4852| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 4853| 17800006 | Memory allocation failed. | 4854 4855**示例:** 4856 4857```ts 4858import { zlib } from '@kit.BasicServicesKit'; 4859import { fileIo as fs } from '@kit.CoreFileKit'; 4860 4861async function gzclosewDemo(pathDir: string) { 4862 fs.mkdirSync(pathDir + "/gzclosew"); 4863 let path = pathDir + "/gzclosew/test.gz"; 4864 let gzip = zlib.createGZipSync(); 4865 await gzip.gzopen(path, "wb"); 4866 await gzip.gzclosew(); 4867} 4868 4869@Entry 4870@Component 4871struct Index { 4872 build() { 4873 Row() { 4874 Column() { 4875 Button('test gzip interface') 4876 .type(ButtonType.Capsule) 4877 .height(60) 4878 .width(200) 4879 .onClick(() => { 4880 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4881 if (typeof pathDir === 'string') { 4882 gzclosewDemo(pathDir); 4883 } 4884 }) 4885 } 4886 .width('100%') 4887 } 4888 .height('100%') 4889 } 4890} 4891``` 4892 4893### gzcloser<sup>12+</sup> 4894 4895gzcloser(): Promise<ReturnStatus> 4896 4897与gzclose()功能相同,仅适用于读取时。 4898 4899**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4900 4901**系统能力:** SystemCapability.BundleManager.Zlib 4902 4903**返回值:** 4904 4905| 类型 | 说明 | 4906| ---------------------------------------------- | --------------------------- | 4907| Promise<[ReturnStatus](#returnstatus12)> | Promise对象,返回结果状态。 | 4908 4909**错误码:** 4910 4911以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 4912 4913| 错误码ID | 错误信息 | 4914| -------- | -------------- | 4915| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 4916 4917**示例:** 4918 4919```ts 4920import { zlib } from '@kit.BasicServicesKit'; 4921import { fileIo as fs } from '@kit.CoreFileKit'; 4922 4923async function gzcloserDemo(pathDir: string) { 4924 fs.mkdirSync(pathDir + "/gzcloser"); 4925 let path = pathDir + "/gzcloser/test.gz"; 4926 let gzip = zlib.createGZipSync(); 4927 await gzip.gzopen(path, "wb"); 4928 await gzip.gzclose(); 4929 await gzip.gzopen(path, "rb"); 4930 await gzip.gzcloser(); 4931} 4932 4933@Entry 4934@Component 4935struct Index { 4936 build() { 4937 Row() { 4938 Column() { 4939 Button('test gzip interface') 4940 .type(ButtonType.Capsule) 4941 .height(60) 4942 .width(200) 4943 .onClick(() => { 4944 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 4945 if (typeof pathDir === 'string') { 4946 gzcloserDemo(pathDir); 4947 } 4948 }) 4949 } 4950 .width('100%') 4951 } 4952 .height('100%') 4953 } 4954} 4955``` 4956 4957### gzwrite<sup>12+</sup> 4958 4959gzwrite(buf: ArrayBuffer, len: number): Promise<number> 4960 4961将buf中的len长度的未压缩字节进行压缩并将其写入文件。 4962 4963**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4964 4965**系统能力:** SystemCapability.BundleManager.Zlib 4966 4967**参数:** 4968 4969| 参数名 | 类型 | 必填 | 说明 | 4970| ------ | ----------- | ---- | ---------------------------- | 4971| buf | ArrayBuffer | 是 | 对象指向要写入的数据缓冲区。 | 4972| len | number | 是 | 未压缩字节长度。 | 4973 4974**返回值:** 4975 4976| 类型 | 说明 | 4977| --------------------- | ------------------------------------- | 4978| Promise<number> | Promise对象,返回写入的未压缩字节数。 | 4979 4980**错误码:** 4981 4982以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 4983 4984| 错误码ID | 错误信息 | 4985| -------- | ------------------------------------------------------------ | 4986| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4987| 17800009 | Internal structure error. | 4988 4989**示例:** 4990 4991```ts 4992import { zlib } from '@kit.BasicServicesKit'; 4993import { fileIo as fs } from '@kit.CoreFileKit'; 4994 4995async function gzwriteDemo(pathDir: string) { 4996 fs.mkdirSync(pathDir + "/gzwrite"); 4997 let path = pathDir + "/gzwrite/test.gz"; 4998 let gzip = zlib.createGZipSync(); 4999 await gzip.gzopen(path, "wb"); 5000 let bufferWithData = new ArrayBuffer(16); 5001 let uint8View = new Uint8Array(bufferWithData); 5002 for (let i = 0; i < uint8View.length; i++) { 5003 uint8View[i] = i; 5004 } 5005 let result = await gzip.gzwrite(bufferWithData, 16); 5006 await gzip.gzclose(); 5007} 5008 5009@Entry 5010@Component 5011struct Index { 5012 build() { 5013 Row() { 5014 Column() { 5015 Button('test gzip interface') 5016 .type(ButtonType.Capsule) 5017 .height(60) 5018 .width(200) 5019 .onClick(() => { 5020 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 5021 if (typeof pathDir === 'string') { 5022 gzwriteDemo(pathDir); 5023 } 5024 }) 5025 } 5026 .width('100%') 5027 } 5028 .height('100%') 5029 } 5030} 5031``` 5032 5033### gzungetc<sup>12+</sup> 5034 5035gzungetc(c: number): Promise<number> 5036 5037将c推回到流中,以便在下次读取文件时将作为第一个字符读取。 5038 5039**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5040 5041**系统能力:** SystemCapability.BundleManager.Zlib 5042 5043**参数:** 5044 5045| 参数名 | 类型 | 必填 | 说明 | 5046| ------ | ------ | ---- | ------------------------ | 5047| c | number | 是 | 回退到输入流之前的字符。 | 5048 5049**返回值:** 5050 5051| 类型 | 说明 | 5052| --------------------- | ----------------------------- | 5053| Promise<number> | Promise对象,返回推送的字符。 | 5054 5055**错误码:** 5056 5057以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5058 5059| 错误码ID | 错误信息 | 5060| -------- | ------------------------------------------------------------ | 5061| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5062| 17800009 | Internal structure error. | 5063 5064**示例:** 5065 5066```ts 5067import { zlib } from '@kit.BasicServicesKit'; 5068import { fileIo as fs } from '@kit.CoreFileKit'; 5069 5070async function gzungetcDemo(pathDir: string) { 5071 fs.mkdirSync(pathDir + "/gzungetc"); 5072 let path = pathDir + "/gzungetc/test.gz"; 5073 let gzip = zlib.createGZipSync(); 5074 await gzip.gzopen(path, "wb"); 5075 await gzip.gzclose(); 5076 await gzip.gzopen(path, "rb"); 5077 await gzip.gzread(new ArrayBuffer(1)); 5078 let result = await gzip.gzungetc(1); 5079 await gzip.gzclose(); 5080} 5081 5082@Entry 5083@Component 5084struct Index { 5085 build() { 5086 Row() { 5087 Column() { 5088 Button('test gzip interface') 5089 .type(ButtonType.Capsule) 5090 .height(60) 5091 .width(200) 5092 .onClick(() => { 5093 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 5094 if (typeof pathDir === 'string') { 5095 gzungetcDemo(pathDir); 5096 } 5097 }) 5098 } 5099 .width('100%') 5100 } 5101 .height('100%') 5102 } 5103} 5104``` 5105 5106### gztell<sup>12+</sup> 5107 5108gztell(): Promise<number> 5109 5110返回文件中下一个gzread或gzwrite的起始位置。 5111 5112**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5113 5114**系统能力:** SystemCapability.BundleManager.Zlib 5115 5116**返回值:** 5117 5118| 类型 | 说明 | 5119| --------------------- | -------------------------------------------------------- | 5120| Promise<number> | Promise对象,返回文件种下一个gzread或gzwrite的起始位置。 | 5121 5122**错误码:** 5123 5124以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 5125 5126| 错误码ID | 错误信息 | 5127| -------- | ------------------------- | 5128| 17800009 | Internal structure error. | 5129 5130**示例:** 5131 5132```ts 5133import { zlib } from '@kit.BasicServicesKit'; 5134import { fileIo as fs } from '@kit.CoreFileKit'; 5135 5136async function gztellDemo(pathDir: string) { 5137 fs.mkdirSync(pathDir + "/gztell"); 5138 let path = pathDir + "/gztell/test.gz"; 5139 let gzip = zlib.createGZipSync(); 5140 await gzip.gzopen(path, "wb"); 5141 let result = await gzip.gztell(); 5142 await gzip.gzclose(); 5143} 5144 5145@Entry 5146@Component 5147struct Index { 5148 build() { 5149 Row() { 5150 Column() { 5151 Button('test gzip interface') 5152 .type(ButtonType.Capsule) 5153 .height(60) 5154 .width(200) 5155 .onClick(() => { 5156 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 5157 if (typeof pathDir === 'string') { 5158 gztellDemo(pathDir); 5159 } 5160 }) 5161 } 5162 .width('100%') 5163 } 5164 .height('100%') 5165 } 5166} 5167``` 5168 5169### gzsetparams<sup>12+</sup> 5170 5171gzsetparams(level: CompressLevel, strategy: CompressStrategy): Promise<ReturnStatus> 5172 5173动态更新文件的压缩级别和压缩策略。 5174 5175**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5176 5177**系统能力:** SystemCapability.BundleManager.Zlib 5178 5179**参数:** 5180 5181| 参数名 | 类型 | 必填 | 说明 | 5182| -------- | ---------------- | ---- | ------------------------------------------------------------ | 5183| level | CompressLevel | 是 | 压缩级别,参考[CompressLevel枚举定义](#compresslevel)。 | 5184| strategy | CompressStrategy | 是 | 压缩策略,参考[CompressStrategy枚举定义](#compressstrategy)。 | 5185 5186**返回值:** 5187 5188| 类型 | 说明 | 5189| ---------------------------------------------- | --------------------------- | 5190| Promise<[ReturnStatus](#returnstatus12)> | Promise对象,返回结果状态。 | 5191 5192**错误码:** 5193 5194以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5195 5196| 错误码ID | 错误信息 | 5197| -------- | ------------------------------------------------------------ | 5198| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5199| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 5200 5201**示例:** 5202 5203```ts 5204import { zlib } from '@kit.BasicServicesKit'; 5205import { fileIo as fs } from '@kit.CoreFileKit'; 5206 5207async function gzsetparamsDemo(pathDir: string) { 5208 fs.mkdirSync(pathDir + "/gzsetparams"); 5209 let path = pathDir + "/gzsetparams/test.gz"; 5210 let gzip = zlib.createGZipSync(); 5211 await gzip.gzopen(path, "wb"); 5212 let result = await gzip.gzsetparams(zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 5213 zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY); 5214 await gzip.gzclose(); 5215} 5216 5217@Entry 5218@Component 5219struct Index { 5220 build() { 5221 Row() { 5222 Column() { 5223 Button('test gzip interface') 5224 .type(ButtonType.Capsule) 5225 .height(60) 5226 .width(200) 5227 .onClick(() => { 5228 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 5229 if (typeof pathDir === 'string') { 5230 gzsetparamsDemo(pathDir); 5231 } 5232 }) 5233 } 5234 .width('100%') 5235 } 5236 .height('100%') 5237 } 5238} 5239``` 5240 5241### gzseek<sup>12+</sup> 5242 5243gzseek(offset: number, whence: OffsetReferencePoint): Promise<number> 5244 5245将起始位置设置为相对于文件中下一个gzread或gzwrite的偏移位置。 5246 5247**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5248 5249**系统能力:** SystemCapability.BundleManager.Zlib 5250 5251**参数:** 5252 5253| 参数名 | 类型 | 必填 | 说明 | 5254| ------ | -------------------- | ---- | ------------------------------------------------------------ | 5255| offset | number | 是 | 目标偏移位置。 | 5256| whence | OffsetReferencePoint | 是 | 定义偏移的参考点,参考[OffsetReferencePoint枚举定义](#offsetreferencepoint12)。 | 5257 5258**返回值:** 5259 5260| 类型 | 说明 | 5261| --------------------- | ------------------------------------------------------------ | 5262| Promise<number> | Promise对象,返回从未压缩流开始以字节为单位测量的结果偏移位置。 | 5263 5264**错误码:** 5265 5266以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5267 5268| 错误码ID | 错误信息 | 5269| -------- | ------------------------------------------------------------ | 5270| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5271| 17800009 | Internal structure error. | 5272 5273**示例:** 5274 5275```ts 5276import { zlib } from '@kit.BasicServicesKit'; 5277import { fileIo as fs } from '@kit.CoreFileKit'; 5278 5279async function gzseekDemo(pathDir: string) { 5280 fs.mkdirSync(pathDir + "/gzseek"); 5281 let path = pathDir + "/gzseek/test.gz"; 5282 let gzip = zlib.createGZipSync(); 5283 await gzip.gzopen(path, "wb"); 5284 let result = await gzip.gzseek(2, zlib.OffsetReferencePoint.SEEK_CUR); 5285 await gzip.gzclose(); 5286} 5287 5288@Entry 5289@Component 5290struct Index { 5291 build() { 5292 Row() { 5293 Column() { 5294 Button('test gzip interface') 5295 .type(ButtonType.Capsule) 5296 .height(60) 5297 .width(200) 5298 .onClick(() => { 5299 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 5300 if (typeof pathDir === 'string') { 5301 gzseekDemo(pathDir); 5302 } 5303 }) 5304 } 5305 .width('100%') 5306 } 5307 .height('100%') 5308 } 5309} 5310``` 5311 5312### gzrewind<sup>12+</sup> 5313 5314gzrewind(): Promise<ReturnStatus> 5315 5316将文件指针重新定位到文件的开头,此功能仅用于读取。 5317 5318**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5319 5320**系统能力:** SystemCapability.BundleManager.Zlib 5321 5322**返回值:** 5323 5324| 类型 | 说明 | 5325| ---------------------------------------------- | --------------------------- | 5326| Promise<[ReturnStatus](#returnstatus12)> | Promise对象,返回结果状态。 | 5327 5328**错误码:** 5329 5330以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 5331 5332| 错误码ID | 错误信息 | 5333| -------- | ------------------------- | 5334| 17800009 | Internal structure error. | 5335 5336**示例:** 5337 5338```ts 5339import { zlib } from '@kit.BasicServicesKit'; 5340import { fileIo as fs } from '@kit.CoreFileKit'; 5341 5342async function gzrewindDemo(pathDir: string) { 5343 fs.mkdirSync(pathDir + "/gzrewind"); 5344 let path = pathDir + "/gzrewind/test.gz"; 5345 let gzip = zlib.createGZipSync(); 5346 await gzip.gzopen(path, "wb"); 5347 await gzip.gzclose(); 5348 await gzip.gzopen(path, "rb"); 5349 let result = await gzip.gzrewind(); 5350 await gzip.gzclose(); 5351} 5352 5353@Entry 5354@Component 5355struct Index { 5356 build() { 5357 Row() { 5358 Column() { 5359 Button('test gzip interface') 5360 .type(ButtonType.Capsule) 5361 .height(60) 5362 .width(200) 5363 .onClick(() => { 5364 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 5365 if (typeof pathDir === 'string') { 5366 gzrewindDemo(pathDir); 5367 } 5368 }) 5369 } 5370 .width('100%') 5371 } 5372 .height('100%') 5373 } 5374} 5375``` 5376 5377### gzread<sup>12+</sup> 5378 5379gzread(buf: ArrayBuffer): Promise<number> 5380 5381从文件中读取最多len个未压缩字节并将其解压缩到buf中。 5382 5383**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5384 5385**系统能力:** SystemCapability.BundleManager.Zlib 5386 5387**参数:** 5388 5389| 参数名 | 类型 | 必填 | 说明 | 5390| ------ | ----------- | ---- | -------------- | 5391| buf | ArrayBuffer | 是 | 目标偏移位置。 | 5392 5393**返回值:** 5394 5395| 类型 | 说明 | 5396| --------------------- | ----------------------------------------- | 5397| Promise<number> | Promise对象,返回实际读取的未压缩字节数。 | 5398 5399**错误码:** 5400 5401以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5402 5403| 错误码ID | 错误信息 | 5404| -------- | ------------------------------------------------------------ | 5405| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5406| 17800009 | Internal structure error. | 5407 5408**示例:** 5409 5410```ts 5411import { zlib } from '@kit.BasicServicesKit'; 5412import { fileIo as fs } from '@kit.CoreFileKit'; 5413 5414async function gzreadDemo(pathDir: string) { 5415 fs.mkdirSync(pathDir + "/gzread"); 5416 let path = pathDir + "/gzread/test.gz"; 5417 let gzip = zlib.createGZipSync(); 5418 await gzip.gzopen(path, "wb"); 5419 let writeBuffer = new ArrayBuffer(16); 5420 let uint8View = new Uint8Array(writeBuffer); 5421 for (let i = 0; i < uint8View.length; i++) { 5422 uint8View[i] = i; 5423 } 5424 await gzip.gzwrite(writeBuffer, 16); 5425 await gzip.gzclose(); 5426 await gzip.gzopen(path, "rb"); 5427 let readBuffer = new ArrayBuffer(16); 5428 let result = await gzip.gzread(readBuffer); 5429 await gzip.gzclose(); 5430} 5431 5432@Entry 5433@Component 5434struct Index { 5435 build() { 5436 Row() { 5437 Column() { 5438 Button('test gzip interface') 5439 .type(ButtonType.Capsule) 5440 .height(60) 5441 .width(200) 5442 .onClick(() => { 5443 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 5444 if (typeof pathDir === 'string') { 5445 gzreadDemo(pathDir); 5446 } 5447 }) 5448 } 5449 .width('100%') 5450 } 5451 .height('100%') 5452 } 5453} 5454``` 5455 5456### gzputs<sup>12+</sup> 5457 5458gzputs(str: string): Promise<number> 5459 5460压缩给定的以null结尾的字符串并将其写入文件,不包括终止的null字符。 5461 5462**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5463 5464**系统能力:** SystemCapability.BundleManager.Zlib 5465 5466**参数:** 5467 5468| 参数名 | 类型 | 必填 | 说明 | 5469| ------ | ------ | ---- | ---------------------- | 5470| str | string | 是 | 格式化描述符和纯文本。 | 5471 5472**返回值:** 5473 5474| 类型 | 说明 | 5475| --------------------- | ------------------------------- | 5476| Promise<number> | Promise对象,返回写入的字符数。 | 5477 5478**错误码:** 5479 5480以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5481 5482| 错误码ID | 错误信息 | 5483| -------- | ------------------------------------------------------------ | 5484| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5485| 17800009 | Internal structure error. | 5486 5487**示例:** 5488 5489```ts 5490import { zlib } from '@kit.BasicServicesKit'; 5491import { fileIo as fs } from '@kit.CoreFileKit'; 5492 5493async function gzputsDemo(pathDir: string) { 5494 fs.mkdirSync(pathDir + "/gzputs"); 5495 let path = pathDir + "/gzputs/test.gz"; 5496 let gzip = zlib.createGZipSync(); 5497 await gzip.gzopen(path, "wb"); 5498 let result = await gzip.gzputs("hello"); 5499 await gzip.gzclose(); 5500} 5501 5502@Entry 5503@Component 5504struct Index { 5505 build() { 5506 Row() { 5507 Column() { 5508 Button('test gzip interface') 5509 .type(ButtonType.Capsule) 5510 .height(60) 5511 .width(200) 5512 .onClick(() => { 5513 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 5514 if (typeof pathDir === 'string') { 5515 gzputsDemo(pathDir); 5516 } 5517 }) 5518 } 5519 .width('100%') 5520 } 5521 .height('100%') 5522 } 5523} 5524``` 5525 5526### gzputc<sup>12+</sup> 5527 5528gzputc(char: number): Promise<number> 5529 5530将转换为无符号字符的c压缩并写入文件。 5531 5532**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5533 5534**系统能力:** SystemCapability.BundleManager.Zlib 5535 5536**参数:** 5537 5538| 参数名 | 类型 | 必填 | 说明 | 5539| ------ | ------ | ---- | --------------- | 5540| char | number | 是 | 写入字符ASCII。 | 5541 5542**返回值:** 5543 5544| 类型 | 说明 | 5545| --------------------- | ----------------------------- | 5546| Promise<number> | Promise对象,返回已写入的值。 | 5547 5548**错误码:** 5549 5550以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5551 5552| 错误码ID | 错误信息 | 5553| -------- | ------------------------------------------------------------ | 5554| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5555| 17800009 | Internal structure error. | 5556 5557**示例:** 5558 5559```ts 5560import { zlib } from '@kit.BasicServicesKit'; 5561import { fileIo as fs } from '@kit.CoreFileKit'; 5562 5563async function gzputcDemo(pathDir: string) { 5564 fs.mkdirSync(pathDir + "/gzputc"); 5565 let path = pathDir + "/gzputc/test.gz"; 5566 let gzip = zlib.createGZipSync(); 5567 await gzip.gzopen(path, "wb"); 5568 let result = await gzip.gzputc(0); 5569 await gzip.gzclose(); 5570} 5571 5572@Entry 5573@Component 5574struct Index { 5575 build() { 5576 Row() { 5577 Column() { 5578 Button('test gzip interface') 5579 .type(ButtonType.Capsule) 5580 .height(60) 5581 .width(200) 5582 .onClick(() => { 5583 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 5584 if (typeof pathDir === 'string') { 5585 gzputcDemo(pathDir); 5586 } 5587 }) 5588 } 5589 .width('100%') 5590 } 5591 .height('100%') 5592 } 5593} 5594``` 5595 5596### gzprintf<sup>12+</sup> 5597 5598gzprintf(format: string, ...args: Array<string | number>): Promise<number> 5599 5600在字符串格式的控制下,将参数转换和格式化后,压缩并写入文件,如fprintf中所示。 5601 5602**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5603 5604**系统能力:** SystemCapability.BundleManager.Zlib 5605 5606**参数:** 5607 5608| 参数名 | 类型 | 必填 | 说明 | 5609| ------ | ----------------------------- | ---- | ---------------------- | 5610| format | string | 是 | 格式化描述符和纯文本。 | 5611| ...args | Array<string \| number> | 否 | 可变参数列表。 | 5612 5613**返回值:** 5614 5615| 类型 | 说明 | 5616| --------------------- | ----------------------------------------- | 5617| Promise<number> | Promise对象,返回实际写入的未压缩字节数。 | 5618 5619**错误码:** 5620 5621以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5622 5623| 错误码ID | 错误信息 | 5624| -------- | ------------------------------------------------------------ | 5625| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5626| 17800004 | Compression or decompression stream error, which may be caused by an initialization error in the zlib stream structure or a modified structure. | 5627| 17800009 | Internal structure error. | 5628 5629**示例:** 5630 5631```ts 5632import { zlib } from '@kit.BasicServicesKit'; 5633import { fileIo as fs } from '@kit.CoreFileKit'; 5634 5635async function gzprintfDemo(pathDir: string) { 5636 fs.mkdirSync(pathDir + "/gzprintf"); 5637 let path = pathDir + "/gzprintf/test.gz"; 5638 let gzip = zlib.createGZipSync(); 5639 await gzip.gzopen(path, "wb"); 5640 let result = await gzip.gzprintf("name is %s, age is %d", "Tom", 23); 5641 await gzip.gzclose(); 5642} 5643 5644@Entry 5645@Component 5646struct Index { 5647 build() { 5648 Row() { 5649 Column() { 5650 Button('test gzip interface') 5651 .type(ButtonType.Capsule) 5652 .height(60) 5653 .width(200) 5654 .onClick(() => { 5655 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 5656 if (typeof pathDir === 'string') { 5657 gzprintfDemo(pathDir); 5658 } 5659 }) 5660 } 5661 .width('100%') 5662 } 5663 .height('100%') 5664 } 5665} 5666``` 5667 5668### gzoffset<sup>12+</sup> 5669 5670gzoffset(): Promise<number> 5671 5672返回文件的当前压缩(实际)读或写偏移量。 5673 5674**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5675 5676**系统能力:** SystemCapability.BundleManager.Zlib 5677 5678**返回值:** 5679 5680| 类型 | 说明 | 5681| --------------------- | ----------------------------------------------------- | 5682| Promise<number> | Promise对象,返回文件的当前压缩(实际)读或写偏移量。 | 5683 5684**错误码:** 5685 5686以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 5687 5688| 错误码ID | 错误信息 | 5689| -------- | ------------------------- | 5690| 17800009 | Internal structure error. | 5691 5692**示例:** 5693 5694```ts 5695import { zlib } from '@kit.BasicServicesKit'; 5696import { fileIo as fs } from '@kit.CoreFileKit'; 5697 5698async function gzoffsetDemo(pathDir: string) { 5699 fs.mkdirSync(pathDir + "/gzoffset"); 5700 let path = pathDir + "/gzoffset/test.gz"; 5701 let gzip = zlib.createGZipSync(); 5702 await gzip.gzopen(path, "wb"); 5703 let result = await gzip.gzoffset(); 5704 await gzip.gzclose(); 5705} 5706 5707@Entry 5708@Component 5709struct Index { 5710 build() { 5711 Row() { 5712 Column() { 5713 Button('test gzip interface') 5714 .type(ButtonType.Capsule) 5715 .height(60) 5716 .width(200) 5717 .onClick(() => { 5718 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 5719 if (typeof pathDir === 'string') { 5720 gzoffsetDemo(pathDir); 5721 } 5722 }) 5723 } 5724 .width('100%') 5725 } 5726 .height('100%') 5727 } 5728} 5729``` 5730 5731### gzgets<sup>12+</sup> 5732 5733gzgets(buf: ArrayBuffer): Promise<string> 5734 5735从文件中读取字节并将其解压缩到buf中,直到读取len-1字符,或者直到读取换行符并将其传输到buf,或者遇到文件结束条件。 5736 5737**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5738 5739**系统能力:** SystemCapability.BundleManager.Zlib 5740 5741**参数:** 5742 5743| 参数名 | 类型 | 必填 | 说明 | 5744| ------ | ----------- | ---- | ------------------ | 5745| buf | ArrayBuffer | 是 | 存储读取的行数据。 | 5746 5747**返回值:** 5748 5749| 类型 | 说明 | 5750| --------------------- | ------------------------------------- | 5751| Promise<string> | Promise对象,返回以null结尾的字符串。 | 5752 5753**错误码:** 5754 5755以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5756 5757| 错误码ID | 错误信息 | 5758| -------- | ------------------------------------------------------------ | 5759| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5760| 17800009 | Internal structure error. | 5761 5762**示例:** 5763 5764```ts 5765import { zlib } from '@kit.BasicServicesKit'; 5766import { fileIo as fs } from '@kit.CoreFileKit'; 5767 5768async function gzgetsDemo(pathDir: string) { 5769 fs.mkdirSync(pathDir + "/gzgets"); 5770 let path = pathDir + "/gzgets/test.gz"; 5771 let gzip = zlib.createGZipSync(); 5772 await gzip.gzopen(path, "wb"); 5773 await gzip.gzputs("hello"); 5774 await gzip.gzclose(); 5775 await gzip.gzopen(path, "rb"); 5776 let bufferWithData = new ArrayBuffer(16); 5777 let result = await gzip.gzgets(bufferWithData); 5778 await gzip.gzclose(); 5779} 5780 5781@Entry 5782@Component 5783struct Index { 5784 build() { 5785 Row() { 5786 Column() { 5787 Button('test gzip interface') 5788 .type(ButtonType.Capsule) 5789 .height(60) 5790 .width(200) 5791 .onClick(() => { 5792 let pathDir = this.getUIContext()?.getHostContext()?.cacheDir; 5793 if (typeof pathDir === 'string') { 5794 gzgetsDemo(pathDir); 5795 } 5796 }) 5797 } 5798 .width('100%') 5799 } 5800 .height('100%') 5801 } 5802} 5803``` 5804 5805## GzErrorOutputInfo<sup>12+</sup> 5806 5807**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5808 5809**系统能力:** SystemCapability.BundleManager.Zlib 5810 5811| 名称 | 类型 | 只读 | 可选 | 说明 | 5812| --------- | ------------ | ---- | ---- | -------------------------------------------- | 5813| status | ReturnStatus | 否 | 否 | 返回zlib文件状态码,参考ReturnStatus的定义。 | 5814| statusMsg | string | 否 | 否 | zlib文件上发生的最后一个状态的状态消息。 | 5815 5816## OffsetReferencePoint<sup>12+</sup> 5817 5818**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5819 5820**系统能力:** SystemCapability.BundleManager.Zlib 5821 5822| 名称 | 值 | 说明 | 5823| -------- | ---- | ---------------- | 5824| SEEK_SET | 0 | 从文件开头查找。 | 5825| SEEK_CUR | 1 | 从当前位置查找。 |