# ohos_7zip **Repository Path**: wolfx/ohos_7zip ## Basic Information - **Project Name**: ohos_7zip - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2025-09-16 - **Last Updated**: 2025-09-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ohos_7zip 对7zip命令行的封装,支持沙箱路径文件的压缩和解压,支持压缩和解压的规格参考官网[7-zip](https://7-zip.org/),不支持压缩解压到内存,提供ArkTS和Capi两套接口。 ## 已测试兼容环境 - OpenHarmony SDK Ohos_sdk_public 5.0.4.150 (API Version 16 Release) - DevEco Studio 5.0.4 Release ## 用户指南 ### 快速接入 #### ohmp har包依赖 指定路径下(如:项目根路径/entry),输入如下命令安装ohpm har包依赖(**当前还未发布,无法安装,请本地编译出har使用**) ``` cd entry ohpm install @ohos/oh7zip ``` 如果**使用Capi**需要在CMakeLists.txt添加依赖,**否则可忽略**: ``` set(OH7ZIP_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules/@ohos/oh7zip) # 设置oh7zip根路径 set(CMAKE_MODULE_PATH ${OH7ZIP_ROOT_PATH}) find_package(OH7ZIP REQUIRED) ... target_link_libraries(entry PUBLIC OH7ZIP::oh7zip) # 链接二进制依赖 & 头文件 entry为工程库名 ``` ### 接口说明 ##### 结构体Config7z变量说明 | 名称 | 描述 | | ------------------------------ | ---------------------------------------------------------- | | std::vector\ src | 压缩的文件或目录/解压的文件(解压文件仅src[0]生效) | | std::string dst | 压缩生成的目标文件/解压文件后存放的目录 | | std::string pwd | 压缩/解压的密码,可选 | | std::string fmt | 压缩的格式,"zip", "7z", "tar", "xz", "gzip", "bzip2" | | std::vector\ xr | 压缩时,需要排除递归子目录中的文件,例如"!hello.*", "!dir" | ##### 枚举类ErrorInfo成员说明 | 名称 | 描述 | | ------------------------ | ------------------------------------------------------------ | | OK | 0 压缩/解压成功 | | CONFIG_NULL | 1 解析Config7z为空 | | ILLEGAL_SRC | 2 传入非法的src路径 | | ILLEGAL_DST | 3 传入非法的dst路径 | | DST_NO_PERMISSION | 4 传入的dst路径合法,但是没有权限 | | COMPRESS_FAIL | 5 调用7zip压缩失败 | | UNCOMPRESS_FAIL | 6 调用7zip解压失败 | | ASYNCDATA_NULL | 7 异步数据为空 | | COMPRESS_FMT_NOT_SUPPURT | 8 传入的压缩格式不支持 | | COMPRESS_FMT_SRC_ILLEGAL | 9 传入的压缩格式支持,src不支持,例如压缩gzip格式,传入一个目录而非文件 | | MISSING_PASSWORD | 10 压缩包需要正确的密码,请输入正确的密码 | ##### 类Uncompress接口说明 | 名称 | 描述 | | ------------------------------------------------------------ | -------------------------------- | | ErrorInfo ExtractSync(std::shared_ptr\ config) | 同步操作,解压一个文件到沙箱路径 | | std::shared_future\ ExtractAsync(std::shared_ptr\ config) | 异步操作,解压一个文件到沙箱路径 | ##### 类Compress接口说明 | 名称 | 描述 | | ------------------------------------------------------------ | ---------------------------------- | | ErrorInfo CompressSync(std::shared_ptr\ config) | 同步操作,压缩文件或目录到沙箱路径 | | std::shared_future\ CompressAsync(std::shared_ptr\ config) | 异步操作,压缩文件或目录到沙箱路径 | ### 使用示例 - C++ ``` #include "uncompress.h" #include "common.h" using namespace Oh7zip; void Test() { // 创建解压配置 std::shared_ptr config = std::make_shared(); config->src.push_back(xxxx); config->dst = xxxx; config->pwd = xxxx; // 创建解压句柄 std::shared_ptr uncompress = std::make_shared(); // 调用同步解压 uncompress->ExtractSync(config); // 调用异步解压 uncompress->ExtractAsync(config); } ``` - arkts ``` import { Uncompress, Config7z } from '@ohos/oh7zip' test() { let uncompress: Uncompress = new Uncompress() let config: Config7z = { src: getContext().resourceDir + "/test_7z.7z", dst: getContext().filesDir, pwd: "test_7z" } // 同步 uncompress.extractSync(config) // 异步 uncompress.extractAsync(config).then((result)=>{}) } ```