要在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的庫文件,以確保代碼可以正確編譯和運行。希望對您有幫助!