• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.zlib (Zip模块)
2
3本模块提供压缩解压缩文件的能力
4
5> **说明:**
6>
7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
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
18压缩接口(Promise形式)。
19
20> 从api9开始不再维护,建议使用[zlib.compressFile](#zlibcompressfile9)
21
22**系统能力:** SystemCapability.BundleManager.Zlib
23
24**参数:**
25
26| 参数名  | 类型                | 必填 | 说明                                                         |
27| ------- | ------------------- | ---- | ------------------------------------------------------------ |
28| inFile  | string              | 是   | 指定压缩的文件夹路径或者文件路径,对应的路径参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。 |
29| outFile | string              | 是   | 指定压缩结果的文件路径(文件的扩展名zip)。                  |
30| options | [Options](#options) | 是   | 压缩的可选参数。                                             |
31
32**返回值:**
33
34| 类型           | 说明                                                         |
35| -------------- | ------------------------------------------------------------ |
36| Promise\<void> | [ERROR_CODE_OK](#ziperrorcode):压缩成功;<br />[ERROR_CODE_ERRNO](#ziperrorcode):压缩失败。 |
37
38**示例1:**
39
40```typescript
41//【压缩文件 例子1】
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**示例2:**
59
60```typescript
61// 【压缩文件夹 例子2】
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
82解压文件,解压完成返回执行结果(Promise形式)。
83
84> 从api9开始不再看护,建议使用[zlib.decompressFile](#zlibdecompressfile9)
85
86**系统能力:** SystemCapability.BundleManager.Zlib
87
88**参数:**
89
90| 参数名  | 类型                | 必填 | 说明                                                         |
91| ------- | ------------------- | ---- | ------------------------------------------------------------ |
92| inFile  | string              | 是   | 指定压缩的文件夹路径或者文件路径,对应的路径参考[FA模型](js-apis-inner-app-context.md),[stage模型](js-apis-inner-application-context.md)。 |
93| outFile | string              | 是   | 指定的解压文件路径。                                         |
94| options | [Options](#options) | 是   | 解压的可选参数。                                             |
95
96**返回值:**
97
98| 类型           | 说明                                                         |
99| -------------- | ------------------------------------------------------------ |
100| Promise\<void> | [ERROR_CODE_OK](#ziperrorcode):解压成功;<br />[ERROR_CODE_ERRNO](#ziperrorcode):解压失败返回执行结果。 |
101
102**示例:**
103
104```typescript
105// 【解压缩 例子1】
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
126压缩文件,压缩的结果通过callback返回。成功时返回null,失败时返回错误码ID。
127
128**系统能力:** SystemCapability.BundleManager.Zlib
129
130**参数:**
131
132| 参数名                  | 类型                | 必填 | 说明                                                         |
133| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ |
134| inFile                  | string              | 是   | 指定压缩的文件夹路径或者文件路径,对应的路径参考[FA模型](js-apis-inner-app-context.md),[stage模型](js-apis-inner-application-context.md)。 |
135| outFile                 | string              | 是   | 指定的解压文件路径。                                           |
136| options                 | [Options](#options) | 是   | 压缩的配置参数。                                               |
137| AsyncCallback<**void**> | callback            | 否   | 压缩时的回调函数。                                             |
138
139**相关错误码**
140
141以下错误码的详细介绍请参见[ohos.zlib错误码](../errorcodes/errorcode-zlib.md)。
142| 错误码ID | 错误信息                               |
143| -------- | --------------------------------------|
144| 900001   | The Input source file is invalid.      |
145| 900002   | The Input destination file is invalid. |
146
147**示例**
148
149```typescript
150// 【压缩文件 例子1】
151// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过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
172compressFile(inFile: string, outFile: string, options: Options): Promise\<void>;
173
174压缩文件,压缩的结果通过promise返回,成功时返回null,失败时返回错误码。
175
176**系统能力:** SystemCapability.BundleManager.Zlib
177
178**参数:**
179
180| 参数名  | 类型                | 必填 | 说明                                                         |
181| ------- | ------------------- | ---- | ------------------------------------------------------------ |
182| inFile  | string              | 是   | 指定压缩的文件夹路径或者文件路径,对应的路径参考[FA模型](js-apis-inner-app-context.md),[stage模型](js-apis-inner-application-context.md)。 |
183| outFile | string              | 是   | 指定的解压文件路径。                                           |
184| options | [Options](#options) | 是   | 压缩的配置参数。                                               |
185
186**相关错误码**
187
188以下错误码的详细介绍请参见[ohos.zlib错误码](../errorcodes/errorcode-zlib.md)。
189
190| 错误码ID | 错误信息                               |
191| -------- | ------------------------------------- |
192| 900001   | The Input source file is invalid.      |
193| 900002   | The Input destination file is invalid. |
194
195```typescript
196// 【压缩文件 例子2】
197// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过context获取
198import zlib from '@ohos.zlib';
199let inFile = '/xxx/filename.xxx';
200let outFile = '/xxx/xxx.zip';
201let options = {
202  level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
203  memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
204  strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
205};
206
207try {
208    zlib.compressFile(inFile, outFile, options).then((data) => {
209        console.info('compressFile success');
210    }).catch((errData) => {
211        console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
212    })
213} catch(errData) {
214    console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
215}
216```
217
218
219
220## zlib.decompressFile<sup>9+</sup>
221
222decompressFile(inFile: string, outFile: string, options: Options, callback: AsyncCallback\<void>): void;
223
224解压文件,解压的结果通过callback返回,成功时返回null,失败时返回错误码。
225
226**系统能力:** SystemCapability.BundleManager.Zlib
227
228**参数:**
229
230| 参数名                  | 类型                | 必填 | 说明                                                         |
231| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ |
232| inFile                  | string              | 是   | 指定的待解压缩文件的文件路径,对应的路径参考[FA模型](js-apis-inner-app-context.md),[stage模型](js-apis-inner-application-context.md)。 |
233| outFile                 | string              | 是   | 指定的解压后的目录路径。                                     |
234| options                 | [Options](#options) | 是   | 解压的配置参数。                                             |
235| AsyncCallback<**void**> | callback            | 否   | 解压是的回调函数。                                           |
236
237**相关错误码**
238
239以下错误码的详细介绍请参见[ohos.zlib错误码](../errorcodes/errorcode-zlib.md)。
240
241| 错误码ID | 错误信息
242| -------- | --------------------------------------|
243| 900001   | The Input source file is invalid.      |
244| 900002   | The Input destination file is invalid. |
245
246**示例**
247
248```typescript
249// 【解压缩 例子1】
250// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过context获取
251import zlib from '@ohos.zlib';
252let inFile = '/xx/xxx.zip';
253let outFile = '/xxx';
254let options = {
255  level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
256  memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
257  strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
258};
259
260try {
261    zlib.decompressFile(inFile, outFile, options, (errData) => {
262        if (errData !== null) {
263            console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
264        }
265    })
266} catch(errData) {
267    console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
268}
269```
270
271decompressFile(inFile: string, outFile: string, options: Options): Promise\<void>;
272
273解压文件,解压的结果通过promise返回,成功时返回null,失败时返回错误码。
274
275**系统能力:** SystemCapability.BundleManager.Zlib
276
277**参数:**
278
279| 参数名  | 类型                | 必填 | 说明                                                         |
280| ------- | ------------------- | ---- | ------------------------------------------------------------ |
281| inFile  | string              | 是   | 指定的待解压缩文件的文件路径,对应的路径参考[FA模型](js-apis-inner-app-context.md),[stage模型](js-apis-inner-application-context.md)。 |
282| outFile | string              | 是   | 指定的解压后的目录路径。                                       |
283| options | [Options](#options) | 是   | 解压时的配置参数。                                             |
284
285**相关错误码**
286
287以下错误码的详细介绍请参见[ohos.zlib错误码](../errorcodes/errorcode-zlib.md)。
288
289| 错误码ID | 错误信息                               |
290| ------ | ------------------------------------- |
291| 900001 | The Input source file is invalid.      |
292| 900002 | The Input destination file is invalid. |
293
294```typescript
295// 【解压缩 例子2】
296// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过context获取
297import zlib from '@ohos.zlib';
298let inFile = '/xx/xxx.zip';
299let outFile = '/xxx';
300let options = {
301  level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
302  memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
303  strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
304};
305
306try {
307    zlib.decompressFile(inFile, outFile, options).then((data) => {
308        console.info('decompressFile success');
309    }).catch((errData) => {
310        console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
311    })
312} catch(errData) {
313    console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
314}
315```
316
317## Options
318
319**系统能力:** SystemCapability.BundleManager.Zlib
320
321| 名称     | 类型             | 可读 | 可写 | 说明                                                       |
322| -------- | ---------------- | ---- | ---- | ---------------------------------------------------------- |
323| level    | CompressLevel     | 是   | 否   | 参考[zip.CompressLevel枚举定义](#zipcompresslevel)。       |
324| memLevel | MemLevel         | 是   | 否   | 参考[zip.MemLevel枚举定义](#zipmemlevel)。                 |
325| strategy | CompressStrategy | 是   | 否   | 参考[zip.CompressStrategy枚举定义](#zipcompressstrategy)。 |
326
327## zip.CompressLevel
328
329**系统能力:** SystemCapability.BundleManager.Zlib
330
331| 名称                               | 值   | 说明              |
332| ---------------------------------- | ---- | ----------------- |
333| COMPRESS_LEVEL_NO_COMPRESSION      | 0    | 压缩率为0压缩等级。 |
334| COMPRESS_LEVEL_BEST_SPEED          | 1    | 最佳速度压缩等级。  |
335| COMPRESS_LEVEL_BEST_COMPRESSION    | 9    | 最佳压缩等级。      |
336| COMPRESS_LEVEL_DEFAULT_COMPRESSION | -1   | 默认压缩等级。      |
337
338## zip.MemLevel
339
340**系统能力:** SystemCapability.BundleManager.Zlib
341
342| 名称              | 值   | 说明                             |
343| ----------------- | ---- | -------------------------------- |
344| MEM_LEVEL_MIN     | 1    | zip 接口在压缩过程中最小使用内存。 |
345| MEM_LEVEL_MAX     | 9    | zip 接口在压缩过程中最大使用内存。 |
346| MEM_LEVEL_DEFAULT | 8    | zip 接口在压缩过程中默认使用内存。 |
347
348## zip.CompressStrategy
349
350**系统能力:** SystemCapability.BundleManager.Zlib
351
352| 名称                               | 值   | 说明                     |
353| ---------------------------------- | ---- | ------------------------ |
354| COMPRESS_STRATEGY_DEFAULT_STRATEGY | 0    | 常规数据策略。             |
355| COMPRESS_STRATEGY_FILTERED         | 1    | 过滤器产生的数据压缩策略。 |
356| COMPRESS_STRATEGY_HUFFMAN_ONLY     | 2    | 霍夫曼编码格式压缩策略。   |
357| COMPRESS_STRATEGY_RLE              | 3    | 游标编码压缩策略。         |
358| COMPRESS_STRATEGY_FIXED            | 4    | 固定的压缩策略。           |
359
360## zip.ErrorCode
361
362**系统能力:** SystemCapability.BundleManager.Zlib
363
364| 名称             | 值   | 说明         |
365| ---------------- | ---- | ------------ |
366| ERROR_CODE_OK    | 0    | 函数调用成功。 |
367| ERROR_CODE_ERRNO | -1   | 函数调用失败。 |
368