要在C++中使用minizip,您需要包括minizip的頭文件,并鏈接minizip的庫(kù)文件。以下是一個(gè)示例代碼,演示如何使用minizip來創(chuàng)建一個(gè)zip文件:
#include "minizip/zip.h"
#include "minizip/unzip.h"
int main() {
// 創(chuàng)建一個(gè)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);
// 關(guān)閉zip文件
zipClose(zf, NULL);
return 0;
}
請(qǐng)記得在編譯時(shí)鏈接minizip的庫(kù)文件,以確保代碼可以正確編譯和運(yùn)行。希望對(duì)您有幫助!