1# Zip 2 3> **NOTE** 4> 5> 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. 6 7## Constraints 8 9None 10## Modules to Import 11 12```javascript 13import zlib from '@ohos.zlib'; 14``` 15 16## zlib.zipFile 17zipFile(inFile:string, outFile:string, options: Options): Promise<void> 18 19Zips a file. This API uses a promise to return the result. 20 21**System capability**: SystemCapability.BundleManager.Zlib 22 23**Parameters** 24 25| Name | Type | Mandatory| Description | 26| ------- | ------------------- | ---- | ------------------------------------------------------------ | 27| inFile | string | Yes | Path of the folder or file to zip. For details about the path, see [FA Model](js-apis-Context.md) and [Stage Model](js-apis-application-context.md).| 28| outFile | string | Yes | Path of the zipped file. The file name extension is .zip. | 29| options | [Options](#options) | No | Optional parameters for the zip operation. | 30 31**Return value** 32 33| Type | Description | 34| -------------- | ---------------------------------------------------- | 35| Promise\<void> | Returns **ERROR_CODE_OK** if the operation is successful; returns **ERROR_CODE_ERRNO** otherwise.| 36 37**Example 1** 38 39```javascript 40 41// Zip a file. 42import zlib from '@ohos.zlib' 43var inFile = "/xxx/filename.xxx"; 44var outFile = "/xxx/xxx.zip"; 45var options = { 46 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 47 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 48 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 49}; 50 51zlib.zipFile(inFile, outFile, options).then((data) => { 52 console.log("zipFile result: " + data); 53}).catch((err)=>{ 54 console.log("catch((err)=>" + err); 55}); 56 57``` 58 59**Example 2** 60 61``` 62// Zip a folder. 63import zlib from '@ohos.zlib' 64var inFile = "/xxx/xxx"; 65var outFile = "/xxx/xxx.zip"; 66var options = { 67 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 68 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 69 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 70}; 71 72zlib.zipFile(inFile , outFile, options).then((data) => { 73 console.log("zipFile result: " + data); 74}).catch((err)=>{ 75 console.log("catch((err)=>" + err); 76}); 77``` 78 79## zlib.unzipFile 80 81unzipFile(inFile:string, outFile:string, options: Options): Promise<void> 82 83Unzips a file. This API uses a promise to return the result. 84 85**System capability**: SystemCapability.BundleManager.Zlib 86 87**Parameters** 88 89| Name | Type | Mandatory| Description | 90| ------- | ------------------- | ---- | ------------------------------------------------------------ | 91| inFile | string | Yes | Path of the folder or file to zip. For details about the path, see [FA Model](js-apis-Context.md) and [Stage Model](js-apis-application-context.md).| 92| outFile | string | Yes | Path of the unzipped file. | 93| options | [Options](#options) | No | Optional parameters for the unzip operation. | 94 95**Return value** 96 97| Type | Description | 98| -------------- | ------------------------------------------------------------ | 99| Promise\<void> | Returns **ERROR_CODE_OK** if the operation is successful; returns **ERROR_CODE_ERRNO** otherwise.| 100 101**Example** 102 103```javascript 104// Unzip a file. 105import zlib from '@ohos.zlib' 106var inFile = "/xx/xxx.zip"; 107var outFile = "/xxx"; 108 109let options = { 110 level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, 111 memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, 112 strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY 113}; 114zlib.unzipFile(inFile, outFile, options).then((data) => { 115 console.log("unzipFile result: " + data); 116}).catch((err)=>{ 117 console.log("catch((err)=>" + err); 118}) 119 120``` 121 122## Options 123 124**System capability**: SystemCapability.BundleManager.Zlib 125 126| Name | Type | Mandatory| Description | 127| -------- | ---------------- | ---- | --------------------------------------------------------- | 128| level | CompressLeve | No | See [zip.CompressLevel](#zipcompresslevel). | 129| memLevel | MemLevel | No | See [zip.MemLevel](#zipmemlevel). | 130| strategy | CompressStrategy | No | See [zip.CompressStrategy](#zipcompressstrategy).| 131 132## zip.MemLevel 133 134**System capability**: SystemCapability.BundleManager.Zlib 135 136| Name | Value | Description | 137| ----------------- | ---- | -------------------------------- | 138| MEM_LEVEL_MIN | 1 | Minimum memory used by the **zip** API during compression.| 139| MEM_LEVEL_MAX | 9 | Maximum memory used by the **zip** API during compression.| 140| MEM_LEVEL_DEFAULT | 8 | Default memory used by the **zip** API during compression.| 141 142## zip.CompressLevel 143 144**System capability**: SystemCapability.BundleManager.Zlib 145 146| Name | Value | Description | 147| ---------------------------------- | ---- | ----------------- | 148| COMPRESS_LEVEL_NO_COMPRESSION | 0 | Compress level 0 that indicates uncompressed.| 149| COMPRESS_LEVEL_BEST_SPEED | 1 | Compression level 1 that gives the best speed. | 150| COMPRESS_LEVEL_BEST_COMPRESSION | 9 | Compression level 9 that gives the best compression. | 151| COMPRESS_LEVEL_DEFAULT_COMPRESSION | -1 | Default compression level. | 152 153## zip.CompressStrategy 154 155**System capability**: SystemCapability.BundleManager.Zlib 156 157| Name | Value | Description | 158| ---------------------------------- | ---- | ------------------------ | 159| COMPRESS_STRATEGY_DEFAULT_STRATEGY | 0 | Default compression strategy. | 160| COMPRESS_STRATEGY_FILTERED | 1 | Filtered compression strategy.| 161| COMPRESS_STRATEGY_HUFFMAN_ONLY | 2 | Huffman coding compression strategy. | 162| COMPRESS_STRATEGY_RLE | 3 | RLE compression strategy. | 163| COMPRESS_STRATEGY_FIXED | 4 | Fixed compression strategy. | 164 165## zip.ErrorCode 166 167**System capability**: SystemCapability.BundleManager.Zlib 168 169| Name | Value | Description | 170| ---------------- | ---- | ------------ | 171| ERROR_CODE_OK | 0 | The API is successfully called.| 172| ERROR_CODE_ERRNO | -1 | Failed to call the API.| 173