1. install libzip322Please respect copyright.PENANAwKJoN8sizb
# sudo apt install libzip-dev322Please respect copyright.PENANA270E25j4oU
check its version: in ubuntu 24, the version is 1.7.3322Please respect copyright.PENANAHdRfGy2Ysf
# apt show libzip-dev322Please respect copyright.PENANALL39DRwsDT
2. ref the libzip doc (the doc version is 1.11.2, it's not the same as the version we installed)322Please respect copyright.PENANAMpLkhLebuT
https://libzip.org/documentation/zip_open.html322Please respect copyright.PENANAj1eaODQfVv
https://libzip.org/documentation/zip_source_file.html322Please respect copyright.PENANA4YQJwVpSiJ
https://libzip.org/documentation/zip_file_add.html
322Please respect copyright.PENANAStlOX61tB0
3. use gemini to get an example, and improve it.
#include <zip.h>322Please respect copyright.PENANA5hoNUOfvMa
#include <stdio.h>322Please respect copyright.PENANAfJrDl1WJcX
#include <stdlib.h>322Please respect copyright.PENANABVV8fWT1Rt
#include <string.h>
// zip_path the result zip path322Please respect copyright.PENANACnpWYxSUeH
// file_list is the list of filenames to be compressed, file_list_len is the count of filenames322Please respect copyright.PENANAxVIKz1uC42
// parent_path is the dir path of the filenames, it must be ended by /322Please respect copyright.PENANAXt16fg8mr0
const char* file_make_zip(const char* zip_path, const char** file_list, int32_t file_list_len, const char* parent_path)322Please respect copyright.PENANAy1c2FUfhc9
{322Please respect copyright.PENANA0TuaBiMcr3
zip_t *zip = zip_open(zip_path, ZIP_CREATE | ZIP_TRUNCATE, NULL); 322Please respect copyright.PENANArX2uMCwGPQ
if (!zip) { printf("make zip file error: %s", zip_path); return NULL; }322Please respect copyright.PENANAcPvNzuHheG
size_t parent_path_len=strlen(parent_path); 322Please respect copyright.PENANAW03R6UYdO5
for(int32_t i=0;i<file_list_len;i++)322Please respect copyright.PENANAm2i9Znw8SW
{322Please respect copyright.PENANAg0pOJ7wVA3
const char* item_name=file_list[i]; size_t item_path_size=parent_path_len + strlen(item_name) + 1; 322Please respect copyright.PENANA9N6KS4okcl
char* item_path=malloc(item_path_size); snprintf(item_path, item_path_size, "%s%s", parent_path, item_name); 322Please respect copyright.PENANA8tLAIZZcMY
zip_source_t * zip_source = zip_source_file(zip, item_path, 0, 0); 322Please respect copyright.PENANAvUf2RdsD6U
if(zip_source==NULL){ printf("make zip item source error: %s", item_path); return NULL; } 322Please respect copyright.PENANArLZZa9Z7mR
if(zip_file_add(zip, item_name, zip_source, ZIP_FL_ENC_UTF_8)<0)322Please respect copyright.PENANAFPbWi7cxBN
{ zip_source_free(zip_source); printf("add zip item error: %s", item_path); return NULL; }322Please respect copyright.PENANAxQrwK0aoDy
} 322Please respect copyright.PENANAad0lCpuuuK
zip_close(zip); return zip_path;322Please respect copyright.PENANA0C5ghiijfZ
}
4. add -lzip when compile
Refs:322Please respect copyright.PENANAZzwBIYSh2j
gemini ai322Please respect copyright.PENANAWV6x4Djaqc
https://www.cnblogs.com/charlee44/p/18299531
322Please respect copyright.PENANAeXCTy4op3Y
If you found any errors or have any suggestions for this article, please let me know, my wechat: si_jinmin, my email: [email protected]322Please respect copyright.PENANAm9eaftdHGK
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: si_jinmin, 我的email: [email protected]
如果您對C/C++ programming, Linux, website development, Vue, Git, vscode感興趣,邀請您加入「Linux/C/C++ Website Development」 微信群,請加我的微信(si_jinmin)以便拉您进群。
ns216.73.216.69da2


