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