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