1# @ohos.zlib (Zip模块) 2 3本模块提供压缩解压缩文件的能力 4 5> **说明:** 6> 7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```javascript 12import zlib from '@ohos.zlib'; 13``` 14 15## zlib.zipFile<sup>(deprecated)</sup> 16zipFile(inFile: string, outFile: string, options: Options): Promise<void> 17 18压缩接口(Promise形式)。 19 20> 从api9开始不再维护,建议使用[zlib.compressFile](#zlibcompressfile9) 21 22**系统能力:** SystemCapability.BundleManager.Zlib 23 24**参数:** 25 26| 参数名 | 类型 | 必填 | 说明 | 27| ------- | ------------------- | ---- | ------------------------------------------------------------ | 28| inFile | string | 是 | 指定压缩的文件夹路径或者文件路径,路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。 | 29| outFile | string | 是 | 指定压缩结果的文件路径(文件的扩展名zip)。 | 30| options | [Options](#options) | 是 | 压缩的可选参数。 | 31 32**返回值:** 33 34| 类型 | 说明 | 35| -------------- | ------------------------------------------------------------ | 36| Promise\<void> | [ERROR_CODE_OK](#ziperrorcode):压缩成功;<br />[ERROR_CODE_ERRNO](#ziperrorcode):压缩失败。 | 37 38**示例1:** 39 40```typescript 41//【压缩文件 例子1】 42// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过context获取 43import zlib from '@ohos.zlib'; 44import { BusinessError } from '@ohos.base'; 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.log('zipFile result is ' + JSON.stringify(data)); 56}).catch((err: BusinessError) => { 57 console.log('error is ' + JSON.stringify(err)); 58}); 59``` 60 61**示例2:** 62 63```typescript 64// 【压缩文件夹 例子2】 65// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过context获取 66import zlib from '@ohos.zlib'; 67import { BusinessError } from '@ohos.base'; 68 69let inFile = '/xxx/xxx'; 70let outFile = '/xxx/xxx.zip'; 71let options: zlib.Options = { 72 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 73 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 74 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 75}; 76 77zlib.zipFile(inFile , outFile, options).then((data: void) => { 78 console.log('zipFile result is ' + JSON.stringify(data)); 79}).catch((err: BusinessError)=>{ 80 console.log('error is ' + JSON.stringify(err)); 81}); 82``` 83 84## zlib.unzipFile<sup>(deprecated)</sup> 85 86unzipFile(inFile:string, outFile:string, options: Options): Promise<void> 87 88解压文件,解压完成返回执行结果(Promise形式)。 89 90> 从api9开始不再看护,建议使用[zlib.decompressFile](#zlibdecompressfile9) 91 92**系统能力:** SystemCapability.BundleManager.Zlib 93 94**参数:** 95 96| 参数名 | 类型 | 必填 | 说明 | 97| ------- | ------------------- | ---- | ------------------------------------------------------------ | 98| inFile | string | 是 | 指定的待解压缩文件的文件路径,路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。 | 99| outFile | string | 是 | 指定的解压文件路径。 | 100| options | [Options](#options) | 是 | 解压的可选参数。 | 101 102**返回值:** 103 104| 类型 | 说明 | 105| -------------- | ------------------------------------------------------------ | 106| Promise\<void> | [ERROR_CODE_OK](#ziperrorcode):解压成功;<br />[ERROR_CODE_ERRNO](#ziperrorcode):解压失败返回执行结果。 | 107 108**示例:** 109 110```typescript 111// 【解压缩 例子1】 112// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过context获取 113import zlib from '@ohos.zlib'; 114import { BusinessError } from '@ohos.base'; 115 116let inFile = '/xx/xxx.zip'; 117let outFile = '/xxx'; 118 119let options: zlib.Options = { 120 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 121 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 122 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 123}; 124zlib.unzipFile(inFile, outFile, options).then((data: void) => { 125 console.log('unzipFile result is ' + JSON.stringify(data)); 126}).catch((err: BusinessError)=>{ 127 console.log('error is ' + JSON.stringify(err)); 128}) 129``` 130 131## zlib.compressFile<sup>9+</sup> 132 133compressFile(inFile: string, outFile: string, options: Options, callback: AsyncCallback\<void>): void; 134 135压缩文件,压缩的结果通过callback返回。成功时返回null,失败时返回错误码ID。 136 137**系统能力:** SystemCapability.BundleManager.Zlib 138 139**参数:** 140 141| 参数名 | 类型 | 必填 | 说明 | 142| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ | 143| inFile | string | 是 | 指定压缩的文件夹路径或者文件路径,路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。 | 144| outFile | string | 是 | 指定的压缩结果的文件路径。 | 145| options | [Options](#options) | 是 | 压缩的配置参数。 | 146| AsyncCallback<**void**> | callback | 否 | 压缩时的回调函数。 | 147 148**错误码:** 149 150以下错误码的详细介绍请参见[ohos.zlib错误码](../errorcodes/errorcode-zlib.md)。 151 152| 错误码ID | 错误信息 | 153| -------- | --------------------------------------| 154| 900001 | The input source file is invalid. | 155| 900002 | The input destination file is invalid. | 156 157**示例** 158 159```typescript 160// 【压缩文件 例子1】 161// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过context获取 162import zlib from '@ohos.zlib'; 163import { BusinessError } from '@ohos.base'; 164 165let inFile = '/xxx/filename.xxx'; 166let outFile = '/xxx/xxx.zip'; 167let options: zlib.Options = { 168 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 169 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 170 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 171}; 172 173try { 174 zlib.compressFile(inFile, outFile, options, (errData: BusinessError) => { 175 if (errData !== null) { 176 console.log(`errData is errCode:${errData.code} message:${errData.message}`); 177 } 178 }) 179} catch(errData) { 180 let code = (errData as BusinessError).code; 181 let message = (errData as BusinessError).message; 182 console.log(`errData is errCode:${code} message:${message}`); 183} 184``` 185 186## zlib.compressFile<sup>9+</sup> 187 188compressFile(inFile: string, outFile: string, options: Options): Promise\<void>; 189 190压缩文件,压缩的结果通过promise返回,成功时返回null,失败时返回错误码。 191 192**系统能力:** SystemCapability.BundleManager.Zlib 193 194**参数:** 195 196| 参数名 | 类型 | 必填 | 说明 | 197| ------- | ------------------- | ---- | ------------------------------------------------------------ | 198| inFile | string | 是 | 指定压缩的文件夹路径或者文件路径,路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。 | 199| outFile | string | 是 | 指定的压缩结果的文件路径。 | 200| options | [Options](#options) | 是 | 压缩的配置参数。 | 201 202**错误码:** 203 204以下错误码的详细介绍请参见[ohos.zlib错误码](../errorcodes/errorcode-zlib.md)。 205 206| 错误码ID | 错误信息 | 207| -------- | ------------------------------------- | 208| 900001 | The input source file is invalid. | 209| 900002 | The input destination file is invalid. | 210 211```typescript 212// 【压缩文件 例子2】 213// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过context获取 214import zlib from '@ohos.zlib'; 215import { BusinessError } from '@ohos.base'; 216 217let inFile = '/xxx/filename.xxx'; 218let outFile = '/xxx/xxx.zip'; 219let options: zlib.Options = { 220 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 221 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 222 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 223}; 224 225try { 226 zlib.compressFile(inFile, outFile, options).then((data: void) => { 227 console.info('compressFile success'); 228 }).catch((errData: BusinessError) => { 229 console.log(`errData is errCode:${errData.code} message:${errData.message}`); 230 }) 231} catch(errData) { 232 let code = (errData as BusinessError).code; 233 let message = (errData as BusinessError).message; 234 console.log(`errData is errCode:${code} message:${message}`); 235} 236``` 237 238## zlib.decompressFile<sup>9+</sup> 239 240decompressFile(inFile: string, outFile: string, options: Options, callback: AsyncCallback\<void>): void; 241 242解压文件,解压的结果通过callback返回,成功时返回null,失败时返回错误码。 243 244**系统能力:** SystemCapability.BundleManager.Zlib 245 246**参数:** 247 248| 参数名 | 类型 | 必填 | 说明 | 249| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ | 250| inFile | string | 是 | 指定的待解压缩文件的文件路径,文件后缀需要以.zip结尾。文件路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。 | 251| outFile | string | 是 | 指定的解压后的文件夹路径,文件夹目录路径需要在系统中存在,不存在则会解压失败。路径必须为沙箱路径,沙箱路径可以通过context获取,具体方法可参考[application/context(Stage模型)](js-apis-inner-application-context.md)或 [app/context(FA模型)](js-apis-inner-app-context.md)。如果待解压的文件或文件夹在解压后的路径下已经存在,则会直接覆盖同名文件或同名文件夹中的同名文件。 | 252| options | [Options](#options) | 是 | 解压的配置参数。 | 253| AsyncCallback<**void**> | callback | 是 | 解压的回调函数。 | 254 255**错误码:** 256 257以下错误码的详细介绍请参见[ohos.zlib错误码](../errorcodes/errorcode-zlib.md)。 258 259| 错误码ID | 错误信息 | 260| -------- | --------------------------------------| 261| 900001 | The input source file is invalid. | 262| 900002 | The input destination file is invalid. | 263| 900003 | The input source file is not ZIP format or damaged. | 264 265**示例** 266 267```typescript 268// 【解压缩 例子1】 269// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过context获取 270import zlib from '@ohos.zlib'; 271import { BusinessError } from '@ohos.base'; 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.log(`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.log(`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**系统能力:** SystemCapability.BundleManager.Zlib 299 300**参数:** 301 302| 参数名 | 类型 | 必填 | 说明 | 303| ------- | ------------------- | ---- | ------------------------------------------------------------ | 304| inFile | string | 是 | 指定的待解压缩文件的文件路径,文件后缀需要以.zip结尾。文件路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。 | 305| outFile | string | 是 | 指定的解压后的文件夹路径,文件夹目录路径需要在系统中存在,不存在则会解压失败。路径必须为沙箱路径,沙箱路径可以通过context获取,具体方法可参考[application/context(Stage模型)](js-apis-inner-application-context.md)或 [app/context(FA模型)](js-apis-inner-app-context.md)。如果待解压的文件或文件夹在解压后的路径下已经存在,则会直接覆盖同名文件或同名文件夹中的同名文件。 | 306| options | [Options](#options) | 否 | 解压时的配置参数。 | 307 308**错误码:** 309 310以下错误码的详细介绍请参见[ohos.zlib错误码](../errorcodes/errorcode-zlib.md)。 311 312| 错误码ID | 错误信息 | 313| ------ | ------------------------------------- | 314| 900001 | The input source file is invalid. | 315| 900002 | The input destination file is invalid. | 316| 900003 | The input source file is not ZIP format or damaged. | 317 318```typescript 319// 【解压缩 例子2】 320// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过context获取 321import zlib from '@ohos.zlib'; 322import { BusinessError } from '@ohos.base'; 323 324let inFile = '/xx/xxx.zip'; 325let outFileDir = '/xxx'; 326let options: zlib.Options = { 327 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION 328}; 329 330try { 331 zlib.decompressFile(inFile, outFileDir, options).then((data: void) => { 332 console.info('decompressFile success'); 333 }).catch((errData: BusinessError) => { 334 console.log(`errData is errCode:${errData.code} message:${errData.message}`); 335 }) 336} catch(errData) { 337 let code = (errData as BusinessError).code; 338 let message = (errData as BusinessError).message; 339 console.log(`errData is errCode:${code} message:${message}`); 340} 341``` 342 343## zlib.decompressFile<sup>10+</sup> 344 345decompressFile(inFile: string, outFile: string, callback: AsyncCallback\<void\>): void; 346 347解压文件,解压的结果通过callback返回,成功时返回null,失败时返回错误码。 348 349**系统能力:** SystemCapability.BundleManager.Zlib 350 351**参数:** 352 353| 参数名 | 类型 | 必填 | 说明 | 354| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ | 355| inFile | string | 是 | 指定的待解压缩文件的文件路径,文件后缀需要以.zip结尾。文件路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。 | 356| outFile | string | 是 | 指定的解压后的文件夹路径,文件夹目录路径需要在系统中存在,不存在则会解压失败。路径必须为沙箱路径,沙箱路径可以通过context获取,具体方法可参考[application/context(Stage模型)](js-apis-inner-application-context.md)或 [app/context(FA模型)](js-apis-inner-app-context.md)。如果待解压的文件或文件夹在解压后的路径下已经存在,则会直接覆盖同名文件或同名文件夹中的同名文件。 | 357| AsyncCallback<**void**> | callback | 是 | 解压的回调函数。 | 358 359**错误码:** 360 361以下错误码的详细介绍请参见[ohos.zlib错误码](../errorcodes/errorcode-zlib.md)。 362 363| 错误码ID | 错误信息 | 364| -------- | --------------------------------------| 365| 900001 | The input source file is invalid. | 366| 900002 | The input destination file is invalid. | 367| 900003 | The input source file is not ZIP format or damaged. | 368 369**示例** 370 371```typescript 372// 【解压缩 例子1】 373// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过context获取 374import zlib from '@ohos.zlib'; 375import { BusinessError } from '@ohos.base'; 376let inFile = '/xx/xxx.zip'; 377let outFileDir = '/xxx'; 378 379try { 380 zlib.decompressFile(inFile, outFileDir, (errData: BusinessError) => { 381 if (errData !== null) { 382 console.log(`decompressFile failed. code is ${errData.code}, message is ${errData.message}`); 383 } 384 }) 385} catch(errData) { 386 let code = (errData as BusinessError).code; 387 let message = (errData as BusinessError).message; 388 console.log(`decompressFile failed. code is ${code}, message is ${message}`); 389} 390``` 391 392## Options 393 394**系统能力:** SystemCapability.BundleManager.Zlib 395 396| 名称 | 类型 | 可读 | 可写 | 说明 | 397| -------- | ---------------- | ---- | ---- | ---------------------------------------------------------- | 398| level | CompressLevel | 是 | 否 | 参考[zip.CompressLevel枚举定义](#zipcompresslevel)。 | 399| memLevel | MemLevel | 是 | 否 | 参考[zip.MemLevel枚举定义](#zipmemlevel)。 | 400| strategy | CompressStrategy | 是 | 否 | 参考[zip.CompressStrategy枚举定义](#zipcompressstrategy)。 | 401 402## zip.CompressLevel 403 404**系统能力:** SystemCapability.BundleManager.Zlib 405 406| 名称 | 值 | 说明 | 407| ---------------------------------- | ---- | ----------------- | 408| COMPRESS_LEVEL_NO_COMPRESSION | 0 | 压缩率为0压缩等级。 | 409| COMPRESS_LEVEL_BEST_SPEED | 1 | 最佳速度压缩等级。 | 410| COMPRESS_LEVEL_BEST_COMPRESSION | 9 | 最佳压缩等级。 | 411| COMPRESS_LEVEL_DEFAULT_COMPRESSION | -1 | 默认压缩等级。 | 412 413## zip.MemLevel 414 415**系统能力:** SystemCapability.BundleManager.Zlib 416 417| 名称 | 值 | 说明 | 418| ----------------- | ---- | -------------------------------- | 419| MEM_LEVEL_MIN | 1 | zip 接口在压缩过程中最小使用内存。 | 420| MEM_LEVEL_MAX | 9 | zip 接口在压缩过程中最大使用内存。 | 421| MEM_LEVEL_DEFAULT | 8 | zip 接口在压缩过程中默认使用内存。 | 422 423## zip.CompressStrategy 424 425**系统能力:** SystemCapability.BundleManager.Zlib 426 427| 名称 | 值 | 说明 | 428| ---------------------------------- | ---- | ------------------------ | 429| COMPRESS_STRATEGY_DEFAULT_STRATEGY | 0 | 常规数据策略。 | 430| COMPRESS_STRATEGY_FILTERED | 1 | 过滤器产生的数据压缩策略。 | 431| COMPRESS_STRATEGY_HUFFMAN_ONLY | 2 | 霍夫曼编码格式压缩策略。 | 432| COMPRESS_STRATEGY_RLE | 3 | 游标编码压缩策略。 | 433| COMPRESS_STRATEGY_FIXED | 4 | 固定的压缩策略。 | 434 435## zip.ErrorCode 436 437**系统能力:** SystemCapability.BundleManager.Zlib 438 439| 名称 | 值 | 说明 | 440| ---------------- | ---- | ------------ | 441| ERROR_CODE_OK | 0 | 函数调用成功。 | 442| ERROR_CODE_ERRNO | -1 | 函数调用失败。 | 443