harmony 鸿蒙@ohos.zlib (Zip)

2022-08-09 浏览 (1024)

@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

NameTypeMandatoryDescription
inFilestringYesPath 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 and Stage Model.
outFilestringYesPath of the zipped file. The file name extension is .zip.
optionsOptionsYesOptional parameters for the zip operation.

Return value

TypeDescription
Promise<void>Returns ERROR_CODE_OK if the operation is successful.
Returns ERROR_CODE_ERRNO if the operation fails.

Example 1

// Zip 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';
import { BusinessError } from '@ohos.base';

let inFile = '/xxx/filename.xxx';
let outFile = '/xxx/xxx.zip';
let options: zlib.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: void) => {
    console.log('zipFile result is ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
    console.log('error is ' + JSON.stringify(err));
});

Example 2

// Zip a folder.
// 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';
import { BusinessError } from '@ohos.base';

let inFile = '/xxx/xxx';
let outFile = '/xxx/xxx.zip';
let options: zlib.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: void) => {
    console.log('zipFile result is ' + JSON.stringify(data));
}).catch((err: BusinessError)=>{
    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

NameTypeMandatoryDescription
inFilestringYesPath 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 and Stage Model.
outFilestringYesPath of the unzipped file.
optionsOptionsYesOptional parameters for the unzip operation.

Return value

TypeDescription
Promise<void>Returns ERROR_CODE_OK if the operation is successful.
Returns ERROR_CODE_ERRNO if the operation fails.

Example

// Unzip 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';
import { BusinessError } from '@ohos.base';

let inFile = '/xx/xxx.zip';
let outFile = '/xxx';

let options: zlib.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: void) => {
    console.log('unzipFile result is ' + JSON.stringify(data));
}).catch((err: BusinessError)=>{
    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

NameTypeMandatoryDescription
inFilestringYesPath 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 and Stage Model.
outFilestringYesPath of the compressed file.
optionsOptionsYesCompression parameters.
AsyncCallback<void>callbackNoCallback 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.

IDError Message
900001The input source file is invalid.
900002The 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';
import { BusinessError } from '@ohos.base';

let inFile = '/xxx/filename.xxx';
let outFile = '/xxx/xxx.zip';
let options: zlib.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: BusinessError) => {
        if (errData !== null) {
            console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
        }
    })
} catch(errData) {
    let code = (errData as BusinessError).code;
    let message = (errData as BusinessError).message;
    console.log(`errData is errCode:${code}  message:${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

NameTypeMandatoryDescription
inFilestringYesPath 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 and Stage Model.
outFilestringYesPath of the compressed file.
optionsOptionsYesCompression parameters.

Error codes

For details about the error codes, see zlib Error Codes.

IDError Message
900001The input source file is invalid.
900002The 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';
import { BusinessError } from '@ohos.base';

let inFile = '/xxx/filename.xxx';
let outFile = '/xxx/xxx.zip';
let options: zlib.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: void) => {
        console.info('compressFile success');
    }).catch((errData: BusinessError) => {
        console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
    })
} catch(errData) {
    let code = (errData as BusinessError).code;
    let message = (errData as BusinessError).message;
    console.log(`errData is errCode:${code}  message:${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

NameTypeMandatoryDescription
inFilestringYesPath of the file to decompress. The file name extension must be .zip. The path must be an application sandbox path, which can be obtained from the context. For details about the context, see FA Model and Stage Model.
outFilestringYesPath 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 and Stage Model. If a file or folder with the same name already exists in the path, they will be overwritten.
optionsOptionsYesDecompression parameters.
AsyncCallback<void>callbackYesCallback 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.

IDError Message
900001The input source file is invalid.
900002The input destination file is invalid.
900003The input source file is not ZIP format or damaged.

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';
import { BusinessError } from '@ohos.base';

let inFile = '/xx/xxx.zip';
let outFileDir = '/xxx';
let options: zlib.Options = {
  level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION
};

try {
    zlib.decompressFile(inFile, outFileDir, options, (errData: BusinessError) => {
        if (errData !== null) {
            console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
        }
    })
} catch(errData) {
    let code = (errData as BusinessError).code;
    let message = (errData as BusinessError).message;
    console.log(`errData is errCode:${code}  message:${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

NameTypeMandatoryDescription
inFilestringYesPath of the file to decompress. The file name extension must be .zip. The path must be an application sandbox path, which can be obtained from the context. For details about the context, see FA Model and Stage Model.
outFilestringYesPath 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 and Stage Model. If a file or folder with the same name already exists in the path, they will be overwritten.
optionsOptionsNoDecompression parameters.

Error codes

For details about the error codes, see zlib Error Codes.

IDError Message
900001The input source file is invalid.
900002The input destination file is invalid.
900003The input source file is not ZIP format or damaged.
// 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';
import { BusinessError } from '@ohos.base';

let inFile = '/xx/xxx.zip';
let outFileDir = '/xxx';
let options: zlib.Options = {
  level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION
};

try {
    zlib.decompressFile(inFile, outFileDir, options).then((data: void) => {
        console.info('decompressFile success');
    }).catch((errData: BusinessError) => {
        console.log(`errData is errCode:${errData.code}  message:${errData.message}`);
    })
} catch(errData) {
    let code = (errData as BusinessError).code;
    let message = (errData as BusinessError).message;
    console.log(`errData is errCode:${code}  message:${message}`);
}

zlib.decompressFile10+

decompressFile(inFile: string, outFile: string, callback: AsyncCallback<void>): void;

Decompresses a file. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.BundleManager.Zlib

Parameters

NameTypeMandatoryDescription
inFilestringYesPath of the file to decompress. The file name extension must be .zip. The path must be an application sandbox path, which can be obtained from the context. For details about the context, see FA Model and Stage Model.
outFilestringYesPath 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 and Stage Model. If a file or folder with the same name already exists in the path, they will be overwritten.
AsyncCallback<void>callbackYesCallback 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.

IDError Message
900001The input source file is invalid.
900002The input destination file is invalid.
900003The input source file is not ZIP format or damaged.

Example

// Unzip 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';
import { BusinessError } from '@ohos.base';
let inFile = '/xx/xxx.zip';
let outFileDir = '/xxx';

try {
    zlib.decompressFile(inFile, outFileDir, (errData: BusinessError) => {
        if (errData !== null) {
            console.log(`decompressFile failed. code is ${errData.code}, message is ${errData.message}`);
        }
    })
} catch(errData) {
    let code = (errData as BusinessError).code;
    let message = (errData as BusinessError).message;
    console.log(`decompressFile failed. code is ${code}, message is ${message}`);
}

Options

System capability: SystemCapability.BundleManager.Zlib

NameTypeReadableWritableDescription
levelCompressLevelYesNoSee zip.CompressLevel.
memLevelMemLevelYesNoSee zip.MemLevel.
strategyCompressStrategyYesNoSee zip.CompressStrategy.

zip.CompressLevel

System capability: SystemCapability.BundleManager.Zlib

NameValueDescription
COMPRESS_LEVEL_NO_COMPRESSION0Compress level 0 that indicates uncompressed.
COMPRESS_LEVEL_BEST_SPEED1Compression level 1 that gives the best speed.
COMPRESS_LEVEL_BEST_COMPRESSION9Compression level 9 that gives the best compression.
COMPRESS_LEVEL_DEFAULT_COMPRESSION-1Default compression level.

zip.MemLevel

System capability: SystemCapability.BundleManager.Zlib

NameValueDescription
MEM_LEVEL_MIN1Minimum memory used by the zip API during compression.
MEM_LEVEL_MAX9Maximum memory used by the zip API during compression.
MEM_LEVEL_DEFAULT8Default memory used by the zip API during compression.

zip.CompressStrategy

System capability: SystemCapability.BundleManager.Zlib

NameValueDescription
COMPRESS_STRATEGY_DEFAULT_STRATEGY0Default compression strategy.
COMPRESS_STRATEGY_FILTERED1Filtered compression strategy.
COMPRESS_STRATEGY_HUFFMAN_ONLY2Huffman coding compression strategy.
COMPRESS_STRATEGY_RLE3RLE compression strategy.
COMPRESS_STRATEGY_FIXED4Fixed compression strategy.

zip.ErrorCode

System capability: SystemCapability.BundleManager.Zlib

NameValueDescription
ERROR_CODE_OK0The API is successfully called.
ERROR_CODE_ERRNO-1Failed to call the API.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/1c1a25efe3bb49dc8bc8c2bf8095b819