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