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