@ohos.zlib (Zip)
The Zip module provides APIs for file compression and decompression.
NOTE
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.
Modules to Import
import zlib from '@ohos.zlib';
zlib.zipFile(deprecated)
zipFile(inFile: string, outFile: string, options: Options): Promise<void>
Zips a file. This API uses a promise to return the result.
This API is deprecated since API version 9. You are advised to use zlib.compressFile instead.
System capability: SystemCapability.BundleManager.Zlib
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
inFile | string | Yes | Path of the folder or file to zip. For details about the path, see FA Model and Stage Model. |
outFile | string | Yes | Path of the zipped file. The file name extension is .zip. |
options | Options | Yes | Optional parameters for the zip operation. |
Return value
Type | Description |
---|---|
Promise<void> | Returns ERROR_CODE_OK if the operation is successful. Returns ERROR_CODE_ERRNO if the operation fails. |
Example 1
// Zip a file.
import zlib from '@ohos.zlib';
let inFile = '/xxx/filename.xxx';
let outFile = '/xxx/xxx.zip';
let options = {
level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
};
zlib.zipFile(inFile, outFile, options).then((data) => {
console.log('zipFile result is ' + JSON.stringify(data));
}).catch((err) => {
console.log('error is ' + JSON.stringify(err));
});
Example 2
// Zip a folder.
import zlib from '@ohos.zlib';
let inFile = '/xxx/xxx';
let outFile = '/xxx/xxx.zip';
let options = {
level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
};
zlib.zipFile(inFile , outFile, options).then((data) => {
console.log('zipFile result is ' + JSON.stringify(data));
}).catch((err)=>{
console.log('error is ' + JSON.stringify(err));
});
zlib.unzipFile(deprecated)
unzipFile(inFile:string, outFile:string, options: Options): Promise<void>
Unzips a file. This API uses a promise to return the result.
This API is deprecated since API version 9. You are advised to use zlib.decompressFile instead.
System capability: SystemCapability.BundleManager.Zlib
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
inFile | string | Yes | Path of the folder or file to unzip. For details about the path, see FA Model and Stage Model. |
outFile | string | Yes | Path of the unzipped file. |
options | Options | Yes | Optional parameters for the unzip operation. |
Return value
Type | Description |
---|---|
Promise<void> | Returns ERROR_CODE_OK if the operation is successful. Returns ERROR_CODE_ERRNO if the operation fails. |
Example
// Unzip a file.
import zlib from '@ohos.zlib';
let inFile = '/xx/xxx.zip';
let outFile = '/xxx';
let options = {
level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
};
zlib.unzipFile(inFile, outFile, options).then((data) => {
console.log('unzipFile result is ' + JSON.stringify(data));
}).catch((err)=>{
console.log('error is ' + JSON.stringify(err));
})
zlib.compressFile9+
compressFile(inFile: string, outFile: string, options: Options, callback: AsyncCallback<void>): void;
Compresses a file. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.BundleManager.Zlib
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
inFile | string | Yes | Path of the folder or file to compress. For details about the path, see FA Model and Stage Model. |
outFile | string | Yes | Path of the compressed file. |
options | Options | Yes | Compression parameters. |
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. |
Error codes
For details about the error codes, see zlib Error Codes.
ID | Error Message |
---|---|
900001 | The Input source file is invalid. |
900002 | The Input destination file is invalid. |
Example
// Compress a file.
// 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.
import zlib from '@ohos.zlib';
let inFile = '/xxx/filename.xxx';
let outFile = '/xxx/xxx.zip';
let options = {
level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
};
try {
zlib.compressFile(inFile, outFile, options, (errData) => {
if (errData !== null) {
console.log(`errData is errCode:${errData.code} message:${errData.message}`);
}
})
} catch(errData) {
console.log(`errData is errCode:${errData.code} message:${errData.message}`);
}
zlib.compressFile9+
compressFile(inFile: string, outFile: string, options: Options): Promise<void>;
Compresses a file. This API uses a promise to return the result.
System capability: SystemCapability.BundleManager.Zlib
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
inFile | string | Yes | Path of the folder or file to compress. For details about the path, see FA Model and Stage Model. |
outFile | string | Yes | Path of the compressed file. |
options | Options | Yes | Compression parameters. |
Error codes
For details about the error codes, see zlib Error Codes.
ID | Error Message |
---|---|
900001 | The Input source file is invalid. |
900002 | The Input destination file is invalid. |
// Compress a file.
// 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.
import zlib from '@ohos.zlib';
let inFile = '/xxx/filename.xxx';
let outFile = '/xxx/xxx.zip';
let options = {
level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
};
try {
zlib.compressFile(inFile, outFile, options).then((data) => {
console.info('compressFile success');
}).catch((errData) => {
console.log(`errData is errCode:${errData.code} message:${errData.message}`);
})
} catch(errData) {
console.log(`errData is errCode:${errData.code} message:${errData.message}`);
}
zlib.decompressFile9+
decompressFile(inFile: string, outFile: string, options: Options, callback: AsyncCallback<void>): void;
Decompresses a file. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.BundleManager.Zlib
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
inFile | string | Yes | Path of the file to decompress. For details about the path, see FA Model and Stage Model. |
outFile | string | Yes | Path of the decompressed file. |
options | Options | Yes | Decompression parameters. |
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. |
Error codes
For details about the error codes, see zlib Error Codes.
ID | Error Message |
---|---|
900001 | The Input source file is invalid. |
900002 | The Input destination file is invalid. |
Example
// Decompress a file.
// 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.
import zlib from '@ohos.zlib';
let inFile = '/xx/xxx.zip';
let outFile = '/xxx';
let options = {
level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
};
try {
zlib.decompressFile(inFile, outFile, options, (errData) => {
if (errData !== null) {
console.log(`errData is errCode:${errData.code} message:${errData.message}`);
}
})
} catch(errData) {
console.log(`errData is errCode:${errData.code} message:${errData.message}`);
}
zlib.decompressFile9+
decompressFile(inFile: string, outFile: string, options: Options): Promise<void>;
Decompresses a file. This API uses a promise to return the result.
System capability: SystemCapability.BundleManager.Zlib
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
inFile | string | Yes | Path of the file to decompress. For details about the path, see FA Model and Stage Model. |
outFile | string | Yes | Path of the decompressed file. |
options | Options | Yes | Decompression parameters. |
Error codes
For details about the error codes, see zlib Error Codes.
ID | Error Message |
---|---|
900001 | The Input source file is invalid. |
900002 | The Input destination file is invalid. |
// Decompress a file.
// 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.
import zlib from '@ohos.zlib';
let inFile = '/xx/xxx.zip';
let outFile = '/xxx';
let options = {
level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
};
try {
zlib.decompressFile(inFile, outFile, options).then((data) => {
console.info('decompressFile success');
}).catch((errData) => {
console.log(`errData is errCode:${errData.code} message:${errData.message}`);
})
} catch(errData) {
console.log(`errData is errCode:${errData.code} message:${errData.message}`);
}
Options
System capability: SystemCapability.BundleManager.Zlib
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
level | CompressLevel | Yes | No | See zip.CompressLevel. |
memLevel | MemLevel | Yes | No | See zip.MemLevel. |
strategy | CompressStrategy | Yes | No | See zip.CompressStrategy. |
zip.CompressLevel
System capability: SystemCapability.BundleManager.Zlib
Name | Value | Description |
---|---|---|
COMPRESS_LEVEL_NO_COMPRESSION | 0 | Compress level 0 that indicates uncompressed. |
COMPRESS_LEVEL_BEST_SPEED | 1 | Compression level 1 that gives the best speed. |
COMPRESS_LEVEL_BEST_COMPRESSION | 9 | Compression level 9 that gives the best compression. |
COMPRESS_LEVEL_DEFAULT_COMPRESSION | -1 | Default compression level. |
zip.MemLevel
System capability: SystemCapability.BundleManager.Zlib
Name | Value | Description |
---|---|---|
MEM_LEVEL_MIN | 1 | Minimum memory used by the zip API during compression. |
MEM_LEVEL_MAX | 9 | Maximum memory used by the zip API during compression. |
MEM_LEVEL_DEFAULT | 8 | Default memory used by the zip API during compression. |
zip.CompressStrategy
System capability: SystemCapability.BundleManager.Zlib
Name | Value | Description |
---|---|---|
COMPRESS_STRATEGY_DEFAULT_STRATEGY | 0 | Default compression strategy. |
COMPRESS_STRATEGY_FILTERED | 1 | Filtered compression strategy. |
COMPRESS_STRATEGY_HUFFMAN_ONLY | 2 | Huffman coding compression strategy. |
COMPRESS_STRATEGY_RLE | 3 | RLE compression strategy. |
COMPRESS_STRATEGY_FIXED | 4 | Fixed compression strategy. |
zip.ErrorCode
System capability: SystemCapability.BundleManager.Zlib
Name | Value | Description |
---|---|---|
ERROR_CODE_OK | 0 | The API is successfully called. |
ERROR_CODE_ERRNO | -1 | Failed to call the API. |