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> **NOTE** 21> 22> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [zlib.compressFile](#zlibcompressfile9) instead. 23 24**System capability**: SystemCapability.BundleManager.Zlib 25 26**Parameters** 27 28| Name | Type | Mandatory| Description | 29| ------- | ------------------- | ---- | ------------------------------------------------------------ | 30| 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).| 31| outFile | string | Yes | Path of the zipped file. The file name extension is .zip. | 32| options | [Options](#options) | Yes | Optional parameters for the zip operation. | 33 34**Return value** 35 36| Type | Description | 37| -------------- | ------------------------------------------------------------ | 38| Promise\<void> | Promise that returns no value.| 39 40**Example** 41 42```typescript 43// 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. 44import zlib from '@ohos.zlib'; 45import { BusinessError } from '@ohos.base'; 46 47let inFile = '/xxx/filename.xxx'; 48let outFile = '/xxx/xxx.zip'; 49let options: zlib.Options = { 50 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 51 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 52 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 53}; 54 55zlib.zipFile(inFile, outFile, options).then((data: void) => { 56 console.info('zipFile result is ' + JSON.stringify(data)); 57}).catch((err: BusinessError) => { 58 console.error('error is ' + JSON.stringify(err)); 59}); 60``` 61 62## zlib.unzipFile<sup>(deprecated)</sup> 63 64unzipFile(inFile:string, outFile:string, options: Options): Promise<void> 65 66Unzips a file. This API uses a promise to return the result. 67 68> **NOTE** 69> 70> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [zlib.decompressFile](#zlibdecompressfile9) instead. 71 72**System capability**: SystemCapability.BundleManager.Zlib 73 74**Parameters** 75 76| Name | Type | Mandatory| Description | 77| ------- | ------------------- | ---- | ------------------------------------------------------------ | 78| 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).| 79| outFile | string | Yes | Path of the unzipped file. | 80| options | [Options](#options) | Yes | Optional parameters for the unzip operation. | 81 82**Return value** 83 84| Type | Description | 85| -------------- | ------------------------------------------------------------ | 86| Promise\<void> | Promise that returns no value.| 87 88**Example** 89 90```typescript 91// 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. 92import zlib from '@ohos.zlib'; 93import { BusinessError } from '@ohos.base'; 94 95let inFile = '/xx/xxx.zip'; 96let outFile = '/xxx'; 97 98let options: zlib.Options = { 99 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 100 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 101 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 102}; 103zlib.unzipFile(inFile, outFile, options).then((data: void) => { 104 console.info('unzipFile result is ' + JSON.stringify(data)); 105}).catch((err: BusinessError)=>{ 106 console.error('error is ' + JSON.stringify(err)); 107}) 108``` 109 110## zlib.compressFile<sup>9+</sup> 111 112compressFile(inFile: string, outFile: string, options: Options, callback: AsyncCallback\<void>): void 113 114Compresses a file. This API uses an asynchronous callback to return the result. 115 116**System capability**: SystemCapability.BundleManager.Zlib 117 118**Parameters** 119 120| Name | Type | Mandatory| Description | 121| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ | 122| 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).| 123| outFile | string | Yes | Path of the compressed file. | 124| options | [Options](#options) | Yes | Compression parameters. | 125| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **null** is returned; otherwise, a specific error code is returned. | 126 127**Error codes** 128 129For details about the error codes, see [zlib Error Codes](../errorcodes/errorcode-zlib.md). 130 131| ID| Error Message | 132| -------- | --------------------------------------| 133| 900001 | The input source file is invalid. | 134| 900002 | The input destination file is invalid. | 135 136**Example** 137 138```typescript 139// 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. 140import zlib from '@ohos.zlib'; 141import { BusinessError } from '@ohos.base'; 142 143let inFile = '/xxx/filename.xxx'; 144let outFile = '/xxx/xxx.zip'; 145let options: zlib.Options = { 146 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 147 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 148 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 149}; 150 151try { 152 zlib.compressFile(inFile, outFile, options, (errData: BusinessError) => { 153 if (errData !== null) { 154 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 155 } 156 }) 157} catch(errData) { 158 let code = (errData as BusinessError).code; 159 let message = (errData as BusinessError).message; 160 console.error(`errData is errCode:${code} message:${message}`); 161} 162``` 163 164## zlib.compressFile<sup>9+</sup> 165 166compressFile(inFile: string, outFile: string, options: Options): Promise\<void> 167 168Compresses a file. This API uses a promise to return the result. 169 170**System capability**: SystemCapability.BundleManager.Zlib 171 172**Parameters** 173 174| Name | Type | Mandatory| Description | 175| ------- | ------------------- | ---- | ------------------------------------------------------------ | 176| 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).| 177| outFile | string | Yes | Path of the compressed file. | 178| options | [Options](#options) | Yes | Compression parameters. | 179 180**Return value** 181 182| Type | Description | 183| -------------- | ----------------------- | 184| Promise\<void> | Promise that returns no value.| 185 186**Error codes** 187 188For details about the error codes, see [zlib Error Codes](../errorcodes/errorcode-zlib.md). 189 190| ID| Error Message | 191| -------- | ------------------------------------- | 192| 900001 | The input source file is invalid. | 193| 900002 | The input destination file is invalid. | 194 195**Example** 196 197```typescript 198// 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. 199import zlib from '@ohos.zlib'; 200import { BusinessError } from '@ohos.base'; 201 202let inFile = '/xxx/filename.xxx'; 203let outFile = '/xxx/xxx.zip'; 204let options: zlib.Options = { 205 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 206 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 207 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 208}; 209 210try { 211 zlib.compressFile(inFile, outFile, options).then((data: void) => { 212 console.info('compressFile success. data: ' + JSON.stringify(data)); 213 }).catch((errData: BusinessError) => { 214 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 215 }) 216} catch(errData) { 217 let code = (errData as BusinessError).code; 218 let message = (errData as BusinessError).message; 219 console.error(`errData is errCode:${code} message:${message}`); 220} 221``` 222 223## zlib.decompressFile<sup>9+</sup> 224 225decompressFile(inFile: string, outFile: string, options: Options, callback: AsyncCallback\<void>): void 226 227Decompresses a file. This API uses an asynchronous callback to return the result. 228 229**System capability**: SystemCapability.BundleManager.Zlib 230 231**Parameters** 232 233| Name | Type | Mandatory| Description | 234| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ | 235| 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).| 236| 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.| 237| options | [Options](#options) | Yes | Decompression parameters. | 238| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **null** is returned; otherwise, a specific error code is returned. | 239 240**Error codes** 241 242For details about the error codes, see [zlib Error Codes](../errorcodes/errorcode-zlib.md). 243 244| ID| Error Message | 245| -------- | --------------------------------------| 246| 900001 | The input source file is invalid. | 247| 900002 | The input destination file is invalid. | 248| 900003 | The input source file is not ZIP format or damaged. | 249 250**Example** 251 252```typescript 253// 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. 254import zlib from '@ohos.zlib'; 255import { BusinessError } from '@ohos.base'; 256 257let inFile = '/xx/xxx.zip'; 258let outFileDir = '/xxx'; 259let options: zlib.Options = { 260 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION 261}; 262 263try { 264 zlib.decompressFile(inFile, outFileDir, options, (errData: BusinessError) => { 265 if (errData !== null) { 266 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 267 } 268 }) 269} catch(errData) { 270 let code = (errData as BusinessError).code; 271 let message = (errData as BusinessError).message; 272 console.error(`errData is errCode:${code} message:${message}`); 273} 274``` 275 276## zlib.decompressFile<sup>9+</sup> 277 278decompressFile(inFile: string, outFile: string, options?: Options): Promise\<void> 279 280Decompresses a file. This API uses a promise to return the result. 281 282**System capability**: SystemCapability.BundleManager.Zlib 283 284**Parameters** 285 286| Name | Type | Mandatory| Description | 287| ------- | ------------------- | ---- | ------------------------------------------------------------ | 288| 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).| 289| 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.| 290| options | [Options](#options) | No | Decompression parameters. | 291 292**Return value** 293 294| Type | Description | 295| -------------- | ----------------------- | 296| Promise\<void> | Promise that returns no value.| 297 298**Error codes** 299 300For details about the error codes, see [zlib Error Codes](../errorcodes/errorcode-zlib.md). 301 302| ID| Error Message | 303| ------ | ------------------------------------- | 304| 900001 | The input source file is invalid. | 305| 900002 | The input destination file is invalid. | 306| 900003 | The input source file is not ZIP format or damaged. | 307 308**Example** 309 310```typescript 311// 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. 312import zlib from '@ohos.zlib'; 313import { BusinessError } from '@ohos.base'; 314 315let inFile = '/xx/xxx.zip'; 316let outFileDir = '/xxx'; 317let options: zlib.Options = { 318 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION 319}; 320 321try { 322 zlib.decompressFile(inFile, outFileDir, options).then((data: void) => { 323 console.info('decompressFile success. data: ' + JSON.stringify(data)); 324 }).catch((errData: BusinessError) => { 325 console.error(`errData is errCode:${errData.code} message:${errData.message}`); 326 }) 327} catch(errData) { 328 let code = (errData as BusinessError).code; 329 let message = (errData as BusinessError).message; 330 console.error(`errData is errCode:${code} message:${message}`); 331} 332``` 333 334## zlib.decompressFile<sup>10+</sup> 335 336decompressFile(inFile: string, outFile: string, callback: AsyncCallback\<void\>): void 337 338Decompresses a file. This API uses an asynchronous callback to return the result. 339 340**System capability**: SystemCapability.BundleManager.Zlib 341 342**Parameters** 343 344| Name | Type | Mandatory| Description | 345| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ | 346| 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).| 347| 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.| 348| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **null** is returned; otherwise, a specific error code is returned. | 349 350**Error codes** 351 352For details about the error codes, see [zlib Error Codes](../errorcodes/errorcode-zlib.md). 353 354| ID| Error Message | 355| -------- | --------------------------------------| 356| 900001 | The input source file is invalid. | 357| 900002 | The input destination file is invalid. | 358| 900003 | The input source file is not ZIP format or damaged. | 359 360**Example** 361 362```typescript 363// 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. 364import zlib from '@ohos.zlib'; 365import { BusinessError } from '@ohos.base'; 366let inFile = '/xx/xxx.zip'; 367let outFileDir = '/xxx'; 368 369try { 370 zlib.decompressFile(inFile, outFileDir, (errData: BusinessError) => { 371 if (errData !== null) { 372 console.error(`decompressFile failed. code is ${errData.code}, message is ${errData.message}`); 373 } 374 }) 375} catch(errData) { 376 let code = (errData as BusinessError).code; 377 let message = (errData as BusinessError).message; 378 console.error(`decompressFile failed. code is ${code}, message is ${message}`); 379} 380``` 381 382## Options 383 384**System capability**: SystemCapability.BundleManager.Zlib 385 386| Name | Type | Readable| Writable| Description | 387| -------- | ---------------- | ---- | ---- | ---------------------------------------------------------- | 388| level | CompressLevel | Yes | No | See [zip.CompressLevel](#zipcompresslevel). | 389| memLevel | MemLevel | Yes | No | See [zip.MemLevel](#zipmemlevel). | 390| strategy | CompressStrategy | Yes | No | See [zip.CompressStrategy](#zipcompressstrategy).| 391 392## zip.CompressLevel 393 394**System capability**: SystemCapability.BundleManager.Zlib 395 396| Name | Value | Description | 397| ---------------------------------- | ---- | ----------------- | 398| COMPRESS_LEVEL_NO_COMPRESSION | 0 | Compress level 0 that indicates uncompressed.| 399| COMPRESS_LEVEL_BEST_SPEED | 1 | Compression level 1 that gives the best speed. | 400| COMPRESS_LEVEL_BEST_COMPRESSION | 9 | Compression level 9 that gives the best compression. | 401| COMPRESS_LEVEL_DEFAULT_COMPRESSION | -1 | Default compression level. | 402 403## zip.MemLevel 404 405**System capability**: SystemCapability.BundleManager.Zlib 406 407| Name | Value | Description | 408| ----------------- | ---- | -------------------------------- | 409| MEM_LEVEL_MIN | 1 | Minimum memory used by the **zip** API during compression.| 410| MEM_LEVEL_MAX | 9 | Maximum memory used by the **zip** API during compression.| 411| MEM_LEVEL_DEFAULT | 8 | Default memory used by the **zip** API during compression.| 412 413## zip.CompressStrategy 414 415**System capability**: SystemCapability.BundleManager.Zlib 416 417| Name | Value | Description | 418| ---------------------------------- | ---- | ------------------------ | 419| COMPRESS_STRATEGY_DEFAULT_STRATEGY | 0 | Default compression strategy. | 420| COMPRESS_STRATEGY_FILTERED | 1 | Filtered compression strategy.| 421| COMPRESS_STRATEGY_HUFFMAN_ONLY | 2 | Huffman coding compression strategy. | 422| COMPRESS_STRATEGY_RLE | 3 | RLE compression strategy. | 423| COMPRESS_STRATEGY_FIXED | 4 | Fixed compression strategy. | 424 425## zip.ErrorCode 426 427**System capability**: SystemCapability.BundleManager.Zlib 428 429| Name | Value | Description | 430| ---------------- | ---- | ------------ | 431| ERROR_CODE_OK | 0 | The API is successfully called.| 432| ERROR_CODE_ERRNO | -1 | Failed to call the API.| 433