• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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&lt;void&gt;
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';
44let inFile = '/xxx/filename.xxx';
45let outFile = '/xxx/xxx.zip';
46let options = {
47  level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
48  memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
49  strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
50};
51
52zlib.zipFile(inFile, outFile, options).then((data) => {
53    console.log('zipFile result is ' + JSON.Stringify(data));
54}).catch((err) => {
55    console.log('error is ' + JSON.Stringify(err));
56});
57```
58
59**Example 2**
60
61```typescript
62// Zip a folder.
63// 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.
64import zlib from '@ohos.zlib';
65let inFile = '/xxx/xxx';
66let outFile = '/xxx/xxx.zip';
67let options = {
68  level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
69  memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
70  strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
71};
72
73zlib.zipFile(inFile , outFile, options).then((data) => {
74    console.log('zipFile result is ' + JSON.Stringify(data));
75}).catch((err)=>{
76    console.log('error is ' + JSON.Stringify(err));
77});
78```
79
80## zlib.unzipFile<sup>(deprecated)</sup>
81
82unzipFile(inFile:string, outFile:string, options: Options): Promise&lt;void&gt;
83
84Unzips a file. This API uses a promise to return the result.
85
86> This API is deprecated since API version 9. You are advised to use [zlib.decompressFile](#zlibdecompressfile9) instead.
87
88**System capability**: SystemCapability.BundleManager.Zlib
89
90**Parameters**
91
92| Name | Type               | Mandatory| Description                                                        |
93| ------- | ------------------- | ---- | ------------------------------------------------------------ |
94| 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).|
95| outFile | string              | Yes  | Path of the unzipped file.                                        |
96| options | [Options](#options) | Yes  | Optional parameters for the unzip operation.                                            |
97
98**Return value**
99
100| Type          | Description                                                        |
101| -------------- | ------------------------------------------------------------ |
102| Promise\<void> | Returns [ERROR_CODE_OK](#ziperrorcode) if the operation is successful.<br>Returns [ERROR_CODE_ERRNO](#ziperrorcode) if the operation fails.|
103
104**Example**
105
106```typescript
107// Unzip a file.
108// 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.
109import zlib from '@ohos.zlib';
110let inFile = '/xx/xxx.zip';
111let outFile = '/xxx';
112
113let options = {
114  level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
115  memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
116  strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
117};
118zlib.unzipFile(inFile, outFile, options).then((data) => {
119    console.log('unzipFile result is ' + JSON.Stringify(data));
120}).catch((err)=>{
121    console.log('error is ' + JSON.Stringify(err));
122})
123```
124
125## zlib.compressFile<sup>9+</sup>
126
127compressFile(inFile: string, outFile: string, options: Options, callback: AsyncCallback\<void>): void;
128
129Compresses a file. This API uses an asynchronous callback to return the result.
130
131**System capability**: SystemCapability.BundleManager.Zlib
132
133**Parameters**
134
135| Name                 | Type               | Mandatory| Description                                                        |
136| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ |
137| 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).|
138| outFile                 | string              | Yes  | Path of the compressed file.                                          |
139| options                 | [Options](#options) | Yes  | Compression parameters.                                              |
140| 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.                                            |
141
142**Error codes**
143
144For details about the error codes, see [zlib Error Codes](../errorcodes/errorcode-zlib.md).
145| ID| Error Message                              |
146| -------- | --------------------------------------|
147| 900001   | The input source file is invalid.      |
148| 900002   | The input destination file is invalid. |
149
150**Example**
151
152```typescript
153// Compress a file.
154// 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.
155import zlib from '@ohos.zlib';
156let inFile = '/xxx/filename.xxx';
157let outFile = '/xxx/xxx.zip';
158let options = {
159  level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
160  memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
161  strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
162};
163
164try {
165    zlib.compressFile(inFile, outFile, options, (errData) => {
166        if (errData !== null) {
167            console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
168        }
169    })
170} catch(errData) {
171    console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
172}
173```
174
175compressFile(inFile: string, outFile: string, options: Options): Promise\<void>;
176
177Compresses a file. This API uses a promise to return the result.
178
179**System capability**: SystemCapability.BundleManager.Zlib
180
181**Parameters**
182
183| Name | Type               | Mandatory| Description                                                        |
184| ------- | ------------------- | ---- | ------------------------------------------------------------ |
185| 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).|
186| outFile | string              | Yes  | Path of the compressed file.                                          |
187| options | [Options](#options) | Yes  | Compression parameters.                                              |
188
189**Error codes**
190
191For details about the error codes, see [zlib Error Codes](../errorcodes/errorcode-zlib.md).
192
193| ID| Error Message                              |
194| -------- | ------------------------------------- |
195| 900001   | The input source file is invalid.      |
196| 900002   | The input destination file is invalid. |
197
198```typescript
199// Compress a file.
200// 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.
201import zlib from '@ohos.zlib';
202let inFile = '/xxx/filename.xxx';
203let outFile = '/xxx/xxx.zip';
204let 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) => {
212        console.info('compressFile success');
213    }).catch((errData) => {
214        console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
215    })
216} catch(errData) {
217    console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
218}
219```
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 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.                                      |
237| options                 | [Options](#options) | Yes  | Decompression parameters.                                              |
238| 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.                                            |
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
249**Example**
250
251```typescript
252// Decompress a file.
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';
255let inFile = '/xx/xxx.zip';
256let outFile = '/xxx';
257let options = {
258  level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
259  memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
260  strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
261};
262
263try {
264    zlib.decompressFile(inFile, outFile, options, (errData) => {
265        if (errData !== null) {
266            console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
267        }
268    })
269} catch(errData) {
270    console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
271}
272```
273
274decompressFile(inFile: string, outFile: string, options: Options): Promise\<void>;
275
276Decompresses a file. This API uses a promise to return the result.
277
278**System capability**: SystemCapability.BundleManager.Zlib
279
280**Parameters**
281
282| Name | Type               | Mandatory| Description                                                        |
283| ------- | ------------------- | ---- | ------------------------------------------------------------ |
284| inFile  | string              | Yes  | Path of the file to decompress. 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).|
285| outFile | string              | Yes  | Path of the decompressed file.                                      |
286| options | [Options](#options) | Yes  | Decompression parameters.                                            |
287
288**Error codes**
289
290For details about the error codes, see [zlib Error Codes](../errorcodes/errorcode-zlib.md).
291
292| ID| Error Message                              |
293| ------ | ------------------------------------- |
294| 900001 | The input source file is invalid.      |
295| 900002 | The input destination file is invalid. |
296
297```typescript
298// Decompress a file.
299// 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.
300import zlib from '@ohos.zlib';
301let inFile = '/xx/xxx.zip';
302let outFile = '/xxx';
303let options = {
304  level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
305  memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
306  strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
307};
308
309try {
310    zlib.decompressFile(inFile, outFile, options).then((data) => {
311        console.info('decompressFile success');
312    }).catch((errData) => {
313        console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
314    })
315} catch(errData) {
316    console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
317}
318```
319
320## Options
321
322**System capability**: SystemCapability.BundleManager.Zlib
323
324| Name    | Type            | Readable| Writable| Description                                                      |
325| -------- | ---------------- | ---- | ---- | ---------------------------------------------------------- |
326| level    | CompressLevel     | Yes  | No  | See [zip.CompressLevel](#zipcompresslevel).      |
327| memLevel | MemLevel         | Yes  | No  | See [zip.MemLevel](#zipmemlevel).                |
328| strategy | CompressStrategy | Yes  | No  | See [zip.CompressStrategy](#zipcompressstrategy).|
329
330## zip.CompressLevel
331
332**System capability**: SystemCapability.BundleManager.Zlib
333
334| Name                              | Value  | Description             |
335| ---------------------------------- | ---- | ----------------- |
336| COMPRESS_LEVEL_NO_COMPRESSION      | 0    | Compress level 0 that indicates uncompressed.|
337| COMPRESS_LEVEL_BEST_SPEED          | 1    | Compression level 1 that gives the best speed. |
338| COMPRESS_LEVEL_BEST_COMPRESSION    | 9    | Compression level 9 that gives the best compression.     |
339| COMPRESS_LEVEL_DEFAULT_COMPRESSION | -1   | Default compression level.     |
340
341## zip.MemLevel
342
343**System capability**: SystemCapability.BundleManager.Zlib
344
345| Name             | Value  | Description                            |
346| ----------------- | ---- | -------------------------------- |
347| MEM_LEVEL_MIN     | 1    | Minimum memory used by the **zip** API during compression.|
348| MEM_LEVEL_MAX     | 9    | Maximum memory used by the **zip** API during compression.|
349| MEM_LEVEL_DEFAULT | 8    | Default memory used by the **zip** API during compression.|
350
351## zip.CompressStrategy
352
353**System capability**: SystemCapability.BundleManager.Zlib
354
355| Name                              | Value  | Description                    |
356| ---------------------------------- | ---- | ------------------------ |
357| COMPRESS_STRATEGY_DEFAULT_STRATEGY | 0    | Default compression strategy.            |
358| COMPRESS_STRATEGY_FILTERED         | 1    | Filtered compression strategy.|
359| COMPRESS_STRATEGY_HUFFMAN_ONLY     | 2    | Huffman coding compression strategy.  |
360| COMPRESS_STRATEGY_RLE              | 3    | RLE compression strategy.        |
361| COMPRESS_STRATEGY_FIXED            | 4    | Fixed compression strategy.          |
362
363## zip.ErrorCode
364
365**System capability**: SystemCapability.BundleManager.Zlib
366
367| Name            | Value  | Description        |
368| ---------------- | ---- | ------------ |
369| ERROR_CODE_OK    | 0    | The API is successfully called.|
370| ERROR_CODE_ERRNO | -1   | Failed to call the API.|
371