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, arrayBufferOut, 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 | 是 | 参考[zip.CompressLevel枚举定义](#zipcompresslevel)。 | 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](#zipreturnstatus12)> | 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](#zipreturnstatus12)> | 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: arrayBufferOut, 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](#zipreturnstatus12)> | 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](#zipreturnstatus12)> | 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: arrayBufferOut, 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](#zipreturnstatus12)> | 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](#zipreturnstatus12)> | 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: arrayBufferOut, 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](#zipreturnstatus12)> | 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: arrayBufferOut, 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](#zipreturnstatus12)> | 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: arrayBufferOut, 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: arrayBufferOut, 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: arrayBufferOut, 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](#zipreturnstatus12)> | 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](#zipreturnstatus12)> | 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: arrayBufferOut, 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](#zipreturnstatus12)> | 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: arrayBufferOut, 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](#zipreturnstatus12)> | 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: arrayBufferOut, 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](#zipreturnstatus12)> | 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: arrayBufferOut, 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: arrayBufferOut, 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](#zipreturnstatus12)> | 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**示例:**参考[inflateBack](#inflateback12) 2404 2405### inflateBackEnd<sup>12+</sup> 2406 2407inflateBackEnd(strm: ZStream): Promise<ReturnStatus> 2408 2409inflateBackInit()函数分配的所有内存都被释放,使用Promise异步返回。成功时返回结果状态。 2410 2411**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2412 2413**系统能力:** SystemCapability.BundleManager.Zlib 2414 2415**参数:** 2416 2417| 参数名 | 类型 | 必填 | 说明 | 2418| ------ | ------- | ---- | ------------------------------- | 2419| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2420 2421**返回值:** 2422 2423| 类型 | 说明 | 2424| ------------------------------------------------- | --------------------------- | 2425| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 2426 2427**错误码:** 2428 2429以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2430 2431| 错误码ID | 错误信息 | 2432| -------- | ------------------------------------------------------------ | 2433| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2434| 17800004 | ZStream error. | 2435 2436**示例:**参考[inflateBack](#inflateback12) 2437 2438### inflateBack<sup>12+</sup> 2439 2440inflateBack(strm: ZStream, backIn: InflateBackInputCallback, inDesc: object, backOut: InflateBackOutputCallback, outDesc: object): Promise<ReturnStatus> 2441 2442实现原始解压缩,采用回调接口来处理输入和输出,使用Promise异步返回。成功时返回结果状态。 2443 2444**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2445 2446**系统能力:** SystemCapability.BundleManager.Zlib 2447 2448**参数:** 2449 2450| 参数名 | 类型 | 必填 | 说明 | 2451| ------- | ------------------------- | ---- | ------------------------------------------------------------ | 2452| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2453| backIn | InflateBackInputCallback | 是 | 一种函数,用于从末尾解压缩数据,以从输入源读取原始压缩数据。 | 2454| inDesc | object | 是 | 通用对象。 | 2455| backOut | InflateBackOutputCallback | 是 | 将解压缩的数据写入目标输出。 | 2456| outDesc | object | 是 | 通用对象。 | 2457 2458InflateBackInputCallback的说明: 2459 2460InflateBackInputCallback = (inDesc: object) => ArrayBuffer 2461 2462| 名称 | 类型 | 必填 | 说明 | 2463| ------ | ------ | ---- | ---------------- | 2464| inDesc | object | 是 | 用户定义数据对象 | 2465 2466InflateBackOutputCallback的说明: 2467 2468InflateBackOutputCallback = (outDesc: object, buf: ArrayBuffer, length: number) => number 2469 2470| 名称 | 类型 | 必填 | 说明 | 2471| ------- | ----------- | ---- | ---------------------- | 2472| outDesc | object | 是 | 用户定义数据对象 | 2473| buf | ArrayBuffer | 是 | 用于存储要写入的数据。 | 2474| length | number | 是 | 写入输出缓冲区的长度。 | 2475 2476**返回值:** 2477 2478| 类型 | 说明 | 2479| ------------------------------------------------- | --------------------------- | 2480| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 2481 2482**错误码:** 2483 2484以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2485 2486| 错误码ID | 错误信息 | 2487| -------- | ------------------------------------------------------------ | 2488| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2489| 17800004 | ZStream error. | 2490 2491**示例:** 2492 2493```ts 2494import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2495 2496async function demo() { 2497 let readIn: (inDesc: object) => ArrayBuffer = (inDesc: object): ArrayBuffer => { 2498 console.info("inDesc = ", JSON.stringify(inDesc)); 2499 let buffer = new ArrayBuffer(26) 2500 let array = new Uint8Array(buffer); 2501 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]); 2502 return buffer; 2503 } 2504 2505 let writeOut: (outDesc: object, buffer: ArrayBuffer, length: number) => number = (outDesc: object, buffer: ArrayBuffer, length: number): number => { 2506 console.info("outDesc = ", outDesc); 2507 console.info("buffer = ", buffer); 2508 console.info("length = ", length); 2509 let array = new Uint8Array(buffer); 2510 let dataString = ""; 2511 for (let i = 0; i < length; i++) { 2512 dataString += String.fromCharCode(array[i]); 2513 } 2514 console.info('writeOut ', dataString); 2515 return 0; 2516 } 2517 2518 let have = 0; 2519 let first = 1; 2520 let arrayBuffer = new ArrayBuffer(26); 2521 let next = new Uint8Array(arrayBuffer); 2522 let last = 0; 2523 let index = 0; 2524 let flags = 0; 2525 let NEXT2: () => number = (): number => { 2526 let o6: object = new Object() 2527 if (!have) { 2528 arrayBuffer = readIn(o6) 2529 next = new Uint8Array(arrayBuffer); 2530 console.info('readIn next = ', next.length) 2531 have = next.length; 2532 } 2533 if (have) { 2534 have--; 2535 last = next[index]; 2536 index++; 2537 } 2538 else { 2539 last = -1; 2540 } 2541 return last; 2542 } 2543 2544 let inflateBackTest: () => void = (async () => { 2545 try { 2546 have = 0; 2547 first = 1; 2548 arrayBuffer = new ArrayBuffer(26); 2549 next = new Uint8Array(arrayBuffer); 2550 last = 0; 2551 index = 0; 2552 flags = 0; 2553 let sr = zlib.createZipSync(); 2554 let buffer = new ArrayBuffer(1024) 2555 await sr.inflateBackInit({}, 15, buffer).then((result) => { 2556 console.info('inflateBackInit Call result res', result) 2557 }) 2558 let ret = 0; 2559 for (; ;) { 2560 if (NEXT2() == -1) { 2561 ret = 0; 2562 console.info('inflateBackTest Call result NEXT2() == -1') 2563 break; 2564 } 2565 console.info('have = last = ', have, last) 2566 if (last != 31 || (NEXT2() != 139 && last >= 157 && last <= 157)) { 2567 ret = first ? -3 : -1; 2568 console.info('inflateBackTest Call result last != 31 || (NEXT2() != 139 && last != 157)') 2569 break; 2570 } 2571 first = 0; 2572 ret = -5; 2573 if (NEXT2() != 8) { 2574 if (last < 0) { 2575 console.info('inflateBackTest Call result 1 last == -1') 2576 break; 2577 } 2578 } 2579 flags = NEXT2(); 2580 NEXT2(); 2581 NEXT2(); 2582 NEXT2(); 2583 NEXT2(); 2584 NEXT2(); 2585 NEXT2(); 2586 if (last < 0) { 2587 console.info('inflateBackTest Call result 2 last == -1') 2588 break; 2589 } 2590 console.info('index = have = ', next[index], have) 2591 let newArrayBuffer = new ArrayBuffer(have); 2592 let newNext = new Uint8Array(newArrayBuffer); 2593 for (let i = 0; i < have; i++) { 2594 newNext[i] = next[26 - have + i]; 2595 } 2596 console.info('newArrayBuffer.length = ', newArrayBuffer.byteLength) 2597 console.info('newNext.length = ', newNext.length) 2598 let zStream: zlib.ZStream = { 2599 nextIn: newArrayBuffer, 2600 availableIn: have, 2601 }; 2602 await sr.inflateBack( 2603 zStream, 2604 readIn, 2605 { fileName: 'test.gz' }, 2606 writeOut, 2607 { fileName: 'test.gz' }).then((result) => { 2608 ret = result; 2609 console.info('inflateBack Call result res', result) 2610 }) 2611 if (ret == 1) { 2612 console.info('inflateBackTest Call result success') 2613 break; 2614 } 2615 } 2616 await sr.inflateBackEnd({}).then((result) => { 2617 console.info('inflateBackEnd Call result res', result) 2618 }) 2619 } 2620 catch (errData) { 2621 console.error(`errData is message:${errData}`); 2622 } 2623 }) 2624 inflateBackTest(); 2625} 2626``` 2627 2628### inflate<sup>12+</sup> 2629 2630inflate(strm: ZStream, flush: CompressFlushMode): Promise<ReturnStatus> 2631 2632解压数据,使用Promise异步返回。成功时返回结果状态。 2633 2634**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2635 2636**系统能力:** SystemCapability.BundleManager.Zlib 2637 2638**参数:** 2639 2640| 参数名 | 类型 | 必填 | 说明 | 2641| ------ | ----------------- | ---- | ------------------------------------------------------ | 2642| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2643| flush | CompressFlushMode | 是 | 参考[CompressFlushMode定义](#zipcompressflushmode12)。 | 2644 2645**返回值:** 2646 2647| 类型 | 说明 | 2648| ------------------------------------------------- | --------------------------- | 2649| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 2650 2651**错误码:** 2652 2653以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2654 2655| 错误码ID | 错误信息 | 2656| -------- | ------------------------------------------------------------ | 2657| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2658| 17800004 | ZStream error. | 2659| 17800005 | Data error. | 2660 2661**示例:** 2662 2663```ts 2664import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2665 2666async function demo() { 2667 let str = 'hello world!'; 2668 let arrayBufferIn = new ArrayBuffer(str.length); 2669 let byteArray = new Uint8Array(arrayBufferIn); 2670 for (let i = 0, j = str.length; i < j; i++) { 2671 byteArray[i] = str.charCodeAt(i) 2672 } 2673 let arrayBufferOut = new ArrayBuffer(100); 2674 let zStream: zlib.ZStream = { 2675 nextIn: arrayBufferIn, 2676 availableIn: 1, 2677 nextOut: arrayBufferOut, 2678 availableOut: 1 2679 }; 2680 let zip = zlib.createZipSync(); 2681 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 2682 console.info('deflateInit success') 2683 }).catch((errData: BusinessError) => { 2684 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2685 }) 2686 await zip.deflate({ availableOut: 8 }, zlib.CompressFlushMode.FINISH).then((data) => { 2687 console.info('deflate success') 2688 }).catch((errData: BusinessError) => { 2689 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2690 }) 2691 await zip.deflateEnd({ nextOut: arrayBufferOut }).then(data => { 2692 console.info('deflateEnd success') 2693 }).catch((errData: BusinessError) => { 2694 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2695 }) 2696 await zip.inflateInit({ nextIn: arrayBufferOut, availableIn: 1, nextOut: arrayBufferOut, availableOut: 1 } 2697 ).then(data => { 2698 console.info('inflateInit success'); 2699 }).catch((errData: BusinessError) => { 2700 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2701 }) 2702 await zip.inflate({ availableIn: 8, availableOut: 8 }, 0).then((data) => { 2703 console.info('inflate success') 2704 }).catch((errData: BusinessError) => { 2705 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2706 }) 2707 await zip.inflateEnd({ nextOut: arrayBufferOut }).then((data) => { 2708 console.info('inflateEnd success') 2709 }).catch((errData: BusinessError) => { 2710 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2711 }) 2712} 2713``` 2714 2715### deflateInit<sup>12+</sup> 2716 2717deflateInit(strm: ZStream, level: CompressLevel): Promise<ReturnStatus> 2718 2719初始化内部流状态以进行压缩,使用Promise异步返回。成功时返回结果状态。 2720 2721**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2722 2723**系统能力:** SystemCapability.BundleManager.Zlib 2724 2725**参数:** 2726 2727| 参数名 | 类型 | 必填 | 说明 | 2728| ------ | ------------- | ---- | ---------------------------------------------------- | 2729| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2730| level | CompressLevel | 是 | 参考[zip.CompressLevel枚举定义](#zipcompresslevel)。 | 2731 2732**返回值:** 2733 2734| 类型 | 说明 | 2735| ------------------------------------------------- | --------------------------- | 2736| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 2737 2738**错误码:** 2739 2740以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2741 2742| 错误码ID | 错误信息 | 2743| -------- | ------------------------------------------------------------ | 2744| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2745| 17800004 | ZStream error. | 2746 2747**示例:** 2748 2749```ts 2750import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2751 2752async function demo() { 2753 let str = 'hello world!'; 2754 let arrayBufferIn = new ArrayBuffer(str.length); 2755 let byteArray = new Uint8Array(arrayBufferIn); 2756 for (let i = 0, j = str.length; i < j; i++) { 2757 byteArray[i] = str.charCodeAt(i) 2758 } 2759 let arrayBufferOut = new ArrayBuffer(100); 2760 let zStream: zlib.ZStream = { 2761 nextIn: arrayBufferIn, 2762 availableIn: 1, 2763 nextOut: arrayBufferOut, 2764 availableOut: 1 2765 }; 2766 let zip = zlib.createZipSync(); 2767 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 2768 console.info('deflateInit success') 2769 }).catch((errData: BusinessError) => { 2770 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2771 }) 2772} 2773``` 2774 2775### deflateInit2<sup>12+</sup> 2776 2777deflateInit2(strm: ZStream, level: CompressLevel, method: CompressMethod, windowBits: number, memLevel: MemLevel, strategy: CompressStrategy): Promise<ReturnStatus> 2778 2779初始化内部流状态以进行压缩,使用Promise异步返回。成功时返回结果状态。 2780 2781**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2782 2783**系统能力:** SystemCapability.BundleManager.Zlib 2784 2785**参数:** 2786 2787| 参数名 | 类型 | 必填 | 说明 | 2788| ---------- | ---------------- | ---- | ---------------------------------------------------------- | 2789| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2790| level | CompressLevel | 是 | 参考[zip.CompressLevel枚举定义](#zipcompresslevel)。 | 2791| method | CompressMethod | 是 | 参考[zip.CompressMethod枚举定义](#zipcompressmethod12)。 | 2792| windowBits | number | 是 | 最大窗口大小的以2为底的对数。 | 2793| memLevel | MemLevel | 是 | 参考[zip.MemLevel枚举定义](#zipmemlevel)。 | 2794| strategy | CompressStrategy | 是 | 参考[zip.CompressStrategy枚举定义](#zipcompressstrategy)。 | 2795 2796**返回值:** 2797 2798| 类型 | 说明 | 2799| ------------------------------------------------- | --------------------------- | 2800| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 2801 2802**错误码:** 2803 2804以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2805 2806| 错误码ID | 错误信息 | 2807| -------- | ------------------------------------------------------------ | 2808| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2809| 17800004 | ZStream error. | 2810 2811**示例:** 2812 2813```ts 2814import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2815 2816async function demo() { 2817 let str = 'hello world!'; 2818 let arrayBufferIn = new ArrayBuffer(str.length); 2819 let byteArray = new Uint8Array(arrayBufferIn); 2820 for (let i = 0, j = str.length; i < j; i++) { 2821 byteArray[i] = str.charCodeAt(i) 2822 } 2823 let arrayBufferOut = new ArrayBuffer(100); 2824 let zStream: zlib.ZStream = { 2825 nextIn: arrayBufferIn, 2826 availableIn: 1, 2827 nextOut: arrayBufferOut, 2828 availableOut: 1 2829 }; 2830 let zip = zlib.createZipSync() 2831 await zip.deflateInit2(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED, zlib.CompressMethod.DEFLATED, 28, 2832 zlib.MemLevel.MEM_LEVEL_DEFAULT, zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY).then((data) => { 2833 console.info('deflateInit2 success'); 2834 }).catch((errData: BusinessError) => { 2835 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2836 }) 2837} 2838``` 2839 2840### deflate<sup>12+</sup> 2841 2842deflate(strm: ZStream, flush: CompressFlushMode): Promise<ReturnStatus> 2843 2844压缩数据,使用Promise异步返回。成功时返回结果状态。 2845 2846**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2847 2848**系统能力:** SystemCapability.BundleManager.Zlib 2849 2850**参数:** 2851 2852| 参数名 | 类型 | 必填 | 说明 | 2853| ------ | ----------------- | ---- | ------------------------------------------------------ | 2854| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2855| flush | CompressFlushMode | 是 | 参考[CompressFlushMode定义](#zipcompressflushmode12)。 | 2856 2857**返回值:** 2858 2859| 类型 | 说明 | 2860| ------------------------------------------------- | --------------------------- | 2861| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 2862 2863**错误码:** 2864 2865以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2866 2867| 错误码ID | 错误信息 | 2868| -------- | ------------------------------------------------------------ | 2869| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2870| 17800004 | ZStream error. | 2871| 17800007 | Buffer error. | 2872 2873**示例:** 2874 2875```ts 2876import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2877 2878async function demo() { 2879 let str = 'hello world!'; 2880 let arrayBufferIn = new ArrayBuffer(str.length); 2881 let byteArray = new Uint8Array(arrayBufferIn); 2882 for (let i = 0, j = str.length; i < j; i++) { 2883 byteArray[i] = str.charCodeAt(i) 2884 } 2885 let arrayBufferOut = new ArrayBuffer(100); 2886 let zStream: zlib.ZStream = { 2887 nextIn: arrayBufferIn, 2888 availableIn: 1, 2889 nextOut: arrayBufferOut, 2890 availableOut: 1 2891 }; 2892 let zip = zlib.createZipSync(); 2893 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 2894 console.info('deflateInit success') 2895 }).catch((errData: BusinessError) => { 2896 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2897 }) 2898 await zip.deflate({ availableOut: 8 }, zlib.CompressFlushMode.FINISH).then((data) => { 2899 console.info('deflate success') 2900 }).catch((errData: BusinessError) => { 2901 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2902 }) 2903} 2904``` 2905 2906### deflateEnd<sup>12+</sup> 2907 2908deflateEnd(strm: ZStream): Promise<ReturnStatus> 2909 2910压缩流的所有动态分配的数据结构都被释放,使用Promise异步返回。成功时返回结果状态。 2911 2912**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2913 2914**系统能力:** SystemCapability.BundleManager.Zlib 2915 2916**参数:** 2917 2918| 参数名 | 类型 | 必填 | 说明 | 2919| ------ | ------- | ---- | ------------------------------- | 2920| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2921 2922**返回值:** 2923 2924| 类型 | 说明 | 2925| ------------------------------------------------- | --------------------------- | 2926| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 2927 2928**错误码:** 2929 2930以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 2931 2932| 错误码ID | 错误信息 | 2933| -------- | ------------------------------------------------------------ | 2934| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 2935| 17800004 | ZStream error. | 2936 2937**示例:** 2938 2939```ts 2940import { zlib, BusinessError } from '@kit.BasicServicesKit'; 2941 2942async function demo() { 2943 let str = 'hello world!'; 2944 let arrayBufferIn = new ArrayBuffer(str.length); 2945 let byteArray = new Uint8Array(arrayBufferIn); 2946 for (let i = 0, j = str.length; i < j; i++) { 2947 byteArray[i] = str.charCodeAt(i) 2948 } 2949 let arrayBufferOut = new ArrayBuffer(100); 2950 let zStream: zlib.ZStream = { 2951 nextIn: arrayBufferIn, 2952 availableIn: 1, 2953 nextOut: arrayBufferOut, 2954 availableOut: 1 2955 }; 2956 let zip = zlib.createZipSync(); 2957 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 2958 console.info('deflateInit success') 2959 }).catch((errData: BusinessError) => { 2960 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2961 }) 2962 await zip.deflate({ availableOut: 8 }, zlib.CompressFlushMode.FINISH).then((data) => { 2963 console.info('deflate success') 2964 }).catch((errData: BusinessError) => { 2965 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 2966 }) 2967 await zip.deflateEnd({ nextOut: arrayBufferOut }).then(data => { 2968 console.info('deflateEnd success') 2969 }).catch((errData: BusinessError) => { 2970 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 2971 }) 2972} 2973``` 2974 2975### deflateBound<sup>12+</sup> 2976 2977deflateBound(strm: ZStream, sourceLength: number): Promise<number> 2978 2979计算压缩大小的上限,使用Promise异步返回。成功时返回压缩大小的上限。 2980 2981**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2982 2983**系统能力:** SystemCapability.BundleManager.Zlib 2984 2985**参数:** 2986 2987| 参数名 | 类型 | 必填 | 说明 | 2988| --------- | ------- | ---- | ------------------------------- | 2989| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 2990| sourceLength | number | 是 | 源数据长度。 | 2991 2992**返回值:** 2993 2994| 类型 | 说明 | 2995| --------------------- | --------------------------------- | 2996| Promise<number> | Promise对象。返回压缩大小的上限。 | 2997 2998**错误码:** 2999 3000以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3001 3002| 错误码ID | 错误信息 | 3003| -------- | ------------------------------------------------------------ | 3004| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3005 3006**示例:** 3007 3008```ts 3009import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3010 3011async function demo() { 3012 let str = 'hello world!'; 3013 let arrayBufferIn = new ArrayBuffer(str.length); 3014 let byteArray = new Uint8Array(arrayBufferIn); 3015 for (let i = 0, j = str.length; i < j; i++) { 3016 byteArray[i] = str.charCodeAt(i) 3017 } 3018 let arrayBufferOut = new ArrayBuffer(100); 3019 let zStream: zlib.ZStream = { 3020 nextIn: arrayBufferIn, 3021 availableIn: 1, 3022 nextOut: arrayBufferOut, 3023 availableOut: 1 3024 }; 3025 let zip = zlib.createZipSync(); 3026 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3027 console.info('deflateInit success') 3028 }).catch((errData: BusinessError) => { 3029 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3030 }) 3031 await zip.deflateBound({ nextOut: arrayBufferOut }, 12).then((data) => { 3032 console.info('deflateBound success') 3033 }).catch((errData: BusinessError) => { 3034 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3035 }) 3036} 3037``` 3038 3039### deflateSetHeader<sup>12+</sup> 3040 3041deflateSetHeader(strm: ZStream, head: GzHeader): Promise<ReturnStatus> 3042 3043当deflateInit2()请求gzip流时,提供gzip标头信息,使用Promise异步返回。成功时返回结果状态。 3044 3045**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3046 3047**系统能力:** SystemCapability.BundleManager.Zlib 3048 3049**参数:** 3050 3051| 参数名 | 类型 | 必填 | 说明 | 3052| ------ | ----------------------- | ---- | -------------------------------- | 3053| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3054| head | [GzHeader](#gzheader12) | 是 | 从压缩数据流中提取的gzip头信息。 | 3055 3056**返回值:** 3057 3058| 类型 | 说明 | 3059| ------------------------------------------------- | --------------------------- | 3060| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 3061 3062**错误码:** 3063 3064以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3065 3066| 错误码ID | 错误信息 | 3067| -------- | ------------------------------------------------------------ | 3068| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3069| 17800004 | ZStream error. | 3070 3071**示例:** 3072 3073```ts 3074import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3075 3076async function demo() { 3077 let str = 'hello world!'; 3078 let arrayBufferIn = new ArrayBuffer(str.length); 3079 let byteArray = new Uint8Array(arrayBufferIn); 3080 for (let i = 0, j = str.length; i < j; i++) { 3081 byteArray[i] = str.charCodeAt(i) 3082 } 3083 let arrayBufferOut = new ArrayBuffer(100); 3084 let zStream: zlib.ZStream = { 3085 nextIn: arrayBufferIn, 3086 availableIn: 1, 3087 nextOut: arrayBufferOut, 3088 availableOut: 1 3089 }; 3090 let zip = zlib.createZipSync() 3091 await zip.deflateInit2(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED, zlib.CompressMethod.DEFLATED, 28, 3092 zlib.MemLevel.MEM_LEVEL_DEFAULT, zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY).then((data) => { 3093 console.info('deflateInit2 success'); 3094 }).catch((errData: BusinessError) => { 3095 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 3096 }) 3097 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) => { 3098 console.info('deflateSetHeader success'); 3099 }).catch((errData: BusinessError) => { 3100 console.error(`errData is errCode:${errData.code} message:${errData.message}`) 3101 }) 3102} 3103``` 3104 3105### deflateCopy<sup>12+</sup> 3106 3107deflateCopy(source: Zip): Promise<ReturnStatus> 3108 3109复制压缩流,使用Promise异步返回。成功时返回结果状态。 3110 3111**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3112 3113**系统能力:** SystemCapability.BundleManager.Zlib 3114 3115**参数:** 3116 3117| 参数名 | 类型 | 必填 | 说明 | 3118| ------ | ---- | ---- | ----------------------- | 3119| source | Zip | 是 | 参考[Zip定义](#zip12)。 | 3120 3121**返回值:** 3122 3123| 类型 | 说明 | 3124| ------------------------------------------------- | --------------------------- | 3125| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 3126 3127**错误码:** 3128 3129以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3130 3131| 错误码ID | 错误信息 | 3132| -------- | ------------------------------------------------------------ | 3133| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3134| 17800004 | ZStream error. | 3135 3136**示例:** 3137 3138```ts 3139import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3140 3141async function demo() { 3142 let str = 'hello world!'; 3143 let arrayBufferIn = new ArrayBuffer(str.length); 3144 let byteArray = new Uint8Array(arrayBufferIn); 3145 for (let i = 0, j = str.length; i < j; i++) { 3146 byteArray[i] = str.charCodeAt(i) 3147 } 3148 let arrayBufferOut = new ArrayBuffer(100); 3149 let zStream: zlib.ZStream = { 3150 nextIn: arrayBufferIn, 3151 availableIn: 1, 3152 nextOut: arrayBufferOut, 3153 availableOut: 1 3154 }; 3155 let zip = zlib.createZipSync(); 3156 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3157 console.info('deflateInit success') 3158 }).catch((errData: BusinessError) => { 3159 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3160 }) 3161 await zip.deflateCopy(zip).then((data) => { 3162 console.info('deflateCopy success') 3163 }).catch((errData: BusinessError) => { 3164 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3165 }) 3166} 3167``` 3168 3169### deflateSetDictionary<sup>12+</sup> 3170 3171deflateSetDictionary(strm: ZStream, dictionary: ArrayBuffer): Promise<ReturnStatus> 3172 3173从给定的字节序列初始化压缩字典,使用Promise异步返回。成功时返回结果状态。 3174 3175**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3176 3177**系统能力:** SystemCapability.BundleManager.Zlib 3178 3179**参数:** 3180 3181| 参数名 | 类型 | 必填 | 说明 | 3182| ---------- | ----------- | ---- | ------------------------------- | 3183| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3184| dictionary | ArrayBuffer | 是 | 字典数据。 | 3185 3186**返回值:** 3187 3188| 类型 | 说明 | 3189| ------------------------------------------------- | --------------------------- | 3190| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 3191 3192**错误码:** 3193 3194以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3195 3196| 错误码ID | 错误信息 | 3197| -------- | ------------------------------------------------------------ | 3198| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3199| 17800004 | ZStream error. | 3200 3201**示例:** 3202 3203```ts 3204import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3205 3206async function demo() { 3207 let str = 'hello world!'; 3208 let arrayBufferIn = new ArrayBuffer(str.length); 3209 let byteArray = new Uint8Array(arrayBufferIn); 3210 for (let i = 0, j = str.length; i < j; i++) { 3211 byteArray[i] = str.charCodeAt(i) 3212 } 3213 let arrayBufferOut = new ArrayBuffer(100); 3214 let zStream: zlib.ZStream = { 3215 nextIn: arrayBufferIn, 3216 availableIn: 1, 3217 nextOut: arrayBufferOut, 3218 availableOut: 1 3219 }; 3220 let zip = zlib.createZipSync(); 3221 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3222 console.info('deflateInit success') 3223 }).catch((errData: BusinessError) => { 3224 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3225 }) 3226 await zip.deflateSetDictionary({ nextOut: arrayBufferOut }, arrayBufferOut).then((data) => { 3227 console.info('deflateSetDictionary success') 3228 }).catch((errData: BusinessError) => { 3229 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3230 }) 3231} 3232``` 3233 3234### deflateGetDictionary<sup>12+</sup> 3235 3236deflateGetDictionary(strm: ZStream, dictionary: ArrayBuffer): Promise<DictionaryOutputInfo> 3237 3238获取当前解压缩流中使用的解压缩字典内容及其长度,使用Promise异步返回。成功时返回结果状态和字典的长度。 3239 3240**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3241 3242**系统能力:** SystemCapability.BundleManager.Zlib 3243 3244**参数:** 3245 3246| 参数名 | 类型 | 必填 | 说明 | 3247| ---------- | ----------- | ---- | ------------------------------- | 3248| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3249| dictionary | ArrayBuffer | 是 | 接收解压缩字典的实际内容。 | 3250 3251**返回值:** 3252 3253| 类型 | 说明 | 3254| ------------------------------------------------------------ | --------------------------------------- | 3255| Promise<[DictionaryOutputInfo](#dictionaryoutputinfo12)> | Promise对象。返回结果状态和字典的长度。 | 3256 3257**错误码:** 3258 3259以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3260 3261| 错误码ID | 错误信息 | 3262| -------- | ------------------------------------------------------------ | 3263| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3264| 17800004 | ZStream error. | 3265 3266**示例:** 3267 3268```ts 3269import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3270 3271async function demo() { 3272 let str = 'hello world!'; 3273 let arrayBufferIn = new ArrayBuffer(str.length); 3274 let byteArray = new Uint8Array(arrayBufferIn); 3275 for (let i = 0, j = str.length; i < j; i++) { 3276 byteArray[i] = str.charCodeAt(i) 3277 } 3278 let arrayBufferOut = new ArrayBuffer(100); 3279 let zStream: zlib.ZStream = { 3280 nextIn: arrayBufferIn, 3281 availableIn: 1, 3282 nextOut: arrayBufferOut, 3283 availableOut: 1 3284 }; 3285 let zip = zlib.createZipSync(); 3286 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3287 console.info('deflateInit success') 3288 }).catch((errData: BusinessError) => { 3289 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3290 }) 3291 await zip.deflateSetDictionary({ nextOut: arrayBufferOut }, arrayBufferOut).then((data) => { 3292 console.info('deflateSetDictionary success') 3293 }).catch((errData: BusinessError) => { 3294 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3295 }) 3296 await zip.deflateGetDictionary({ nextOut: arrayBufferOut }, arrayBufferOut).then((data) => { 3297 console.info('deflateGetDictionary success') 3298 }).catch((errData: BusinessError) => { 3299 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3300 }) 3301} 3302``` 3303 3304### deflateTune<sup>12+</sup> 3305 3306deflateTune(strm: ZStream, goodLength: number, maxLazy: number, niceLength: number, maxChain: number): Promise<ReturnStatus> 3307 3308微调deflate的内部压缩参数,使用Promise异步返回。成功时返回结果状态。 3309 3310**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3311 3312**系统能力:** SystemCapability.BundleManager.Zlib 3313 3314**参数:** 3315 3316| 参数名 | 类型 | 必填 | 说明 | 3317| ---------- | ------- | ---- | ------------------------------- | 3318| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3319| goodLength | number | 是 | 匹配的长度阈值。 | 3320| maxLazy | number | 是 | 最大延迟匹配时间。 | 3321| niceLength | number | 是 | 适合的延迟长度阈值 | 3322| maxChain | number | 是 | 最大链条长度 | 3323 3324**返回值:** 3325 3326| 类型 | 说明 | 3327| ------------------------------------------------- | --------------------------- | 3328| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 3329 3330**错误码:** 3331 3332以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3333 3334| 错误码ID | 错误信息 | 3335| -------- | ------------------------------------------------------------ | 3336| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3337| 17800004 | ZStream error. | 3338 3339**示例:** 3340 3341```ts 3342import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3343 3344async function demo() { 3345 let str = 'hello world!'; 3346 let arrayBufferIn = new ArrayBuffer(str.length); 3347 let byteArray = new Uint8Array(arrayBufferIn); 3348 for (let i = 0, j = str.length; i < j; i++) { 3349 byteArray[i] = str.charCodeAt(i) 3350 } 3351 let arrayBufferOut = new ArrayBuffer(100); 3352 let zStream: zlib.ZStream = { 3353 nextIn: arrayBufferIn, 3354 availableIn: 1, 3355 nextOut: arrayBufferOut, 3356 availableOut: 1 3357 }; 3358 let zip = zlib.createZipSync(); 3359 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3360 console.info('deflateInit success') 3361 }).catch((errData: BusinessError) => { 3362 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3363 }) 3364 await zip.deflateTune({ nextOut: arrayBufferOut }, 2, 2, 2, 2).then((data) => { 3365 console.info('deflateTune success:') 3366 }).catch((errData: BusinessError) => { 3367 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3368 }) 3369} 3370``` 3371 3372### deflateReset<sup>12+</sup> 3373 3374deflateReset(strm: ZStream): Promise<ReturnStatus> 3375 3376这个函数相当于先调用deflateEnd再调用deflateInit,但是并不会释放和重新分配内部解压缩状态,使用Promise异步返回。成功时返回结果状态。 3377 3378**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3379 3380**系统能力:** SystemCapability.BundleManager.Zlib 3381 3382**参数:** 3383 3384| 参数名 | 类型 | 必填 | 说明 | 3385| ------ | ------- | ---- | ------------------------------- | 3386| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3387 3388**返回值:** 3389 3390| 类型 | 说明 | 3391| ------------------------------------------------- | --------------------------- | 3392| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 3393 3394**错误码:** 3395 3396以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3397 3398| 错误码ID | 错误信息 | 3399| -------- | ------------------------------------------------------------ | 3400| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3401| 17800004 | ZStream error. | 3402 3403**示例:** 3404 3405```ts 3406import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3407 3408async function demo() { 3409 let str = 'hello world!'; 3410 let arrayBufferIn = new ArrayBuffer(str.length); 3411 let byteArray = new Uint8Array(arrayBufferIn); 3412 for (let i = 0, j = str.length; i < j; i++) { 3413 byteArray[i] = str.charCodeAt(i) 3414 } 3415 let arrayBufferOut = new ArrayBuffer(100); 3416 let zStream: zlib.ZStream = { 3417 nextIn: arrayBufferIn, 3418 availableIn: 1, 3419 nextOut: arrayBufferOut, 3420 availableOut: 1 3421 }; 3422 let zip = zlib.createZipSync(); 3423 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3424 console.info('deflateInit success') 3425 }).catch((errData: BusinessError) => { 3426 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3427 }) 3428 await zip.deflateReset({ nextOut: arrayBufferOut }).then((data) => { 3429 console.info('deflateReset success') 3430 }).catch((errData: BusinessError) => { 3431 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3432 }) 3433} 3434``` 3435 3436### deflateResetKeep<sup>12+</sup> 3437 3438deflateResetKeep(strm: ZStream): Promise<ReturnStatus> 3439 3440重置初始化的deflate压缩流,但保留其设置的压缩参数和字典,使用Promise异步返回。成功时返回结果状态。 3441 3442**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3443 3444**系统能力:** SystemCapability.BundleManager.Zlib 3445 3446**参数:** 3447 3448| 参数名 | 类型 | 必填 | 说明 | 3449| ------ | ------- | ---- | ------------------------------- | 3450| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3451 3452**返回值:** 3453 3454| 类型 | 说明 | 3455| ------------------------------------------------- | --------------------------- | 3456| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 3457 3458**错误码:** 3459 3460以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3461 3462| 错误码ID | 错误信息 | 3463| -------- | ------------------------------------------------------------ | 3464| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3465| 17800004 | ZStream error. | 3466 3467**示例:** 3468 3469```ts 3470import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3471 3472async function demo() { 3473 let str = 'hello world!'; 3474 let arrayBufferIn = new ArrayBuffer(str.length); 3475 let byteArray = new Uint8Array(arrayBufferIn); 3476 for (let i = 0, j = str.length; i < j; i++) { 3477 byteArray[i] = str.charCodeAt(i) 3478 } 3479 let arrayBufferOut = new ArrayBuffer(100); 3480 let zStream: zlib.ZStream = { 3481 nextIn: arrayBufferIn, 3482 availableIn: 1, 3483 nextOut: arrayBufferOut, 3484 availableOut: 1 3485 }; 3486 let zip = zlib.createZipSync(); 3487 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3488 console.info('deflateInit success') 3489 }).catch((errData: BusinessError) => { 3490 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3491 }) 3492 await zip.deflateResetKeep({ nextOut: arrayBufferOut }).then((data) => { 3493 console.info('deflateResetKeep success') 3494 }).catch((errData: BusinessError) => { 3495 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3496 }) 3497} 3498``` 3499 3500### deflatePending<sup>12+</sup> 3501 3502deflatePending(strm: ZStream): Promise<DeflatePendingOutputInfo> 3503 3504返回已生成但尚未在可用输出中提供的输出的字节数和位数,使用Promise异步返回。成功时返回结果状态、输出位数和输出字节数。 3505 3506**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3507 3508**系统能力:** SystemCapability.BundleManager.Zlib 3509 3510**参数:** 3511 3512| 参数名 | 类型 | 必填 | 说明 | 3513| ------ | ------- | ---- | ------------------------------- | 3514| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3515 3516**返回值:** 3517 3518| 类型 | 说明 | 3519| ------------------------------------------------------------ | ------------------------------------------------- | 3520| Promise<[DeflatePendingOutputInfo](#deflatependingoutputinfo12)> | Promise对象。返回结果状态、输出位数和输出字节数。 | 3521 3522**错误码:** 3523 3524以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3525 3526| 错误码ID | 错误信息 | 3527| -------- | ------------------------------------------------------------ | 3528| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3529| 17800004 | ZStream error. | 3530 3531**示例:** 3532 3533```ts 3534import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3535 3536async function demo() { 3537 let str = 'hello world!'; 3538 let arrayBufferIn = new ArrayBuffer(str.length); 3539 let byteArray = new Uint8Array(arrayBufferIn); 3540 for (let i = 0, j = str.length; i < j; i++) { 3541 byteArray[i] = str.charCodeAt(i) 3542 } 3543 let arrayBufferOut = new ArrayBuffer(100); 3544 let zStream: zlib.ZStream = { 3545 nextIn: arrayBufferIn, 3546 availableIn: 1, 3547 nextOut: arrayBufferOut, 3548 availableOut: 1 3549 }; 3550 let zip = zlib.createZipSync(); 3551 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3552 console.info('deflateInit success') 3553 }).catch((errData: BusinessError) => { 3554 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3555 }) 3556 await zip.deflatePending({ nextOut: arrayBufferOut }).then((data) => { 3557 console.info('deflatePending success') 3558 }).catch((errData: BusinessError) => { 3559 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3560 }) 3561} 3562``` 3563 3564### deflateParams<sup>12+</sup> 3565 3566deflateParams(strm: ZStream, level: CompressLevel, strategy: CompressStrategy): Promise<ReturnStatus> 3567 3568动态更新压缩级别和压缩策略,使用Promise异步返回。成功时返回结果状态。 3569 3570**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3571 3572**系统能力:** SystemCapability.BundleManager.Zlib 3573 3574**参数:** 3575 3576| 参数名 | 类型 | 必填 | 说明 | 3577| -------- | ---------------- | ---- | ---------------------------------------------------------- | 3578| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3579| level | CompressLevel | 是 | 参考[zip.CompressLevel枚举定义](#zipcompresslevel)。 | 3580| strategy | CompressStrategy | 是 | 参考[zip.CompressStrategy枚举定义](#zipcompressstrategy)。 | 3581 3582**返回值:** 3583 3584| 类型 | 说明 | 3585| ------------------------------------------------- | --------------------------- | 3586| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 3587 3588**错误码:** 3589 3590以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3591 3592| 错误码ID | 错误信息 | 3593| -------- | ------------------------------------------------------------ | 3594| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3595| 17800004 | ZStream error. | 3596 3597**示例:** 3598 3599```ts 3600import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3601 3602async function demo() { 3603 let str = 'hello world!'; 3604 let arrayBufferIn = new ArrayBuffer(str.length); 3605 let byteArray = new Uint8Array(arrayBufferIn); 3606 for (let i = 0, j = str.length; i < j; i++) { 3607 byteArray[i] = str.charCodeAt(i) 3608 } 3609 let arrayBufferOut = new ArrayBuffer(100); 3610 let zStream: zlib.ZStream = { 3611 nextIn: arrayBufferIn, 3612 availableIn: 1, 3613 nextOut: arrayBufferOut, 3614 availableOut: 1 3615 }; 3616 let zip = zlib.createZipSync() 3617 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3618 console.info('deflateInit success') 3619 }).catch((errData: BusinessError) => { 3620 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3621 }) 3622 await zip.deflateParams(zStream, zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY).then((data) => { 3623 console.info('deflateParams success') 3624 }).catch((errData: BusinessError) => { 3625 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3626 }) 3627} 3628``` 3629 3630### deflatePrime<sup>12+</sup> 3631 3632deflatePrime(strm: ZStream, bits: number, value: number): Promise<ReturnStatus> 3633 3634在压缩流中插入位和值,使用Promise异步返回。成功时返回结果状态。 3635 3636**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3637 3638**系统能力:** SystemCapability.BundleManager.Zlib 3639 3640**参数:** 3641 3642| 参数名 | 类型 | 必填 | 说明 | 3643| ------ | ------- | ---- | ------------------------------- | 3644| strm | ZStream | 是 | 参考[ZStream定义](#zstream12)。 | 3645| bits | number | 是 | 要插入的位数,取值范围在0~16。 | 3646| value | number | 是 | 与位数相对应的位值。 | 3647 3648**返回值:** 3649 3650| 类型 | 说明 | 3651| ------------------------------------------------- | --------------------------- | 3652| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象。返回结果状态。 | 3653 3654**错误码:** 3655 3656以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3657 3658| 错误码ID | 错误信息 | 3659| -------- | ------------------------------------------------------------ | 3660| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3661| 17800004 | ZStream error. | 3662 3663**示例:** 3664 3665```ts 3666import { zlib, BusinessError } from '@kit.BasicServicesKit'; 3667 3668async function demo() { 3669 let str = 'hello world!'; 3670 let arrayBufferIn = new ArrayBuffer(str.length); 3671 let byteArray = new Uint8Array(arrayBufferIn); 3672 for (let i = 0, j = str.length; i < j; i++) { 3673 byteArray[i] = str.charCodeAt(i) 3674 } 3675 let arrayBufferOut = new ArrayBuffer(100); 3676 let zStream: zlib.ZStream = { 3677 nextIn: arrayBufferIn, 3678 availableIn: 1, 3679 nextOut: arrayBufferOut, 3680 availableOut: 1 3681 }; 3682 let zip = zlib.createZipSync(); 3683 await zip.deflateInit(zStream, zlib.CompressLevel.COMPRESS_LEVEL_BEST_SPEED).then((data) => { 3684 console.info('deflateInit success') 3685 }).catch((errData: BusinessError) => { 3686 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3687 }) 3688 await zip.deflatePrime({ nextOut: arrayBufferOut }, 5, 2).then((data) => { 3689 console.info('deflatePrime success') 3690 }).catch((errData: BusinessError) => { 3691 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 3692 }) 3693} 3694``` 3695 3696## Options 3697 3698**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3699 3700**系统能力:** SystemCapability.BundleManager.Zlib 3701 3702| 名称 | 类型 | 可读 | 可写 | 说明 | 3703| -------- | ---------------- | ---- | ---- | ---------------------------------------------------------- | 3704| level | CompressLevel | 是 | 否 | 参考[zip.CompressLevel枚举定义](#zipcompresslevel)。 | 3705| memLevel | MemLevel | 是 | 否 | 参考[zip.MemLevel枚举定义](#zipmemlevel)。 | 3706| strategy | CompressStrategy | 是 | 否 | 参考[zip.CompressStrategy枚举定义](#zipcompressstrategy)。 | 3707 3708## zip.CompressLevel 3709 3710**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3711 3712**系统能力:** SystemCapability.BundleManager.Zlib 3713 3714| 名称 | 值 | 说明 | 3715| ---------------------------------- | ---- | ----------------- | 3716| COMPRESS_LEVEL_NO_COMPRESSION | 0 | 压缩率为0压缩等级。 | 3717| COMPRESS_LEVEL_BEST_SPEED | 1 | 最佳速度压缩等级。 | 3718| COMPRESS_LEVEL_BEST_COMPRESSION | 9 | 最佳压缩等级。 | 3719| COMPRESS_LEVEL_DEFAULT_COMPRESSION | -1 | 默认压缩等级。 | 3720 3721## zip.MemLevel 3722 3723**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3724 3725**系统能力:** SystemCapability.BundleManager.Zlib 3726 3727| 名称 | 值 | 说明 | 3728| ----------------- | ---- | -------------------------------- | 3729| MEM_LEVEL_MIN | 1 | zip 接口在压缩过程中最小使用内存。 | 3730| MEM_LEVEL_MAX | 9 | zip 接口在压缩过程中最大使用内存。 | 3731| MEM_LEVEL_DEFAULT | 8 | zip 接口在压缩过程中默认使用内存。 | 3732 3733## zip.CompressStrategy 3734 3735**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3736 3737**系统能力:** SystemCapability.BundleManager.Zlib 3738 3739| 名称 | 值 | 说明 | 3740| ---------------------------------- | ---- | ------------------------ | 3741| COMPRESS_STRATEGY_DEFAULT_STRATEGY | 0 | 常规数据策略。 | 3742| COMPRESS_STRATEGY_FILTERED | 1 | 过滤器产生的数据压缩策略。 | 3743| COMPRESS_STRATEGY_HUFFMAN_ONLY | 2 | 霍夫曼编码格式压缩策略。 | 3744| COMPRESS_STRATEGY_RLE | 3 | 游标编码压缩策略。 | 3745| COMPRESS_STRATEGY_FIXED | 4 | 固定的压缩策略。 | 3746 3747## zip.ErrorCode 3748 3749**系统能力:** SystemCapability.BundleManager.Zlib 3750 3751| 名称 | 值 | 说明 | 3752| ---------------- | ---- | ------------ | 3753| ERROR_CODE_OK | 0 | 函数调用成功。 | 3754| ERROR_CODE_ERRNO | -1 | 函数调用失败。 | 3755 3756## zip.CompressFlushMode<sup>12+</sup> 3757 3758**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3759 3760**系统能力:** SystemCapability.BundleManager.Zlib 3761 3762| 名称 | 值 | 说明 | 3763| ------------- | ---- | -------------------------------------------- | 3764| NO_FLUSH | 0 | 默认值,表示正常操作。 | 3765| PARTIAL_FLUSH | 1 | 在流中生成部分刷新点。 | 3766| SYNC_FLUSH | 2 | 在保持压缩流状态的同时强制输出所有压缩数据。 | 3767| FULL_FLUSH | 3 | 重置压缩状态。 | 3768| FINISH | 4 | 压缩或解压缩过程结束。 | 3769| BLOCK | 5 | 允许更精确的控制。 | 3770| TREES | 6 | 实施过程中有特殊目的。 | 3771 3772## zip.CompressMethod<sup>12+</sup> 3773 3774**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3775 3776**系统能力:** SystemCapability.BundleManager.Zlib 3777 3778| 名称 | 值 | 说明 | 3779| -------- | ---- | ---------- | 3780| DEFLATED | 8 | 压缩方法。 | 3781 3782## zip.ReturnStatus<sup>12+</sup> 3783 3784**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3785 3786**系统能力:** SystemCapability.BundleManager.Zlib 3787 3788| 名称 | 值 | 说明 | 3789| ---------- | ---- | ---------------------------------------------- | 3790| OK | 0 | 函数调用成功。 | 3791| STREAM_END | 1 | 函数调用成功,表示已处理了整个数据。 | 3792| NEED_DICT | 2 | 函数调用成功,表示需要预设字典才能继续解压缩。 | 3793 3794## ZStream<sup>12+</sup> 3795 3796**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3797 3798**系统能力:** SystemCapability.BundleManager.Zlib 3799 3800| 名称 | 类型 | 可读 | 可写 | 说明 | 3801| ------------ | ----------- | ---- | ---- | ------------------------------------------------------------ | 3802| nextIn | ArrayBuffer | 是 | 否 | 需要压缩的输入字节 | 3803| availableIn | number | 是 | 否 | nextIn可用的字节数 | 3804| totalIn | number | 是 | 否 | 到目前为止读取的输入字节总数 | 3805| nextOut | ArrayBuffer | 是 | 否 | 压缩后的输出字节 | 3806| availableOut | number | 是 | 否 | nextOut的剩余可用字节数 | 3807| totalOut | number | 是 | 否 | 到目前为止输出字节总数 | 3808| dataType | number | 是 | 否 | 关于数据类型的最佳猜测:deflate的二进制或文本,或inflate的解码状态 | 3809| adler | number | 是 | 否 | 未压缩数据的Adler-32或CRC-32值 | 3810 3811## ZipOutputInfo<sup>12+</sup> 3812 3813**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3814 3815**系统能力:** SystemCapability.BundleManager.Zlib 3816 3817| 名称 | 类型 | 可读 | 可写 | 说明 | 3818| ------- | ------------ | ---- | ---- | ----------------------------------------------------- | 3819| status | ReturnStatus | 是 | 否 | 参考[zlib.ReturnStatus枚举定义](#zipreturnstatus12)。 | 3820| destLen | number | 是 | 否 | 目标缓冲区的总长度。 | 3821 3822## DictionaryOutputInfo<sup>12+</sup> 3823 3824**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3825 3826**系统能力:** SystemCapability.BundleManager.Zlib 3827 3828| 名称 | 类型 | 可读 | 可写 | 说明 | 3829| ---------------- | ------------ | ---- | ---- | ----------------------------------------------------- | 3830| status | ReturnStatus | 是 | 否 | 参考[zlib.ReturnStatus枚举定义](#zipreturnstatus12)。 | 3831| dictionaryLength | number | 是 | 否 | 字典的长度。 | 3832 3833## DecompressionOutputInfo<sup>12+</sup> 3834 3835**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3836 3837**系统能力:** SystemCapability.BundleManager.Zlib 3838 3839| 名称 | 类型 | 可读 | 可写 | 说明 | 3840| ---------------- | ------------ | ---- | ---- | ----------------------------------------------------- | 3841| status | ReturnStatus | 是 | 否 | 参考[zlib.ReturnStatus枚举定义](#zipreturnstatus12)。 | 3842| destLength | number | 是 | 否 | 目标缓冲区的长度。 | 3843| sourceLength | number | 是 | 否 | 源缓冲区的长度。 | 3844 3845## DeflatePendingOutputInfo<sup>12+</sup> 3846 3847**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3848 3849**系统能力:** SystemCapability.BundleManager.Zlib 3850 3851| 名称 | 类型 | 可读 | 可写 | 说明 | 3852| ------- | ------------ | ---- | ---- | ----------------------------------------------------- | 3853| status | ReturnStatus | 是 | 否 | 参考[zlib.ReturnStatus枚举定义](#zipreturnstatus12)。 | 3854| pending | number | 是 | 否 | 已生成的输出字节数。 | 3855| bits | number | 是 | 否 | 已生成的输出位数。 | 3856 3857## GzHeader<sup>12+</sup> 3858 3859**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3860 3861**系统能力:** SystemCapability.BundleManager.Zlib 3862 3863| 名称 | 类型 | 可读 | 可写 | 说明 | 3864| -------- | ----------- | ---- | ---- | ------------------------------------ | 3865| isText | boolean | 是 | 否 | 如果压缩数据被认为是文本,则为True。 | 3866| os | number | 是 | 否 | 操作系统。 | 3867| time | number | 是 | 否 | 修改时间。 | 3868| xflags | number | 是 | 否 | 额外标志。 | 3869| extra | ArrayBuffer | 是 | 否 | 额外字段。 | 3870| extraLen | number | 是 | 否 | 额外字段的长度。 | 3871| name | ArrayBuffer | 是 | 否 | 文件名。 | 3872| comment | ArrayBuffer | 是 | 否 | 注释。 | 3873| hcrc | boolean | 是 | 否 | 如果存在crc标头,则为True。 | 3874| done | boolean | 是 | 否 | 读取gzip标头后为True。 | 3875 3876## zlib.createGZip<sup>12+</sup> 3877 3878createGZip(): Promise<GZip> 3879 3880创建GZip对象,使用Promise异步返回。成功时返回Gzip对象实例。 3881 3882**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3883 3884**系统能力:** SystemCapability.BundleManager.Zlib 3885 3886**返回值:** 3887 3888| 类型 | 说明 | 3889| ------------------------------ | ------------------------------- | 3890| Promise<[GZip](#gzip12)> | Promise对象。返回GZip对象实例。 | 3891 3892**示例:** 3893 3894```ts 3895import { zlib } from '@kit.BasicServicesKit'; 3896 3897zlib.createGZip().then((data) => { 3898 console.info('createGZip success'); 3899}) 3900``` 3901 3902## zlib.createGZipSync<sup>12+</sup> 3903 3904createGZipSync(): GZip 3905 3906创建GZip对象。成功时返回GZip对象实例。 3907 3908**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3909 3910**系统能力:** SystemCapability.BundleManager.Zlib 3911 3912**返回值:** 3913 3914| 类型 | 说明 | 3915| --------------- | -------------- | 3916| [GZip](#gzip12) | GZip对象实例。 | 3917 3918**示例:** 3919 3920```ts 3921import { zlib } from '@kit.BasicServicesKit'; 3922 3923let gzip = zlib.createGZipSync(); 3924``` 3925 3926## GZip<sup>12+</sup> 3927 3928Gzip相关接口。 3929 3930### gzdopen<sup>12+</sup> 3931 3932gzdopen(fd: number, mode: string): Promise<void> 3933 3934将gzFile与文件描述符fd相关联,打开文件,用于进行读取并解压缩,或者压缩并写入。 3935 3936**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3937 3938**系统能力:** SystemCapability.BundleManager.Zlib 3939 3940**参数:** 3941 3942| 参数名 | 类型 | 必填 | 说明 | 3943| ------ | ------ | ---- | ------------------------------------------------------------ | 3944| fd | number | 是 | 文件描述符。通常情况下,通过系统调用“open”或其他方法获得的。 | 3945| mode | string | 是 | 用于指定访问模式。 | 3946 3947**返回值:** 3948 3949| 类型 | 说明 | 3950| ------------------- | ----------------------- | 3951| Promise<void> | Promise对象,无返回值。 | 3952 3953**错误码:** 3954 3955以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 3956 3957| 错误码ID | 错误信息 | 3958| -------- | ------------------------------------------------------------ | 3959| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 3960| 17800002 | No such file or access mode error. | 3961 3962**示例:** 3963 3964```ts 3965import { zlib } from '@kit.BasicServicesKit'; 3966import { fileIo as fs } from '@kit.CoreFileKit'; 3967 3968async function gzdopenDemo(pathDir: string) { 3969 fs.mkdirSync(pathDir + "/gzdopen"); 3970 let path = pathDir + "/gzdopen/test.gz"; 3971 let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 3972 let gzip = zlib.createGZipSync(); 3973 await gzip.gzdopen(file.fd, "wb"); 3974 await gzip.gzclose(); 3975} 3976 3977@Entry 3978@Component 3979struct Index { 3980 build() { 3981 Row() { 3982 Column() { 3983 Button('test gzip interface') 3984 .type(ButtonType.Capsule) 3985 .height(60) 3986 .width(200) 3987 .onClick(() => { 3988 let context = getContext(this); 3989 let pathDir = context.cacheDir; 3990 gzdopenDemo(pathDir); 3991 }) 3992 } 3993 .width('100%') 3994 } 3995 .height('100%') 3996 } 3997} 3998``` 3999 4000### gzbuffer<sup>12+</sup> 4001 4002gzbuffer(size: number):Promise<number> 4003 4004为当前库函数设置内部缓冲区尺寸。 4005 4006**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4007 4008**系统能力:** SystemCapability.BundleManager.Zlib 4009 4010**参数:** 4011 4012| 参数名 | 类型 | 必填 | 说明 | 4013| ------ | ------ | ---- | -------------------------- | 4014| size | number | 是 | 需要设置的内部缓冲区尺寸。 | 4015 4016**返回值:** 4017 4018| 类型 | 说明 | 4019| --------------------- | ---------------------------- | 4020| Promise<number> | Promise对象,成功时,返回0。 | 4021 4022**错误码:** 4023 4024以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 4025 4026| 错误码ID | 错误信息 | 4027| -------- | ------------------------------------------------------------ | 4028| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4029| 17800009 | Internal structure error. | 4030 4031**示例:** 4032 4033```ts 4034import { fileIo as fs } from '@kit.CoreFileKit'; 4035import { zlib } from '@kit.BasicServicesKit' 4036 4037async function gzbufferDemo(pathDir: string) { 4038 fs.mkdirSync(pathDir + "/gzbuffer"); 4039 let path = pathDir + "/gzbuffer/test.gz"; 4040 let gzip = zlib.createGZipSync(); 4041 await gzip.gzopen(path, "wb"); 4042 await gzip.gzclose(); 4043 await gzip.gzopen(path, "rb"); 4044 let result = await gzip.gzbuffer(648); 4045 await gzip.gzclose(); 4046} 4047 4048@Entry 4049@Component 4050struct Index { 4051 build() { 4052 Row() { 4053 Column() { 4054 Button('test gzip interface') 4055 .type(ButtonType.Capsule) 4056 .height(60) 4057 .width(200) 4058 .onClick(() => { 4059 let context = getContext(this); 4060 let pathDir = context.cacheDir; 4061 gzbufferDemo(pathDir); 4062 }) 4063 } 4064 .width('100%') 4065 } 4066 .height('100%') 4067 } 4068} 4069``` 4070 4071### gzopen<sup>12+</sup> 4072 4073gzopen(path: string, mode: string): Promise<void> 4074 4075打开位于指定路径的gzip(.gz)文件,用于进行读取并解压缩,或者压缩并写入。 4076 4077**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4078 4079**系统能力:** SystemCapability.BundleManager.Zlib 4080 4081**参数:** 4082 4083| 参数名 | 类型 | 必填 | 说明 | 4084| ------ | ------ | ---- | -------------------- | 4085| path | string | 是 | 需要打开的文件路径。 | 4086| mode | string | 是 | 指定文件打开方法。 | 4087 4088**返回值:** 4089 4090| 类型 | 说明 | 4091| ------------------- | ----------------------- | 4092| Promise<void> | Promise对象,无返回值。 | 4093 4094**错误码:** 4095 4096以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 4097 4098| 错误码ID | 错误信息 | 4099| -------- | ------------------------------------------------------------ | 4100| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4101| 17800002 | No such file or access mode error. | 4102 4103**示例:** 4104 4105```ts 4106import { zlib } from '@kit.BasicServicesKit'; 4107import { fileIo as fs } from '@kit.CoreFileKit'; 4108 4109async function gzopenDemo(pathDir: string) { 4110 fs.mkdirSync(pathDir + "/gzopen"); 4111 let path = pathDir + "/gzopen/test.gz"; 4112 let gzip = zlib.createGZipSync(); 4113 await gzip.gzopen(path, "wb"); 4114 await gzip.gzclose(); 4115} 4116 4117@Entry 4118@Component 4119struct Index { 4120 build() { 4121 Row() { 4122 Column() { 4123 Button('test gzip interface') 4124 .type(ButtonType.Capsule) 4125 .height(60) 4126 .width(200) 4127 .onClick(() => { 4128 let context = getContext(this); 4129 let pathDir = context.cacheDir; 4130 gzopenDemo(pathDir); 4131 }) 4132 } 4133 .width('100%') 4134 } 4135 .height('100%') 4136 } 4137} 4138``` 4139 4140### gzeof<sup>12+</sup> 4141 4142gzeof(): Promise<number> 4143 4144检查gzip压缩文件的读取位置是否已到达文件的末尾。 4145 4146**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4147 4148**系统能力:** SystemCapability.BundleManager.Zlib 4149 4150**返回值:** 4151 4152| 类型 | 说明 | 4153| --------------------- | ------------------------------------------------------------ | 4154| Promise<number> | Promise对象,如果在读取时设置了文件的文件结束指示符,则返回1。 | 4155 4156**示例:** 4157 4158```ts 4159import { zlib } from '@kit.BasicServicesKit'; 4160import { fileIo as fs } from '@kit.CoreFileKit'; 4161 4162async function gzeofDemo(pathDir: string) { 4163 fs.mkdirSync(pathDir + "/gzeof"); 4164 let path = pathDir + "/gzeof/test.gz"; 4165 let gzip = zlib.createGZipSync(); 4166 await gzip.gzopen(path, "wb"); 4167 let writeBufferWithData = new ArrayBuffer(16); 4168 let uint8View = new Uint8Array(writeBufferWithData); 4169 for (let i = 0; i < uint8View.length; i++) { 4170 uint8View[i] = i; 4171 } 4172 let writeNum = await gzip.gzwrite(writeBufferWithData, 16) 4173 await gzip.gzclose(); 4174 await gzip.gzopen(path, "rb"); 4175 let readBufferWithData = new ArrayBuffer(20); 4176 let readNum = await gzip.gzread(readBufferWithData); 4177 let eofNum = await gzip.gzeof(); 4178 await gzip.gzclose(); 4179} 4180 4181@Entry 4182@Component 4183struct Index { 4184 build() { 4185 Row() { 4186 Column() { 4187 Button('test gzip interface') 4188 .type(ButtonType.Capsule) 4189 .height(60) 4190 .width(200) 4191 .onClick(() => { 4192 let context = getContext(this); 4193 let pathDir = context.cacheDir; 4194 gzeofDemo(pathDir); 4195 }) 4196 } 4197 .width('100%') 4198 } 4199 .height('100%') 4200 } 4201} 4202``` 4203 4204### gzdirect<sup>12+</sup> 4205 4206gzdirect(): Promise<number> 4207 4208检查指定的gzip文件句柄文件是否直接访问原始未压缩数据,重新分配缓冲区。 4209 4210**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4211 4212**系统能力:** SystemCapability.BundleManager.Zlib 4213 4214**返回值:** 4215 4216| 类型 | 说明 | 4217| --------------------- | -------------------------------------------------- | 4218| Promise<number> | Promise对象,如果直接访问原始未压缩数据,则返回1。 | 4219 4220**示例:** 4221 4222```ts 4223import { zlib } from '@kit.BasicServicesKit'; 4224import { fileIo as fs } from '@kit.CoreFileKit'; 4225 4226async function gzdirectDemo(pathDir: string) { 4227 fs.mkdirSync(pathDir + "/gzdirect"); 4228 let path = pathDir + "/gzdirect/test.gz"; 4229 let gzip = zlib.createGZipSync(); 4230 await gzip.gzopen(path, "wb"); 4231 let directNum = await gzip.gzdirect(); 4232 await gzip.gzclose(); 4233} 4234 4235@Entry 4236@Component 4237struct Index { 4238 build() { 4239 Row() { 4240 Column() { 4241 Button('test gzip interface') 4242 .type(ButtonType.Capsule) 4243 .height(60) 4244 .width(200) 4245 .onClick(() => { 4246 let context = getContext(this); 4247 let pathDir = context.cacheDir; 4248 gzdirectDemo(pathDir); 4249 }) 4250 } 4251 .width('100%') 4252 } 4253 .height('100%') 4254 } 4255} 4256``` 4257 4258### gzclose<sup>12+</sup> 4259 4260gzclose(): Promise<ReturnStatus> 4261 4262清除文件的所有挂起输出,如有必要,关闭文件和释放(解)压缩状态。 4263 4264**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4265 4266**系统能力:** SystemCapability.BundleManager.Zlib 4267 4268**返回值:** 4269 4270| 类型 | 说明 | 4271| ------------------------------------------------- | --------------------------- | 4272| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象,返回结果状态。 | 4273 4274**错误码:** 4275 4276以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 4277 4278| 错误码ID | 错误信息 | 4279| -------- | ------------------------- | 4280| 17800004 | ZStream error. | 4281| 17800006 | Memory allocation failed. | 4282 4283**示例:** 4284 4285```ts 4286import { zlib } from '@kit.BasicServicesKit'; 4287import { fileIo as fs } from '@kit.CoreFileKit'; 4288 4289async function gzcloseDemo(pathDir: string) { 4290 fs.mkdirSync(pathDir + "/gzclose"); 4291 let path = pathDir + "/gzclose/test.gz"; 4292 let gzip = zlib.createGZipSync(); 4293 await gzip.gzopen(path, "wb"); 4294 await gzip.gzclose(); 4295} 4296 4297@Entry 4298@Component 4299struct Index { 4300 build() { 4301 Row() { 4302 Column() { 4303 Button('test gzip interface') 4304 .type(ButtonType.Capsule) 4305 .height(60) 4306 .width(200) 4307 .onClick(() => { 4308 let context = getContext(this); 4309 let pathDir = context.cacheDir; 4310 gzcloseDemo(pathDir); 4311 }) 4312 } 4313 .width('100%') 4314 } 4315 .height('100%') 4316 } 4317} 4318``` 4319 4320### gzclearerr<sup>12+</sup> 4321 4322gzclearerr(): Promise<void> 4323 4324清除文件的错误和文件结束标志。 4325 4326**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4327 4328**系统能力:** SystemCapability.BundleManager.Zlib 4329 4330**返回值:** 4331 4332| 类型 | 说明 | 4333| ------------------- | ----------------------- | 4334| Promise<void> | Promise对象,无返回值。 | 4335 4336**示例:** 4337 4338```ts 4339import { zlib } from '@kit.BasicServicesKit'; 4340import { fileIo as fs } from '@kit.CoreFileKit'; 4341 4342async function gzclearerrDemo(pathDir: string) { 4343 fs.mkdirSync(pathDir + "/gzclearerr"); 4344 let path = pathDir + "/gzclearerr/test.gz"; 4345 let gzip = zlib.createGZipSync(); 4346 await gzip.gzopen(path, "wb"); 4347 let writeBufferWithData = new ArrayBuffer(16); 4348 let uint8View = new Uint8Array(writeBufferWithData); 4349 for (let i = 0; i < uint8View.length; i++) { 4350 uint8View[i] = i; 4351 } 4352 let writeNum = await gzip.gzwrite(writeBufferWithData, 16) 4353 await gzip.gzclose(); 4354 await gzip.gzopen(path, "rb"); 4355 let readBufferWithData = new ArrayBuffer(20); 4356 let readNum = await gzip.gzread(readBufferWithData); 4357 let eofNum = await gzip.gzeof(); 4358 await gzip.gzclearerr(); 4359 let eofNumClear = await gzip.gzeof(); 4360 await gzip.gzclose(); 4361} 4362 4363@Entry 4364@Component 4365struct Index { 4366 build() { 4367 Row() { 4368 Column() { 4369 Button('test gzip interface') 4370 .type(ButtonType.Capsule) 4371 .height(60) 4372 .width(200) 4373 .onClick(() => { 4374 let context = getContext(this); 4375 let pathDir = context.cacheDir; 4376 gzclearerrDemo(pathDir); 4377 }) 4378 } 4379 .width('100%') 4380 } 4381 .height('100%') 4382 } 4383} 4384``` 4385 4386### gzerror<sup>12+</sup> 4387 4388gzerror(): Promise<GzErrorOutputInfo> 4389 4390文件上发生的最后一个错误的错误消息。 4391 4392**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4393 4394**系统能力:** SystemCapability.BundleManager.Zlib 4395 4396**返回值:** 4397 4398| 类型 | 说明 | 4399| ----------------------------------------------------------- | --------------------------------------------------------- | 4400| Promise<[GzErrorOutputInfo](#zipgzerroroutputinfo12)> | Promise对象,返回结果状态和出现的最后一个状态的状态消息。 | 4401 4402**错误码:** 4403 4404以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 4405 4406| 错误码ID | 错误信息 | 4407| -------- | -------------- | 4408| 17800004 | ZStream error. | 4409 4410**示例:** 4411 4412```ts 4413import { zlib } from '@kit.BasicServicesKit'; 4414import { fileIo as fs } from '@kit.CoreFileKit'; 4415 4416async function gzerrorDemo(pathDir: string) { 4417 fs.mkdirSync(pathDir + "/gzerror"); 4418 let path = pathDir + "/gzerror/test.gz"; 4419 let gzip = zlib.createGZipSync(); 4420 await gzip.gzopen(path, "wb"); 4421 let writeBufferWithData = new ArrayBuffer(16); 4422 let uint8View = new Uint8Array(writeBufferWithData); 4423 for (let i = 0; i < uint8View.length; i++) { 4424 uint8View[i] = i; 4425 } 4426 try { 4427 await gzip.gzwrite(writeBufferWithData, -1); 4428 } catch (errData) { 4429 await gzip.gzerror().then((GzErrorOutputInfo) => { 4430 console.info('errCode', GzErrorOutputInfo.status); 4431 console.info('errMsg', GzErrorOutputInfo.statusMsg); 4432 }) 4433 } 4434 await gzip.gzclose(); 4435} 4436 4437@Entry 4438@Component 4439struct Index { 4440 build() { 4441 Row() { 4442 Column() { 4443 Button('test gzip interface') 4444 .type(ButtonType.Capsule) 4445 .height(60) 4446 .width(200) 4447 .onClick(() => { 4448 let context = getContext(this); 4449 let pathDir = context.cacheDir; 4450 gzerrorDemo(pathDir); 4451 }) 4452 } 4453 .width('100%') 4454 } 4455 .height('100%') 4456 } 4457} 4458``` 4459 4460### gzgetc<sup>12+</sup> 4461 4462gzgetc(): Promise<number> 4463 4464从文件中读取并解压缩一个字节。 4465 4466**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4467 4468**系统能力:** SystemCapability.BundleManager.Zlib 4469 4470**返回值:** 4471 4472| 类型 | 说明 | 4473| --------------------- | ------------------------------------ | 4474| Promise<number> | Promise对象,返回读取字符的ASCII值。 | 4475 4476**错误码:** 4477 4478以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 4479 4480| 错误码ID | 错误信息 | 4481| -------- | ------------------------- | 4482| 17800009 | Internal structure error. | 4483 4484**示例:** 4485 4486```ts 4487import { zlib } from '@kit.BasicServicesKit'; 4488import { fileIo as fs } from '@kit.CoreFileKit'; 4489 4490async function gzgetcDemo(pathDir: string) { 4491 fs.mkdirSync(pathDir + "/gzgetc"); 4492 let path = pathDir + "/gzgetc/test.gz"; 4493 let gzip = zlib.createGZipSync(); 4494 await gzip.gzopen(path, "wb"); 4495 await gzip.gzputc(1); 4496 await gzip.gzclose(); 4497 await gzip.gzopen(path, "rb"); 4498 let resulit = await gzip.gzgetc(); 4499 await gzip.gzclose(); 4500} 4501 4502@Entry 4503@Component 4504struct Index { 4505 build() { 4506 Row() { 4507 Column() { 4508 Button('test gzip interface') 4509 .type(ButtonType.Capsule) 4510 .height(60) 4511 .width(200) 4512 .onClick(() => { 4513 let context = getContext(this); 4514 let pathDir = context.cacheDir; 4515 gzgetcDemo(pathDir); 4516 }) 4517 } 4518 .width('100%') 4519 } 4520 .height('100%') 4521 } 4522} 4523``` 4524 4525### gzflush<sup>12+</sup> 4526 4527gzflush(flush: CompressFlushMode): Promise<ReturnStatus> 4528 4529将所有挂起的输出刷新到文件中。 4530 4531**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4532 4533**系统能力:** SystemCapability.BundleManager.Zlib 4534 4535**参数:** 4536 4537| 参数名 | 类型 | 必填 | 说明 | 4538| ------ | ----------------- | ---- | ------------------------------------------------------------ | 4539| flush | CompressFlushMode | 是 | 控制刷新操作的行为,参考[CompressFlushMode枚举](#zipcompressflushmode12)的定义。 | 4540 4541**返回值:** 4542 4543| 类型 | 说明 | 4544| ------------------------------------------------- | --------------------------- | 4545| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象,返回结果状态。 | 4546 4547**错误码:** 4548 4549以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 4550 4551| 错误码ID | 错误信息 | 4552| -------- | ------------------------------------------------------------ | 4553| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4554| 17800004 | ZStream error. | 4555 4556**示例:** 4557 4558```ts 4559import { zlib } from '@kit.BasicServicesKit'; 4560import { fileIo as fs } from '@kit.CoreFileKit'; 4561 4562async function gzflushDemo(pathDir: string) { 4563 fs.mkdirSync(pathDir + "/gzflush"); 4564 let path = pathDir + "/gzflush/test.gz"; 4565 let gzip = zlib.createGZipSync(); 4566 await gzip.gzopen(path, "wb"); 4567 let flushNum = await gzip.gzflush(zlib.CompressFlushMode.NO_FLUSH); 4568 await gzip.gzclose(); 4569} 4570 4571@Entry 4572@Component 4573struct Index { 4574 build() { 4575 Row() { 4576 Column() { 4577 Button('test gzip interface') 4578 .type(ButtonType.Capsule) 4579 .height(60) 4580 .width(200) 4581 .onClick(() => { 4582 let context = getContext(this); 4583 let pathDir = context.cacheDir; 4584 gzflushDemo(pathDir); 4585 }) 4586 } 4587 .width('100%') 4588 } 4589 .height('100%') 4590 } 4591} 4592``` 4593 4594### gzfwrite<sup>12+</sup> 4595 4596gzfwrite(buf: ArrayBuffer, size: number, nitems: number): Promise<number> 4597 4598将大小为size,数量为nitems的数据块从buf压缩并写入文件。 4599 4600**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4601 4602**系统能力:** SystemCapability.BundleManager.Zlib 4603 4604**参数:** 4605 4606| 参数名 | 类型 | 必填 | 说明 | 4607| ------ | ----------- | ---- | ---------------------- | 4608| buf | ArrayBuffer | 是 | 要将数据写入的缓冲区。 | 4609| size | number | 是 | 单个数据块中的字节数。 | 4610| nitems | number | 是 | 要写入的数据块数。 | 4611 4612**返回值:** 4613 4614| 类型 | 说明 | 4615| --------------------- | --------------------------------------------------- | 4616| Promise<number> | Promise对象,返回写入大小为size的完整数据块的数目。 | 4617 4618**错误码:** 4619 4620以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 4621 4622| 错误码ID | 错误信息 | 4623| -------- | ------------------------------------------------------------ | 4624| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4625| 17800009 | Internal structure error. | 4626 4627**示例:** 4628 4629```ts 4630import { zlib } from '@kit.BasicServicesKit'; 4631import { fileIo as fs } from '@kit.CoreFileKit'; 4632 4633async function gzfwriteDemo(pathDir: string) { 4634 fs.mkdirSync(pathDir + "/gzfwrite"); 4635 let path = pathDir + "/gzfwrite/test.gz"; 4636 let gzip = zlib.createGZipSync(); 4637 await gzip.gzopen(path, "wb"); 4638 let bufferWithData = new ArrayBuffer(16); 4639 let uint8View = new Uint8Array(bufferWithData); 4640 for (let i = 0; i < uint8View.length; i++) { 4641 uint8View[i] = i; 4642 } 4643 let resulit = await gzip.gzfwrite(bufferWithData, 8, 2) 4644 await gzip.gzclose(); 4645} 4646 4647@Entry 4648@Component 4649struct Index { 4650 build() { 4651 Row() { 4652 Column() { 4653 Button('test gzip interface') 4654 .type(ButtonType.Capsule) 4655 .height(60) 4656 .width(200) 4657 .onClick(() => { 4658 let context = getContext(this); 4659 let pathDir = context.cacheDir; 4660 gzfwriteDemo(pathDir); 4661 }) 4662 } 4663 .width('100%') 4664 } 4665 .height('100%') 4666 } 4667} 4668``` 4669 4670### gzfread<sup>12+</sup> 4671 4672gzfread(buf: ArrayBuffer, size: number, nitems: number): Promise<number> 4673 4674从gzip压缩文件中解压缩并读取数据。 4675 4676**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4677 4678**系统能力:** SystemCapability.BundleManager.Zlib 4679 4680**参数:** 4681 4682| 参数名 | 类型 | 必填 | 说明 | 4683| ------ | ----------- | ---- | ------------------------------ | 4684| buf | ArrayBuffer | 是 | 用于存储读取结果的目标缓冲区。 | 4685| size | number | 是 | 单个数据块中的字节数。 | 4686| nitems | number | 是 | 要写入的数据块数。 | 4687 4688**返回值:** 4689 4690| 类型 | 说明 | 4691| --------------------- | --------------------------------------------------- | 4692| Promise<number> | Promise对象,返回读取大小为size的完整数据块的数目。 | 4693 4694**错误码:** 4695 4696以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 4697 4698| 错误码ID | 错误信息 | 4699| -------- | ------------------------------------------------------------ | 4700| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4701| 17800009 | Internal structure error. | 4702 4703**示例:** 4704 4705```ts 4706import { zlib } from '@kit.BasicServicesKit'; 4707import { fileIo as fs } from '@kit.CoreFileKit'; 4708 4709async function gzfreadDemo(pathDir: string) { 4710 fs.mkdirSync(pathDir + "/gzfread"); 4711 let path = pathDir + "/gzfread/test.gz"; 4712 let gzip = zlib.createGZipSync(); 4713 await gzip.gzopen(path, "wb"); 4714 let writeBuffer = new ArrayBuffer(16); 4715 let uint8View = new Uint8Array(writeBuffer); 4716 for (let i = 0; i < uint8View.length; i++) { 4717 uint8View[i] = i; 4718 } 4719 await gzip.gzfwrite(writeBuffer, 8, 2); 4720 await gzip.gzclose(); 4721 await gzip.gzopen(path, "rb"); 4722 let readBuffer = new ArrayBuffer(16); 4723 let result = await gzip.gzfread(readBuffer, 8, 2); 4724 await gzip.gzclose(); 4725} 4726 4727@Entry 4728@Component 4729struct Index { 4730 build() { 4731 Row() { 4732 Column() { 4733 Button('test gzip interface') 4734 .type(ButtonType.Capsule) 4735 .height(60) 4736 .width(200) 4737 .onClick(() => { 4738 let context = getContext(this); 4739 let pathDir = context.cacheDir; 4740 gzfreadDemo(pathDir); 4741 }) 4742 } 4743 .width('100%') 4744 } 4745 .height('100%') 4746 } 4747} 4748``` 4749 4750### gzclosew<sup>12+</sup> 4751 4752gzclosew(): Promise<ReturnStatus> 4753 4754与gzclose()功能相同,仅适用于写入或追加时。 4755 4756**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4757 4758**系统能力:** SystemCapability.BundleManager.Zlib 4759 4760**返回值:** 4761 4762| 类型 | 说明 | 4763| ------------------------------------------------- | --------------------------- | 4764| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象,返回结果状态。 | 4765 4766**错误码:** 4767 4768以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 4769 4770| 错误码ID | 错误信息 | 4771| -------- | ------------------------- | 4772| 17800004 | ZStream error. | 4773| 17800006 | Memory allocation failed. | 4774 4775**示例:** 4776 4777```ts 4778import { zlib } from '@kit.BasicServicesKit'; 4779import { fileIo as fs } from '@kit.CoreFileKit'; 4780 4781async function gzclosewDemo(pathDir: string) { 4782 fs.mkdirSync(pathDir + "/gzclosew"); 4783 let path = pathDir + "/gzclosew/test.gz"; 4784 let gzip = zlib.createGZipSync(); 4785 await gzip.gzopen(path, "wb"); 4786 await gzip.gzclosew(); 4787} 4788 4789@Entry 4790@Component 4791struct Index { 4792 build() { 4793 Row() { 4794 Column() { 4795 Button('test gzip interface') 4796 .type(ButtonType.Capsule) 4797 .height(60) 4798 .width(200) 4799 .onClick(() => { 4800 let context = getContext(this); 4801 let pathDir = context.cacheDir; 4802 gzclosewDemo(pathDir); 4803 }) 4804 } 4805 .width('100%') 4806 } 4807 .height('100%') 4808 } 4809} 4810``` 4811 4812### gzcloser<sup>12+</sup> 4813 4814gzcloser(): Promise<ReturnStatus> 4815 4816与gzclose()功能相同,仅适用于读取时。 4817 4818**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4819 4820**系统能力:** SystemCapability.BundleManager.Zlib 4821 4822**返回值:** 4823 4824| 类型 | 说明 | 4825| ------------------------------------------------- | --------------------------- | 4826| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象,返回结果状态。 | 4827 4828**错误码:** 4829 4830以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 4831 4832| 错误码ID | 错误信息 | 4833| -------- | -------------- | 4834| 17800004 | ZStream error. | 4835 4836**示例:** 4837 4838```ts 4839import { zlib } from '@kit.BasicServicesKit'; 4840import { fileIo as fs } from '@kit.CoreFileKit'; 4841 4842async function gzcloserDemo(pathDir: string) { 4843 fs.mkdirSync(pathDir + "/gzcloser"); 4844 let path = pathDir + "/gzcloser/test.gz"; 4845 let gzip = zlib.createGZipSync(); 4846 await gzip.gzopen(path, "wb"); 4847 await gzip.gzclose(); 4848 await gzip.gzopen(path, "rb"); 4849 await gzip.gzcloser(); 4850} 4851 4852@Entry 4853@Component 4854struct Index { 4855 build() { 4856 Row() { 4857 Column() { 4858 Button('test gzip interface') 4859 .type(ButtonType.Capsule) 4860 .height(60) 4861 .width(200) 4862 .onClick(() => { 4863 let context = getContext(this); 4864 let pathDir = context.cacheDir; 4865 gzcloserDemo(pathDir); 4866 }) 4867 } 4868 .width('100%') 4869 } 4870 .height('100%') 4871 } 4872} 4873``` 4874 4875### gzwrite<sup>12+</sup> 4876 4877gzwrite(buf: ArrayBuffer, len: number): Promise<number> 4878 4879将buf中的len长度的未压缩字节进行压缩并将其写入文件。 4880 4881**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4882 4883**系统能力:** SystemCapability.BundleManager.Zlib 4884 4885| 参数名 | 类型 | 必填 | 说明 | 4886| ------ | ----------- | ---- | ---------------------------- | 4887| buf | ArrayBuffer | 是 | 对象指向要写入的数据缓冲区。 | 4888| len | number | 是 | 未压缩字节长度。 | 4889 4890**返回值:** 4891 4892| 类型 | 说明 | 4893| --------------------- | ------------------------------------- | 4894| Promise<number> | Promise对象,返回写入的未压缩字节数。 | 4895 4896**错误码:** 4897 4898以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 4899 4900| 错误码ID | 错误信息 | 4901| -------- | ------------------------------------------------------------ | 4902| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4903| 17800009 | Internal structure error. | 4904 4905**示例:** 4906 4907```ts 4908import { zlib } from '@kit.BasicServicesKit'; 4909import { fileIo as fs } from '@kit.CoreFileKit'; 4910 4911async function gzwriteDemo(pathDir: string) { 4912 fs.mkdirSync(pathDir + "/gzwrite"); 4913 let path = pathDir + "/gzwrite/test.gz"; 4914 let gzip = zlib.createGZipSync(); 4915 await gzip.gzopen(path, "wb"); 4916 let bufferWithData = new ArrayBuffer(16); 4917 let uint8View = new Uint8Array(bufferWithData); 4918 for (let i = 0; i < uint8View.length; i++) { 4919 uint8View[i] = i; 4920 } 4921 let result = await gzip.gzwrite(bufferWithData, 16); 4922 await gzip.gzclose(); 4923} 4924 4925@Entry 4926@Component 4927struct Index { 4928 build() { 4929 Row() { 4930 Column() { 4931 Button('test gzip interface') 4932 .type(ButtonType.Capsule) 4933 .height(60) 4934 .width(200) 4935 .onClick(() => { 4936 let context = getContext(this); 4937 let pathDir = context.cacheDir; 4938 gzwriteDemo(pathDir); 4939 }) 4940 } 4941 .width('100%') 4942 } 4943 .height('100%') 4944 } 4945} 4946``` 4947 4948### gzungetc<sup>12+</sup> 4949 4950gzungetc(c: number): Promise<number> 4951 4952将c推回到流中,以便在下次读取文件时将作为第一个字符读取。 4953 4954**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4955 4956**系统能力:** SystemCapability.BundleManager.Zlib 4957 4958| 参数名 | 类型 | 必填 | 说明 | 4959| ------ | ------ | ---- | ------------------------ | 4960| c | number | 是 | 回退到输入流之前的字符。 | 4961 4962**返回值:** 4963 4964| 类型 | 说明 | 4965| --------------------- | ----------------------------- | 4966| Promise<number> | Promise对象,返回推送的字符。 | 4967 4968**错误码:** 4969 4970以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 4971 4972| 错误码ID | 错误信息 | 4973| -------- | ------------------------------------------------------------ | 4974| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 4975| 17800009 | Internal structure error. | 4976 4977**示例:** 4978 4979```ts 4980import { zlib } from '@kit.BasicServicesKit'; 4981import { fileIo as fs } from '@kit.CoreFileKit'; 4982 4983async function gzungetcDemo(pathDir: string) { 4984 fs.mkdirSync(pathDir + "/gzungetc"); 4985 let path = pathDir + "/gzungetc/test.gz"; 4986 let gzip = zlib.createGZipSync(); 4987 await gzip.gzopen(path, "wb"); 4988 await gzip.gzclose(); 4989 await gzip.gzopen(path, "rb"); 4990 await gzip.gzread(new ArrayBuffer(1)); 4991 let result = await gzip.gzungetc(1); 4992 await gzip.gzclose(); 4993} 4994 4995@Entry 4996@Component 4997struct Index { 4998 build() { 4999 Row() { 5000 Column() { 5001 Button('test gzip interface') 5002 .type(ButtonType.Capsule) 5003 .height(60) 5004 .width(200) 5005 .onClick(() => { 5006 let context = getContext(this); 5007 let pathDir = context.cacheDir; 5008 gzungetcDemo(pathDir); 5009 }) 5010 } 5011 .width('100%') 5012 } 5013 .height('100%') 5014 } 5015} 5016``` 5017 5018### gztell<sup>12+</sup> 5019 5020gztell(): Promise<number> 5021 5022返回文件中下一个gzread或gzwrite的起始位置。 5023 5024**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5025 5026**系统能力:** SystemCapability.BundleManager.Zlib 5027 5028**返回值:** 5029 5030| 类型 | 说明 | 5031| --------------------- | -------------------------------------------------------- | 5032| Promise<number> | Promise对象,返回文件种下一个gzread或gzwrite的起始位置。 | 5033 5034**错误码:** 5035 5036以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 5037 5038| 错误码ID | 错误信息 | 5039| -------- | ------------------------- | 5040| 17800009 | Internal structure error. | 5041 5042**示例:** 5043 5044```ts 5045import { zlib } from '@kit.BasicServicesKit'; 5046import { fileIo as fs } from '@kit.CoreFileKit'; 5047 5048async function gztellDemo(pathDir: string) { 5049 fs.mkdirSync(pathDir + "/gztell"); 5050 let path = pathDir + "/gztell/test.gz"; 5051 let gzip = zlib.createGZipSync(); 5052 await gzip.gzopen(path, "wb"); 5053 let result = await gzip.gztell(); 5054 await gzip.gzclose(); 5055} 5056 5057@Entry 5058@Component 5059struct Index { 5060 build() { 5061 Row() { 5062 Column() { 5063 Button('test gzip interface') 5064 .type(ButtonType.Capsule) 5065 .height(60) 5066 .width(200) 5067 .onClick(() => { 5068 let context = getContext(this); 5069 let pathDir = context.cacheDir; 5070 gztellDemo(pathDir); 5071 }) 5072 } 5073 .width('100%') 5074 } 5075 .height('100%') 5076 } 5077} 5078``` 5079 5080### gzsetparams<sup>12+</sup> 5081 5082gzsetparams(level: CompressLevel, strategy: CompressStrategy): Promise<ReturnStatus> 5083 5084动态更新文件的压缩级别和压缩策略。 5085 5086**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5087 5088**系统能力:** SystemCapability.BundleManager.Zlib 5089 5090**参数:** 5091 5092| 参数名 | 类型 | 必填 | 说明 | 5093| -------- | ---------------- | ---- | ------------------------------------------------------------ | 5094| level | CompressLevel | 是 | 压缩级别,参考[zip.CompressLevel枚举定义](#zipcompresslevel)。 | 5095| strategy | CompressStrategy | 是 | 压缩策略,参考[zip.CompressStrategy枚举定义](#zipcompressstrategy)。 | 5096 5097**返回值:** 5098 5099| 类型 | 说明 | 5100| ------------------------------------------------- | --------------------------- | 5101| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象,返回结果状态。 | 5102 5103**错误码:** 5104 5105以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5106 5107| 错误码ID | 错误信息 | 5108| -------- | ------------------------------------------------------------ | 5109| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5110| 17800004 | ZStream error. | 5111 5112**示例:** 5113 5114```ts 5115import { zlib } from '@kit.BasicServicesKit'; 5116import { fileIo as fs } from '@kit.CoreFileKit'; 5117 5118async function gzsetparamsDemo(pathDir: string) { 5119 fs.mkdirSync(pathDir + "/gzsetparams"); 5120 let path = pathDir + "/gzsetparams/test.gz"; 5121 let gzip = zlib.createGZipSync(); 5122 await gzip.gzopen(path, "wb"); 5123 let result = await gzip.gzsetparams(zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 5124 zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY); 5125 await gzip.gzclose(); 5126} 5127 5128@Entry 5129@Component 5130struct Index { 5131 build() { 5132 Row() { 5133 Column() { 5134 Button('test gzip interface') 5135 .type(ButtonType.Capsule) 5136 .height(60) 5137 .width(200) 5138 .onClick(() => { 5139 let context = getContext(this); 5140 let pathDir = context.cacheDir; 5141 gzsetparamsDemo(pathDir); 5142 }) 5143 } 5144 .width('100%') 5145 } 5146 .height('100%') 5147 } 5148} 5149``` 5150 5151### gzseek<sup>12+</sup> 5152 5153gzseek(offset: number, whence: OffsetReferencePoint): Promise<number> 5154 5155将起始位置设置为相对于文件中下一个gzread或gzwrite的偏移位置。 5156 5157**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5158 5159**系统能力:** SystemCapability.BundleManager.Zlib 5160 5161**参数:** 5162 5163| 参数名 | 类型 | 必填 | 说明 | 5164| ------ | -------------------- | ---- | ------------------------------------------------------------ | 5165| offset | number | 是 | 目标偏移位置。 | 5166| whence | OffsetReferencePoint | 是 | 定义偏移的参考点,参考[zip.OffsetReferencePoint枚举定义](#zipoffsetreferencepoint12)。 | 5167 5168**返回值:** 5169 5170| 类型 | 说明 | 5171| --------------------- | ------------------------------------------------------------ | 5172| Promise<number> | Promise对象,返回从未压缩流开始以字节为单位测量的结果偏移位置。 | 5173 5174**错误码:** 5175 5176以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5177 5178| 错误码ID | 错误信息 | 5179| -------- | ------------------------------------------------------------ | 5180| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5181| 17800009 | Internal structure error. | 5182 5183**示例:** 5184 5185```ts 5186import { zlib } from '@kit.BasicServicesKit'; 5187import { fileIo as fs } from '@kit.CoreFileKit'; 5188 5189async function gzseekDemo(pathDir: string) { 5190 fs.mkdirSync(pathDir + "/gzseek"); 5191 let path = pathDir + "/gzseek/test.gz"; 5192 let gzip = zlib.createGZipSync(); 5193 await gzip.gzopen(path, "wb"); 5194 let result = await gzip.gzseek(2, zlib.OffsetReferencePoint.SEEK_CUR); 5195 await gzip.gzclose(); 5196} 5197 5198@Entry 5199@Component 5200struct Index { 5201 build() { 5202 Row() { 5203 Column() { 5204 Button('test gzip interface') 5205 .type(ButtonType.Capsule) 5206 .height(60) 5207 .width(200) 5208 .onClick(() => { 5209 let context = getContext(this); 5210 let pathDir = context.cacheDir; 5211 gzseekDemo(pathDir); 5212 }) 5213 } 5214 .width('100%') 5215 } 5216 .height('100%') 5217 } 5218} 5219``` 5220 5221### gzrewind<sup>12+</sup> 5222 5223gzrewind(): Promise<ReturnStatus> 5224 5225将文件指针重新定位到文件的开头,此功能仅用于读取。 5226 5227**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5228 5229**系统能力:** SystemCapability.BundleManager.Zlib 5230 5231**返回值:** 5232 5233| 类型 | 说明 | 5234| ------------------------------------------------- | --------------------------- | 5235| Promise<[ReturnStatus](#zipreturnstatus12)> | Promise对象,返回结果状态。 | 5236 5237**错误码:** 5238 5239以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 5240 5241| 错误码ID | 错误信息 | 5242| -------- | ------------------------- | 5243| 17800009 | Internal structure error. | 5244 5245**示例:** 5246 5247```ts 5248import { zlib } from '@kit.BasicServicesKit'; 5249import { fileIo as fs } from '@kit.CoreFileKit'; 5250 5251async function gzrewindDemo(pathDir: string) { 5252 fs.mkdirSync(pathDir + "/gzrewind"); 5253 let path = pathDir + "/gzrewind/test.gz"; 5254 let gzip = zlib.createGZipSync(); 5255 await gzip.gzopen(path, "wb"); 5256 await gzip.gzclose(); 5257 await gzip.gzopen(path, "rb"); 5258 let result = await gzip.gzrewind(); 5259 await gzip.gzclose(); 5260} 5261 5262@Entry 5263@Component 5264struct Index { 5265 build() { 5266 Row() { 5267 Column() { 5268 Button('test gzip interface') 5269 .type(ButtonType.Capsule) 5270 .height(60) 5271 .width(200) 5272 .onClick(() => { 5273 let context = getContext(this); 5274 let pathDir = context.cacheDir; 5275 gzrewindDemo(pathDir); 5276 }) 5277 } 5278 .width('100%') 5279 } 5280 .height('100%') 5281 } 5282} 5283``` 5284 5285### gzread<sup>12+</sup> 5286 5287gzread(buf: ArrayBuffer): Promise<number> 5288 5289从文件中读取最多len个未压缩字节并将其解压缩到buf中。 5290 5291**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5292 5293**系统能力:** SystemCapability.BundleManager.Zlib 5294 5295**参数:** 5296 5297| 参数名 | 类型 | 必填 | 说明 | 5298| ------ | ----------- | ---- | -------------- | 5299| buf | ArrayBuffer | 是 | 目标偏移位置。 | 5300 5301**返回值:** 5302 5303| 类型 | 说明 | 5304| --------------------- | ----------------------------------------- | 5305| Promise<number> | Promise对象,返回实际读取的未压缩字节数。 | 5306 5307**错误码:** 5308 5309以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5310 5311| 错误码ID | 错误信息 | 5312| -------- | ------------------------------------------------------------ | 5313| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5314| 17800009 | Internal structure error. | 5315 5316**示例:** 5317 5318```ts 5319import { zlib } from '@kit.BasicServicesKit'; 5320import { fileIo as fs } from '@kit.CoreFileKit'; 5321 5322async function gzreadDemo(pathDir: string) { 5323 fs.mkdirSync(pathDir + "/gzread"); 5324 let path = pathDir + "/gzread/test.gz"; 5325 let gzip = zlib.createGZipSync(); 5326 await gzip.gzopen(path, "wb"); 5327 let writeBuffer = new ArrayBuffer(16); 5328 let uint8View = new Uint8Array(writeBuffer); 5329 for (let i = 0; i < uint8View.length; i++) { 5330 uint8View[i] = i; 5331 } 5332 await gzip.gzwrite(writeBuffer, 16); 5333 await gzip.gzclose(); 5334 await gzip.gzopen(path, "rb"); 5335 let readBuffer = new ArrayBuffer(16); 5336 let result = await gzip.gzread(readBuffer); 5337 await gzip.gzclose(); 5338} 5339 5340@Entry 5341@Component 5342struct Index { 5343 build() { 5344 Row() { 5345 Column() { 5346 Button('test gzip interface') 5347 .type(ButtonType.Capsule) 5348 .height(60) 5349 .width(200) 5350 .onClick(() => { 5351 let context = getContext(this); 5352 let pathDir = context.cacheDir; 5353 gzreadDemo(pathDir); 5354 }) 5355 } 5356 .width('100%') 5357 } 5358 .height('100%') 5359 } 5360} 5361``` 5362 5363### gzputs<sup>12+</sup> 5364 5365gzputs(str: string): Promise<number> 5366 5367压缩给定的以null结尾的字符串并将其写入文件,不包括终止的null字符。 5368 5369**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5370 5371**系统能力:** SystemCapability.BundleManager.Zlib 5372 5373**参数:** 5374 5375| 参数名 | 类型 | 必填 | 说明 | 5376| ------ | ------ | ---- | ---------------------- | 5377| str | string | 是 | 格式化描述符和纯文本。 | 5378 5379**返回值:** 5380 5381| 类型 | 说明 | 5382| --------------------- | ------------------------------- | 5383| Promise<number> | Promise对象,返回写入的字符数。 | 5384 5385**错误码:** 5386 5387以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5388 5389| 错误码ID | 错误信息 | 5390| -------- | ------------------------------------------------------------ | 5391| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5392| 17800009 | Internal structure error. | 5393 5394**示例:** 5395 5396```ts 5397import { zlib } from '@kit.BasicServicesKit'; 5398import { fileIo as fs } from '@kit.CoreFileKit'; 5399 5400async function gzputsDemo(pathDir: string) { 5401 fs.mkdirSync(pathDir + "/gzputs"); 5402 let path = pathDir + "/gzputs/test.gz"; 5403 let gzip = zlib.createGZipSync(); 5404 await gzip.gzopen(path, "wb"); 5405 let result = await gzip.gzputs("hello"); 5406 await gzip.gzclose(); 5407} 5408 5409@Entry 5410@Component 5411struct Index { 5412 build() { 5413 Row() { 5414 Column() { 5415 Button('test gzip interface') 5416 .type(ButtonType.Capsule) 5417 .height(60) 5418 .width(200) 5419 .onClick(() => { 5420 let context = getContext(this); 5421 let pathDir = context.cacheDir; 5422 gzputsDemo(pathDir); 5423 }) 5424 } 5425 .width('100%') 5426 } 5427 .height('100%') 5428 } 5429} 5430``` 5431 5432### gzputc<sup>12+</sup> 5433 5434gzputc(char: number): Promise<number> 5435 5436将转换为无符号字符的c压缩并写入文件。 5437 5438**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5439 5440**系统能力:** SystemCapability.BundleManager.Zlib 5441 5442**参数:** 5443 5444| 参数名 | 类型 | 必填 | 说明 | 5445| ------ | ------ | ---- | --------------- | 5446| char | number | 是 | 写入字符ASCII。 | 5447 5448**返回值:** 5449 5450| 类型 | 说明 | 5451| --------------------- | ----------------------------- | 5452| Promise<number> | Promise对象,返回已写入的值。 | 5453 5454**错误码:** 5455 5456以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5457 5458| 错误码ID | 错误信息 | 5459| -------- | ------------------------------------------------------------ | 5460| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5461| 17800009 | Internal structure error. | 5462 5463**示例:** 5464 5465```ts 5466import { zlib } from '@kit.BasicServicesKit'; 5467import { fileIo as fs } from '@kit.CoreFileKit'; 5468 5469async function gzputcDemo(pathDir: string) { 5470 fs.mkdirSync(pathDir + "/gzputc"); 5471 let path = pathDir + "/gzputc/test.gz"; 5472 let gzip = zlib.createGZipSync(); 5473 await gzip.gzopen(path, "wb"); 5474 let result = await gzip.gzputc(0); 5475 await gzip.gzclose(); 5476} 5477 5478@Entry 5479@Component 5480struct Index { 5481 build() { 5482 Row() { 5483 Column() { 5484 Button('test gzip interface') 5485 .type(ButtonType.Capsule) 5486 .height(60) 5487 .width(200) 5488 .onClick(() => { 5489 let context = getContext(this); 5490 let pathDir = context.cacheDir; 5491 gzputcDemo(pathDir); 5492 }) 5493 } 5494 .width('100%') 5495 } 5496 .height('100%') 5497 } 5498} 5499``` 5500 5501### gzprintf<sup>12+</sup> 5502 5503gzprintf(format: string, ...args: Array<string | number>): Promise<number> 5504 5505在字符串格式的控制下,将参数转换和格式化后,压缩并写入文件,如fprintf中所示。 5506 5507**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5508 5509**系统能力:** SystemCapability.BundleManager.Zlib 5510 5511**参数:** 5512 5513| 参数名 | 类型 | 必填 | 说明 | 5514| ------ | ----------------------------- | ---- | ---------------------- | 5515| format | string | 是 | 格式化描述符和纯文本。 | 5516| args | Array<string \| number> | 否 | 可变参数列表。 | 5517 5518**返回值:** 5519 5520| 类型 | 说明 | 5521| --------------------- | ----------------------------------------- | 5522| Promise<number> | Promise对象,返回实际写入的未压缩字节数。 | 5523 5524**错误码:** 5525 5526以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5527 5528| 错误码ID | 错误信息 | 5529| -------- | ------------------------------------------------------------ | 5530| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5531| 17800004 | ZStream error. | 5532| 17800009 | Internal structure error. | 5533 5534**示例:** 5535 5536```ts 5537import { zlib } from '@kit.BasicServicesKit'; 5538import { fileIo as fs } from '@kit.CoreFileKit'; 5539 5540async function gzprintfDemo(pathDir: string) { 5541 fs.mkdirSync(pathDir + "/gzprintf"); 5542 let path = pathDir + "/gzprintf/test.gz"; 5543 let gzip = zlib.createGZipSync(); 5544 await gzip.gzopen(path, "wb"); 5545 let result = await gzip.gzprintf("name is %s, age is %d", "Tom", 23); 5546 await gzip.gzclose(); 5547} 5548 5549@Entry 5550@Component 5551struct Index { 5552 build() { 5553 Row() { 5554 Column() { 5555 Button('test gzip interface') 5556 .type(ButtonType.Capsule) 5557 .height(60) 5558 .width(200) 5559 .onClick(() => { 5560 let context = getContext(this); 5561 let pathDir = context.cacheDir; 5562 gzprintfDemo(pathDir); 5563 }) 5564 } 5565 .width('100%') 5566 } 5567 .height('100%') 5568 } 5569} 5570``` 5571 5572### gzoffset<sup>12+</sup> 5573 5574gzoffset(): Promise<number> 5575 5576返回文件的当前压缩(实际)读或写偏移量。 5577 5578**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5579 5580**系统能力:** SystemCapability.BundleManager.Zlib 5581 5582**返回值:** 5583 5584| 类型 | 说明 | 5585| --------------------- | ----------------------------------------------------- | 5586| Promise<number> | Promise对象,返回文件的当前压缩(实际)读或写偏移量。 | 5587 5588**错误码:** 5589 5590以下错误码的详细介绍请参见[ohos.zlib错误码](./errorcode-zlib.md)。 5591 5592| 错误码ID | 错误信息 | 5593| -------- | ------------------------- | 5594| 17800009 | Internal structure error. | 5595 5596**示例:** 5597 5598```ts 5599import { zlib } from '@kit.BasicServicesKit'; 5600import { fileIo as fs } from '@kit.CoreFileKit'; 5601 5602async function gzoffsetDemo(pathDir: string) { 5603 fs.mkdirSync(pathDir + "/gzoffset"); 5604 let path = pathDir + "/gzoffset/test.gz"; 5605 let gzip = zlib.createGZipSync(); 5606 await gzip.gzopen(path, "wb"); 5607 let result = await gzip.gzoffset(); 5608 await gzip.gzclose(); 5609} 5610 5611@Entry 5612@Component 5613struct Index { 5614 build() { 5615 Row() { 5616 Column() { 5617 Button('test gzip interface') 5618 .type(ButtonType.Capsule) 5619 .height(60) 5620 .width(200) 5621 .onClick(() => { 5622 let context = getContext(this); 5623 let pathDir = context.cacheDir; 5624 gzoffsetDemo(pathDir); 5625 }) 5626 } 5627 .width('100%') 5628 } 5629 .height('100%') 5630 } 5631} 5632``` 5633 5634### gzgets<sup>12+</sup> 5635 5636gzgets(buf: ArrayBuffer): Promise<string> 5637 5638从文件中读取字节并将其解压缩到buf中,直到读取len-1字符,或者直到读取换行符并将其传输到buf,或者遇到文件结束条件。 5639 5640**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5641 5642**系统能力:** SystemCapability.BundleManager.Zlib 5643 5644**参数:** 5645 5646| 参数名 | 类型 | 必填 | 说明 | 5647| ------ | ----------- | ---- | ------------------ | 5648| buf | ArrayBuffer | 是 | 存储读取的行数据。 | 5649 5650**返回值:** 5651 5652| 类型 | 说明 | 5653| --------------------- | ------------------------------------- | 5654| Promise<string> | Promise对象,返回以null结尾的字符串。 | 5655 5656**错误码:** 5657 5658以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.zlib错误码](./errorcode-zlib.md)。 5659 5660| 错误码ID | 错误信息 | 5661| -------- | ------------------------------------------------------------ | 5662| 401 | The parameter check failed. Possible causes: <br />1. Mandatory parameters are left unspecified;<br />2. Incorrect parameter types;<br />3. Parameter verification failed. | 5663| 17800009 | Internal structure error. | 5664 5665**示例:** 5666 5667```ts 5668import { zlib } from '@kit.BasicServicesKit'; 5669import { fileIo as fs } from '@kit.CoreFileKit'; 5670 5671async function gzgetsDemo(pathDir: string) { 5672 fs.mkdirSync(pathDir + "/gzgets"); 5673 let path = pathDir + "/gzgets/test.gz"; 5674 let gzip = zlib.createGZipSync(); 5675 await gzip.gzopen(path, "wb"); 5676 await gzip.gzputs("hello"); 5677 await gzip.gzclose(); 5678 await gzip.gzopen(path, "rb"); 5679 let bufferWithData = new ArrayBuffer(16); 5680 let result = await gzip.gzgets(bufferWithData); 5681 await gzip.gzclose(); 5682} 5683 5684@Entry 5685@Component 5686struct Index { 5687 build() { 5688 Row() { 5689 Column() { 5690 Button('test gzip interface') 5691 .type(ButtonType.Capsule) 5692 .height(60) 5693 .width(200) 5694 .onClick(() => { 5695 let context = getContext(this); 5696 let pathDir = context.cacheDir; 5697 gzgetsDemo(pathDir); 5698 }) 5699 } 5700 .width('100%') 5701 } 5702 .height('100%') 5703 } 5704} 5705``` 5706 5707## zip.GzErrorOutputInfo<sup>12+</sup> 5708 5709**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5710 5711**系统能力:** SystemCapability.BundleManager.Zlib 5712 5713| 名称 | 类型 | 可读 | 可写 | 说明 | 5714| --------- | ------------ | ---- | ---- | ------------------------------------------------ | 5715| status | ReturnStatus | 是 | 否 | 返回zlib文件状态码,参考zip.ReturnStatus的定义。 | 5716| statusMsg | string | 是 | 否 | zlib文件上发生的最后一个状态的状态消息。 | 5717 5718## zip.OffsetReferencePoint<sup>12+</sup> 5719 5720**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5721 5722**系统能力:** SystemCapability.BundleManager.Zlib 5723 5724| 名称 | 值 | 说明 | 5725| -------- | ---- | ---------------- | 5726| SEEK_SET | 0 | 从文件开头查找。 | 5727| SEEK_CUR | 1 | 从当前位置查找。 |