1. install libzip21Please respect copyright.PENANAvnzGL74LDM
# sudo apt install libzip-dev21Please respect copyright.PENANAMSG6ZoHkRx
check its version: in ubuntu 24, the version is 1.7.321Please respect copyright.PENANACSbJLP2VB0
# apt show libzip-dev21Please respect copyright.PENANABtiIJlkHwH
2. ref the libzip doc (the doc version is 1.11.2, it's not the same as the version we installed)21Please respect copyright.PENANAuI4hEKV49K
https://libzip.org/documentation/zip_open.html21Please respect copyright.PENANAjheBnySLz4
https://libzip.org/documentation/zip_source_file.html21Please respect copyright.PENANApn8Zs8Lamg
https://libzip.org/documentation/zip_file_add.html
21Please respect copyright.PENANApYeTdbGE6r
3. use gemini to get an example, and improve it.
#include <zip.h>21Please respect copyright.PENANAJPCFx84rRO
#include <stdio.h>21Please respect copyright.PENANATDJ9U7KFBX
#include <stdlib.h>21Please respect copyright.PENANARwgnTk3IVF
#include <string.h>
// zip_path the result zip path21Please respect copyright.PENANAlafyUxWwXw
// file_list is the list of filenames to be compressed, file_list_len is the count of filenames21Please respect copyright.PENANAAV3F0W8AJM
// parent_path is the dir path of the filenames, it must be ended by /21Please respect copyright.PENANAw7rUP2Gg5l
const char* file_make_zip(const char* zip_path, const char** file_list, int32_t file_list_len, const char* parent_path)21Please respect copyright.PENANAYEqji3rWKl
{21Please respect copyright.PENANAudlOBT9MW3
zip_t *zip = zip_open(zip_path, ZIP_CREATE | ZIP_TRUNCATE, NULL); 21Please respect copyright.PENANAqtvBlRv1wY
if (!zip) { printf("make zip file error: %s", zip_path); return NULL; }21Please respect copyright.PENANAnXL3zCW04A
size_t parent_path_len=strlen(parent_path); 21Please respect copyright.PENANAL7NHM1LbxI
for(int32_t i=0;i<file_list_len;i++)21Please respect copyright.PENANAkQrnYC2BOx
{21Please respect copyright.PENANAKvW1hXwD48
const char* item_name=file_list[i]; size_t item_path_size=parent_path_len + strlen(item_name) + 1; 21Please respect copyright.PENANAVsN1t39SoR
char* item_path=malloc(item_path_size); snprintf(item_path, item_path_size, "%s%s", parent_path, item_name); 21Please respect copyright.PENANAW4zjyH7dno
zip_source_t * zip_source = zip_source_file(zip, item_path, 0, 0); 21Please respect copyright.PENANAIMOmrWn6eH
if(zip_source==NULL){ printf("make zip item source error: %s", item_path); return NULL; } 21Please respect copyright.PENANAkPMi2k7Yz0
if(zip_file_add(zip, item_name, zip_source, ZIP_FL_ENC_UTF_8)<0)21Please respect copyright.PENANALrWLaGDqSS
{ zip_source_free(zip_source); printf("add zip item error: %s", item_path); return NULL; }21Please respect copyright.PENANAiQMsB3y1Py
} 21Please respect copyright.PENANADtpTvzfyyl
zip_close(zip); return zip_path;21Please respect copyright.PENANAwqXLtfPRvQ
}
4. add -lzip when compile
Refs:21Please respect copyright.PENANAJ0e0cAC0Dq
gemini ai21Please respect copyright.PENANAC86q9d3Rbr
https://www.cnblogs.com/charlee44/p/18299531
21Please respect copyright.PENANAPBDWcqGKFq
If you found any errors or have any suggestions for this article, please let me know, my wechat: si_jinmin, my email: si.jinmin@gmail.com21Please respect copyright.PENANA7tf9po8XYC
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: si_jinmin, 我的email: si.jinmin@gmail.com
如果您對C/C++ programming, Linux, website development, Vue, Git, vscode感興趣,邀請您加入「Linux/C/C++ Website Development」 微信群,請加我的微信(si_jinmin)以便拉您进群。
ns 15.158.61.37da2