1# @ohos.zlib (Zip) 2 3The **Zip** module provides APIs for file compression and decompression. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9## Modules to Import 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 18Zips a file. This API uses a promise to return the result. 19 20> This API is deprecated since API version 9. You are advised to use [zlib.compressFile](#zlibcompressfile9) instead. 21 22**System capability**: SystemCapability.BundleManager.Zlib 23 24**Parameters** 25 26| Name | Type | Mandatory| Description | 27| ------- | ------------------- | ---- | ------------------------------------------------------------ | 28| inFile | string | Yes | Path of the folder or file to zip. The path must be an application sandbox path, which can be obtained from the context. For details about the context, see [FA Model](js-apis-inner-app-context.md) and [Stage Model](js-apis-inner-application-context.md).| 29| outFile | string | Yes | Path of the zipped file. The file name extension is .zip. | 30| options | [Options](#options) | Yes | Optional parameters for the zip operation. | 31 32**Return value** 33 34| Type | Description | 35| -------------- | ------------------------------------------------------------ | 36| Promise\<void> | Returns [ERROR_CODE_OK](#ziperrorcode) if the operation is successful.<br>Returns [ERROR_CODE_ERRNO](#ziperrorcode) if the operation fails.| 37 38**Example 1** 39 40```typescript 41// Zip a file. 42// The path used in the code must be an application sandbox path, for example, /data/storage/el2/base/haps. You can obtain the path through the 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**Example 2** 62 63```typescript 64// Zip a folder. 65// The path used in the code must be an application sandbox path, for example, /data/storage/el2/base/haps. You can obtain the path through the 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 88Unzips a file. This API uses a promise to return the result. 89 90> This API is deprecated since API version 9. You are advised to use [zlib.decompressFile](#zlibdecompressfile9) instead. 91 92**System capability**: SystemCapability.BundleManager.Zlib 93 94**Parameters** 95 96| Name | Type | Mandatory| Description | 97| ------- | ------------------- | ---- | ------------------------------------------------------------ | 98| inFile | string | Yes | Path of the file to unzip. The path must be an application sandbox path, which can be obtained from the context. For details about the context, see [FA Model](js-apis-inner-app-context.md) and [Stage Model](js-apis-inner-application-context.md).| 99| outFile | string | Yes | Path of the unzipped file. | 100| options | [Options](#options) | Yes | Optional parameters for the unzip operation. | 101 102**Return value** 103 104| Type | Description | 105| -------------- | ------------------------------------------------------------ | 106| Promise\<void> | Returns [ERROR_CODE_OK](#ziperrorcode) if the operation is successful.<br>Returns [ERROR_CODE_ERRNO](#ziperrorcode) if the operation fails.| 107 108**Example** 109 110```typescript 111// Unzip a file. 112// The path used in the code must be an application sandbox path, for example, /data/storage/el2/base/haps. You can obtain the path through the 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 135Compresses a file. This API uses an asynchronous callback to return the result. 136 137**System capability**: SystemCapability.BundleManager.Zlib 138 139**Parameters** 140 141| Name | Type | Mandatory| Description | 142| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ | 143| inFile | string | Yes | Path of the folder or file to compress. The path must be an application sandbox path, which can be obtained from the context. For details about the context, see [FA Model](js-apis-inner-app-context.md) and [Stage Model](js-apis-inner-application-context.md).| 144| outFile | string | Yes | Path of the compressed file. | 145| options | [Options](#options) | Yes | Compression parameters. | 146| AsyncCallback<**void**> | callback | No | Callback used to return the result. If the operation is successful, **null** is returned; otherwise, a specific error code is returned. | 147 148**Error codes** 149 150For details about the error codes, see [zlib Error Codes](../errorcodes/errorcode-zlib.md). 151 152| ID| Error Message | 153| -------- | --------------------------------------| 154| 900001 | The input source file is invalid. | 155| 900002 | The input destination file is invalid. | 156 157**Example** 158 159```typescript 160// Compress a file. 161// The path used in the code must be an application sandbox path, for example, /data/storage/el2/base/haps. You can obtain the path through the 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 190Compresses a file. This API uses a promise to return the result. 191 192**System capability**: SystemCapability.BundleManager.Zlib 193 194**Parameters** 195 196| Name | Type | Mandatory| Description | 197| ------- | ------------------- | ---- | ------------------------------------------------------------ | 198| inFile | string | Yes | Path of the folder or file to compress. The path must be an application sandbox path, which can be obtained from the context. For details about the context, see [FA Model](js-apis-inner-app-context.md) and [Stage Model](js-apis-inner-application-context.md).| 199| outFile | string | Yes | Path of the compressed file. | 200| options | [Options](#options) | Yes | Compression parameters. | 201 202**Error codes** 203 204For details about the error codes, see [zlib Error Codes](../errorcodes/errorcode-zlib.md). 205 206| ID| Error Message | 207| -------- | ------------------------------------- | 208| 900001 | The input source file is invalid. | 209| 900002 | The input destination file is invalid. | 210 211```typescript 212// Compress a file. 213// The path used in the code must be an application sandbox path, for example, /data/storage/el2/base/haps. You can obtain the path through the 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 242Decompresses a file. This API uses an asynchronous callback to return the result. 243 244**System capability**: SystemCapability.BundleManager.Zlib 245 246**Parameters** 247 248| Name | Type | Mandatory| Description | 249| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ | 250| inFile | string | Yes | Path of the file to decompress. The file name extension must be .zip. The path must be an application sandbox path, which can be obtained from the context. For details about the context, see [FA Model](js-apis-inner-app-context.md) and [Stage Model](js-apis-inner-application-context.md).| 251| outFile | string | Yes | Path of the decompressed file. The path must exist in the system. Otherwise, the decompression fails. The path must be an application sandbox path, which can be obtained from the context. For details about the context, see [FA Model](js-apis-inner-app-context.md) and [Stage Model](js-apis-inner-application-context.md). If a file or folder with the same name already exists in the path, they will be overwritten.| 252| options | [Options](#options) | Yes | Decompression parameters. | 253| AsyncCallback<**void**> | callback | Yes | Callback used to return the result. If the operation is successful, **null** is returned; otherwise, a specific error code is returned. | 254 255**Error codes** 256 257For details about the error codes, see [zlib Error Codes](../errorcodes/errorcode-zlib.md). 258 259| ID| Error Message | 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**Example** 266 267```typescript 268// Decompress a file. 269// The path used in the code must be an application sandbox path, for example, /data/storage/el2/base/haps. You can obtain the path through the 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 296Decompresses a file. This API uses a promise to return the result. 297 298**System capability**: SystemCapability.BundleManager.Zlib 299 300**Parameters** 301 302| Name | Type | Mandatory| Description | 303| ------- | ------------------- | ---- | ------------------------------------------------------------ | 304| inFile | string | Yes | Path of the file to decompress. The file name extension must be .zip. The path must be an application sandbox path, which can be obtained from the context. For details about the context, see [FA Model](js-apis-inner-app-context.md) and [Stage Model](js-apis-inner-application-context.md).| 305| outFile | string | Yes | Path of the decompressed file. The path must exist in the system. Otherwise, the decompression fails. The path must be an application sandbox path, which can be obtained from the context. For details about the context, see [FA Model](js-apis-inner-app-context.md) and [Stage Model](js-apis-inner-application-context.md). If a file or folder with the same name already exists in the path, they will be overwritten.| 306| options | [Options](#options) | No | Decompression parameters. | 307 308**Error codes** 309 310For details about the error codes, see [zlib Error Codes](../errorcodes/errorcode-zlib.md). 311 312| ID| Error Message | 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// Decompress a file. 320// The path used in the code must be an application sandbox path, for example, /data/storage/el2/base/haps. You can obtain the path through the 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 347Decompresses a file. This API uses an asynchronous callback to return the result. 348 349**System capability**: SystemCapability.BundleManager.Zlib 350 351**Parameters** 352 353| Name | Type | Mandatory| Description | 354| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ | 355| inFile | string | Yes | Path of the file to decompress. The file name extension must be .zip. The path must be an application sandbox path, which can be obtained from the context. For details about the context, see [FA Model](js-apis-inner-app-context.md) and [Stage Model](js-apis-inner-application-context.md).| 356| outFile | string | Yes | Path of the decompressed file. The path must exist in the system. Otherwise, the decompression fails. The path must be an application sandbox path, which can be obtained from the context. For details about the context, see [FA Model](js-apis-inner-app-context.md) and [Stage Model](js-apis-inner-application-context.md). If a file or folder with the same name already exists in the path, they will be overwritten.| 357| AsyncCallback<**void**> | callback | Yes | Callback used to return the result. If the operation is successful, **null** is returned; otherwise, a specific error code is returned. | 358 359**Error codes** 360 361For details about the error codes, see [zlib Error Codes](../errorcodes/errorcode-zlib.md). 362 363| ID| Error Message | 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**Example** 370 371```typescript 372// Unzip a file. 373// The path used in the code must be an application sandbox path, for example, /data/storage/el2/base/haps. You can obtain the path through the 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**System capability**: SystemCapability.BundleManager.Zlib 395 396| Name | Type | Readable| Writable| Description | 397| -------- | ---------------- | ---- | ---- | ---------------------------------------------------------- | 398| level | CompressLevel | Yes | No | See [zip.CompressLevel](#zipcompresslevel). | 399| memLevel | MemLevel | Yes | No | See [zip.MemLevel](#zipmemlevel). | 400| strategy | CompressStrategy | Yes | No | See [zip.CompressStrategy](#zipcompressstrategy).| 401 402## zip.CompressLevel 403 404**System capability**: SystemCapability.BundleManager.Zlib 405 406| Name | Value | Description | 407| ---------------------------------- | ---- | ----------------- | 408| COMPRESS_LEVEL_NO_COMPRESSION | 0 | Compress level 0 that indicates uncompressed.| 409| COMPRESS_LEVEL_BEST_SPEED | 1 | Compression level 1 that gives the best speed. | 410| COMPRESS_LEVEL_BEST_COMPRESSION | 9 | Compression level 9 that gives the best compression. | 411| COMPRESS_LEVEL_DEFAULT_COMPRESSION | -1 | Default compression level. | 412 413## zip.MemLevel 414 415**System capability**: SystemCapability.BundleManager.Zlib 416 417| Name | Value | Description | 418| ----------------- | ---- | -------------------------------- | 419| MEM_LEVEL_MIN | 1 | Minimum memory used by the **zip** API during compression.| 420| MEM_LEVEL_MAX | 9 | Maximum memory used by the **zip** API during compression.| 421| MEM_LEVEL_DEFAULT | 8 | Default memory used by the **zip** API during compression.| 422 423## zip.CompressStrategy 424 425**System capability**: SystemCapability.BundleManager.Zlib 426 427| Name | Value | Description | 428| ---------------------------------- | ---- | ------------------------ | 429| COMPRESS_STRATEGY_DEFAULT_STRATEGY | 0 | Default compression strategy. | 430| COMPRESS_STRATEGY_FILTERED | 1 | Filtered compression strategy.| 431| COMPRESS_STRATEGY_HUFFMAN_ONLY | 2 | Huffman coding compression strategy. | 432| COMPRESS_STRATEGY_RLE | 3 | RLE compression strategy. | 433| COMPRESS_STRATEGY_FIXED | 4 | Fixed compression strategy. | 434 435## zip.ErrorCode 436 437**System capability**: SystemCapability.BundleManager.Zlib 438 439| Name | Value | Description | 440| ---------------- | ---- | ------------ | 441| ERROR_CODE_OK | 0 | The API is successfully called.| 442| ERROR_CODE_ERRNO | -1 | Failed to call the API.| 443