// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".
#include <iostream>
#include <cerrno>
#include "minizip/unzip.h"
#include "hilog/log.h"
#include <fstream> // 包含对文件操作的支持
int readZipEntryTest(std::string inFile) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- inFile: %{public}s", inFile.c_str());
zlib_filefunc_def *zipFuncPtrs = nullptr;
unzFile zipFile = unzOpen2(inFile.c_str(), zipFuncPtrs);
if (zipFile == nullptr) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- errno: %{public}d - %{public}s", errno, strerror(errno));
return -1;
}
unz_global_info unzInfo = {}; // Zero-clear.
if (unzGetGlobalInfo(zipFile, &unzInfo) != UNZ_OK) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- errno: %{public}d - %{public}s", errno,
strerror(errno));
return -2;
}
do {
unz_file_info raw_file_info = {};
char raw_file_name_in_zip[256] = {};
int result = unzGetCurrentFileInfo(zipFile, &raw_file_info, raw_file_name_in_zip,
sizeof(raw_file_name_in_zip) - 1, NULL, 0, NULL, 0);
if (result != UNZ_OK) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- errno: %{public}d - %{public}s", errno,
strerror(errno));
return -3;
}
if (raw_file_name_in_zip[0] == '\0') {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- errno: %{public}d - %{public}s", errno,
strerror(errno));
return -4;
}
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- zipName: %{public}s", raw_file_name_in_zip);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", " compressed_size: %{public}d", (int)raw_file_info.compressed_size);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", " uncompressed_size: %{public}d", (int)raw_file_info.uncompressed_size);
unz_file_pos position = {};
if (unzGetFilePos(zipFile, &position) != UNZ_OK) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- errno: %{public}d - %{public}s", errno,
strerror(errno));
return -5;
}
if (position.num_of_file + 1 == unzInfo.number_entry) {
break;
} else {
if (unzGoToNextFile(zipFile) != UNZ_OK) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- errno: %{public}d - %{public}s", errno,
strerror(errno));
return -6;
}
}
} while (true);
return 0;
}
int createFile(std::string inFile) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "ndkTest", "-- createFile: %{public}s", inFile.c_str());
std::ofstream myfile(inFile);
// 检查文件是否成功打开
if (myfile.is_open()) {
// 向文件写入内容
myfile << "Hello, this is a test.\n";
myfile << "Writing to a file in C++ is easy!\n";
// 关闭文件
myfile.close();
std::cout << "File successfully created and written to." << std::endl;
} else {
std::cout << "Unable to open file";
}
return 0;
}
reader.h头文件中的内容\#
//
// Created on 2024/2/24.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".
#ifndef MYAPPNDK_READER_H
#define MYAPPNDK_READER_H
#include <string>
int readZipEntryTest(std::string inFile);
int createFile(std::string inFile);
#endif //MYAPPNDK_READER_H
请参考参考文档:
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/rawfile-V5
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/resourcemanager-V5
操作filesDir请参考:
napi\_init.cpp文件中的内容\#
reader.cpp文件中的内容\#
reader.h头文件中的内容\#
libentry目录中index.d.ts文件中的内容
CMakeLists.txt文件中的内容
ArkTS页面处调用的内容