• 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> **说明:**
21>
22> 从API version 7 开始支持,从API 9 开始废弃。建议使用[zlib.compressFile](#zlibcompressfile9)。
23
24**系统能力:** SystemCapability.BundleManager.Zlib
25
26**参数:**
27
28| 参数名  | 类型                | 必填 | 说明                                                         |
29| ------- | ------------------- | ---- | ------------------------------------------------------------ |
30| inFile  | string              | 是   | 指定压缩的文件夹路径或者文件路径,路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。 |
31| outFile | string              | 是   | 指定压缩结果的文件路径(文件的扩展名zip)。                  |
32| options | [Options](#options) | 是   | 压缩的可选参数。                                             |
33
34**返回值:**
35
36| 类型           | 说明                                                         |
37| -------------- | ------------------------------------------------------------ |
38| Promise\<void> | Promise对象,无返回值。 |
39
40**示例:**
41
42```typescript
43// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过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&lt;void&gt;
65
66解压文件,解压完成后返回执行结果,使用Promise异步返回。
67
68> **说明:**
69>
70> 从API version 7 开始支持,从API 9 开始废弃。建议使用[zlib.decompressFile](#zlibdecompressfile9)。
71
72**系统能力:** SystemCapability.BundleManager.Zlib
73
74**参数:**
75
76| 参数名  | 类型                | 必填 | 说明                                                         |
77| ------- | ------------------- | ---- | ------------------------------------------------------------ |
78| inFile  | string              | 是   | 指定的待解压缩文件的文件路径,路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。 |
79| outFile | string              | 是   | 指定的解压文件路径。                                         |
80| options | [Options](#options) | 是   | 解压的可选参数。                                             |
81
82**返回值:**
83
84| 类型           | 说明                                                         |
85| -------------- | ------------------------------------------------------------ |
86| Promise\<void> | Promise对象,无返回值。 |
87
88**示例:**
89
90```typescript
91// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过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
114压缩文件,压缩的结果,使用callback异步回调返回。成功返回null,失败返回错误码。
115
116**系统能力:** SystemCapability.BundleManager.Zlib
117
118**参数:**
119
120| 参数名                  | 类型                | 必填 | 说明                                                         |
121| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ |
122| inFile                  | string              | 是   | 指定压缩的文件夹路径或者文件路径,路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。待压缩的文件夹不可为空,否则使用[decompressFile](#zlibdecompressfile9)对压缩后的文件解压时会报错。 |
123| outFile                 | string              | 是   | 指定的压缩结果的文件路径。                                           |
124| options                 | [Options](#options) | 是   | 压缩的配置参数。                                               |
125| callback | AsyncCallback\<void>            | 是   | 异步获取压缩结果之后的回调。成功返回null,失败返回错误码。             |
126
127**错误码:**
128
129以下错误码的详细介绍请参见[ohos.zlib错误码](../errorcodes/errorcode-zlib.md)。
130
131| 错误码ID | 错误信息                               |
132| -------- | --------------------------------------|
133| 900001   | The input source file is invalid.      |
134| 900002   | The input destination file is invalid. |
135
136**示例:**
137
138```typescript
139// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过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
168压缩文件,压缩的结果,使用Promise异步返回。成功时返回null,失败时返回错误码。
169
170**系统能力:** SystemCapability.BundleManager.Zlib
171
172**参数:**
173
174| 参数名  | 类型                | 必填 | 说明                                                         |
175| ------- | ------------------- | ---- | ------------------------------------------------------------ |
176| inFile  | string              | 是   | 指定压缩的文件夹路径或者文件路径,路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。待压缩的文件夹不可为空,否则使用[decompressFile](#zlibdecompressfile9)对压缩后的文件解压时会报错。 |
177| outFile | string              | 是   | 指定的压缩结果的文件路径。                                           |
178| options | [Options](#options) | 是   | 压缩的配置参数。                                               |
179
180**返回值:**
181
182| 类型           | 说明                    |
183| -------------- | ----------------------- |
184| Promise\<void> | Promise对象,无返回值。 |
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**示例:**
196
197```typescript
198// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过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
227解压文件,解压的结果,使用callback异步回调返回。成功时返回null,失败时返回错误码。
228
229**系统能力:** SystemCapability.BundleManager.Zlib
230
231**参数:**
232
233| 参数名                  | 类型                | 必填 | 说明                                                         |
234| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ |
235| inFile                  | string              | 是   | 指定的待解压缩文件的文件路径,文件后缀需要以.zip结尾。文件路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。 |
236| outFile                 | string              | 是   | 指定的解压后的文件夹路径,文件夹目录路径需要在系统中存在,不存在则会解压失败。路径必须为沙箱路径,沙箱路径可以通过context获取,具体方法可参考[application/context(Stage模型)](js-apis-inner-application-context.md)或 [app/context(FA模型)](js-apis-inner-app-context.md)。如果待解压的文件或文件夹在解压后的路径下已经存在,则会直接覆盖同名文件或同名文件夹中的同名文件。 |
237| options                 | [Options](#options) | 是   | 解压的配置参数。                                             |
238| callback | AsyncCallback\<void>            | 是   | 异步获取解压结果之后的回调。成功返回null,失败返回错误码。                                             |
239
240**错误码:**
241
242以下错误码的详细介绍请参见[ohos.zlib错误码](../errorcodes/errorcode-zlib.md)。
243
244| 错误码ID | 错误信息                               |
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**示例:**
251
252```typescript
253// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过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
280解压文件,解压的结果,使用Promise异步返回,成功时返回null,失败时返回错误码。
281
282**系统能力:** SystemCapability.BundleManager.Zlib
283
284**参数:**
285
286| 参数名  | 类型                | 必填 | 说明                                                         |
287| ------- | ------------------- | ---- | ------------------------------------------------------------ |
288| inFile  | string              | 是   | 指定的待解压缩文件的文件路径,文件后缀需要以.zip结尾。文件路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。 |
289| outFile | string              | 是   | 指定的解压后的文件夹路径,文件夹目录路径需要在系统中存在,不存在则会解压失败。路径必须为沙箱路径,沙箱路径可以通过context获取,具体方法可参考[application/context(Stage模型)](js-apis-inner-application-context.md)或 [app/context(FA模型)](js-apis-inner-app-context.md)。如果待解压的文件或文件夹在解压后的路径下已经存在,则会直接覆盖同名文件或同名文件夹中的同名文件。 |
290| options | [Options](#options) | 否   | 解压时的配置参数。                                           |
291
292**返回值:**
293
294| 类型           | 说明                    |
295| -------------- | ----------------------- |
296| Promise\<void> | Promise对象,无返回值。 |
297
298**错误码:**
299
300以下错误码的详细介绍请参见[ohos.zlib错误码](../errorcodes/errorcode-zlib.md)。
301
302| 错误码ID | 错误信息                               |
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**示例:**
309
310```typescript
311// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过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
338解压文件,解压的结果,使用callback异步回调返回。成功时返回null,失败时返回错误码。
339
340**系统能力:** SystemCapability.BundleManager.Zlib
341
342**参数:**
343
344| 参数名                  | 类型                | 必填 | 说明                                                         |
345| ----------------------- | ------------------- | ---- | ------------------------------------------------------------ |
346| inFile                  | string              | 是   | 指定的待解压缩文件的文件路径,文件后缀需要以.zip结尾。文件路径必须为沙箱路径,沙箱路径可以通过context获取,可参考[FA模型](js-apis-inner-app-context.md),[Stage模型](js-apis-inner-application-context.md)。 |
347| outFile                 | string              | 是   | 指定的解压后的文件夹路径,文件夹目录路径需要在系统中存在,不存在则会解压失败。路径必须为沙箱路径,沙箱路径可以通过context获取,具体方法可参考[application/context(Stage模型)](js-apis-inner-application-context.md)或 [app/context(FA模型)](js-apis-inner-app-context.md)。如果待解压的文件或文件夹在解压后的路径下已经存在,则会直接覆盖同名文件或同名文件夹中的同名文件。 |
348| callback | AsyncCallback\<void>            | 是   | 异步获取解压结果之后的回调。成功返回null,失败返回错误码。                                             |
349
350**错误码:**
351
352以下错误码的详细介绍请参见[ohos.zlib错误码](../errorcodes/errorcode-zlib.md)。
353
354| 错误码ID | 错误信息                               |
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**示例:**
361
362```typescript
363// 代码中使用的路径需为应用的沙箱路径,如/data/storage/el2/base/haps,也可以通过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**系统能力:** SystemCapability.BundleManager.Zlib
385
386| 名称     | 类型             | 可读 | 可写 | 说明                                                       |
387| -------- | ---------------- | ---- | ---- | ---------------------------------------------------------- |
388| level    | CompressLevel     | 是   | 否   | 参考[zip.CompressLevel枚举定义](#zipcompresslevel)。       |
389| memLevel | MemLevel         | 是   | 否   | 参考[zip.MemLevel枚举定义](#zipmemlevel)。                 |
390| strategy | CompressStrategy | 是   | 否   | 参考[zip.CompressStrategy枚举定义](#zipcompressstrategy)。 |
391
392## zip.CompressLevel
393
394**系统能力:** SystemCapability.BundleManager.Zlib
395
396| 名称                               | 值   | 说明              |
397| ---------------------------------- | ---- | ----------------- |
398| COMPRESS_LEVEL_NO_COMPRESSION      | 0    | 压缩率为0压缩等级。 |
399| COMPRESS_LEVEL_BEST_SPEED          | 1    | 最佳速度压缩等级。  |
400| COMPRESS_LEVEL_BEST_COMPRESSION    | 9    | 最佳压缩等级。      |
401| COMPRESS_LEVEL_DEFAULT_COMPRESSION | -1   | 默认压缩等级。      |
402
403## zip.MemLevel
404
405**系统能力:** SystemCapability.BundleManager.Zlib
406
407| 名称              | 值   | 说明                             |
408| ----------------- | ---- | -------------------------------- |
409| MEM_LEVEL_MIN     | 1    | zip 接口在压缩过程中最小使用内存。 |
410| MEM_LEVEL_MAX     | 9    | zip 接口在压缩过程中最大使用内存。 |
411| MEM_LEVEL_DEFAULT | 8    | zip 接口在压缩过程中默认使用内存。 |
412
413## zip.CompressStrategy
414
415**系统能力:** SystemCapability.BundleManager.Zlib
416
417| 名称                               | 值   | 说明                     |
418| ---------------------------------- | ---- | ------------------------ |
419| COMPRESS_STRATEGY_DEFAULT_STRATEGY | 0    | 常规数据策略。             |
420| COMPRESS_STRATEGY_FILTERED         | 1    | 过滤器产生的数据压缩策略。 |
421| COMPRESS_STRATEGY_HUFFMAN_ONLY     | 2    | 霍夫曼编码格式压缩策略。   |
422| COMPRESS_STRATEGY_RLE              | 3    | 游标编码压缩策略。         |
423| COMPRESS_STRATEGY_FIXED            | 4    | 固定的压缩策略。           |
424
425## zip.ErrorCode
426
427**系统能力:** SystemCapability.BundleManager.Zlib
428
429| 名称             | 值   | 说明         |
430| ---------------- | ---- | ------------ |
431| ERROR_CODE_OK    | 0    | 函数调用成功。 |
432| ERROR_CODE_ERRNO | -1   | 函数调用失败。 |
433