要在C++中使用minizip,您需要包括minizip的头文件,并链接minizip的库文件。以下是一个示例代码,演示如何使用minizip来创建一个zip文件:
#include "minizip/zip.h"
#include "minizip/unzip.h"
int main() {
// 创建一个zip文件
const char* zipfilename = "test.zip";
zipFile zf = zipOpen(zipfilename, APPEND_STATUS_CREATE);
if (zf == NULL) {
printf("Error opening %s\n", zipfilename);
return -1;
}
// 添加文件到zip文件
const char* filename = "file.txt";
const char* filedata = "Hello, World!";
zip_fileinfo zi = {0};
zipOpenNewFileInZip(zf, filename, &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
zipWriteInFileInZip(zf, filedata, strlen(filedata));
zipCloseFileInZip(zf);
// 关闭zip文件
zipClose(zf, NULL);
return 0;
}
请记得在编译时链接minizip的库文件,以确保代码可以正确编译和运行。希望对您有帮助!