1. install libzip39Please respect copyright.PENANAjtRWtJ7i7R
# sudo apt install libzip-dev39Please respect copyright.PENANAy7s6csA30I
check its version: in ubuntu 24, the version is 1.7.339Please respect copyright.PENANAxVw39IzODI
# apt show libzip-dev39Please respect copyright.PENANAj6wach1SJA
2. ref the libzip doc (the doc version is 1.11.2, it's not the same as the version we installed)39Please respect copyright.PENANAVU3vnHHMdU
https://libzip.org/documentation/zip_open.html39Please respect copyright.PENANAe7rJv6jqLZ
https://libzip.org/documentation/zip_source_file.html39Please respect copyright.PENANAhehTLosEmo
https://libzip.org/documentation/zip_file_add.html
39Please respect copyright.PENANA3rHVFooqKK
3. use gemini to get an example, and improve it.
#include <zip.h>39Please respect copyright.PENANAWkqmFwmPvG
#include <stdio.h>39Please respect copyright.PENANAm7uxn9gRo8
#include <stdlib.h>39Please respect copyright.PENANAoWcvZY7lwD
#include <string.h>
// zip_path the result zip path39Please respect copyright.PENANA8R7TqnP0wt
// file_list is the list of filenames to be compressed, file_list_len is the count of filenames39Please respect copyright.PENANAUk4Rhr9oAy
// parent_path is the dir path of the filenames, it must be ended by /39Please respect copyright.PENANAKyV6XIFOD4
const char* file_make_zip(const char* zip_path, const char** file_list, int32_t file_list_len, const char* parent_path)39Please respect copyright.PENANAnqlzngp2wy
{39Please respect copyright.PENANAESKtv3qP25
zip_t *zip = zip_open(zip_path, ZIP_CREATE | ZIP_TRUNCATE, NULL); 39Please respect copyright.PENANAamLtGnnFkR
if (!zip) { printf("make zip file error: %s", zip_path); return NULL; }39Please respect copyright.PENANAP7QLJ3nQMR
size_t parent_path_len=strlen(parent_path); 39Please respect copyright.PENANAd7djF238WB
for(int32_t i=0;i<file_list_len;i++)39Please respect copyright.PENANAYzJxy95KBE
{39Please respect copyright.PENANAbCkyYZaicQ
const char* item_name=file_list[i]; size_t item_path_size=parent_path_len + strlen(item_name) + 1; 39Please respect copyright.PENANAWySFgnC16x
char* item_path=malloc(item_path_size); snprintf(item_path, item_path_size, "%s%s", parent_path, item_name); 39Please respect copyright.PENANADglNW3YOoy
zip_source_t * zip_source = zip_source_file(zip, item_path, 0, 0); 39Please respect copyright.PENANANIdqekRy5j
if(zip_source==NULL){ printf("make zip item source error: %s", item_path); return NULL; } 39Please respect copyright.PENANA8cv0ekq3Gk
if(zip_file_add(zip, item_name, zip_source, ZIP_FL_ENC_UTF_8)<0)39Please respect copyright.PENANAXNndynuRSD
{ zip_source_free(zip_source); printf("add zip item error: %s", item_path); return NULL; }39Please respect copyright.PENANA8pcJGPKiOj
} 39Please respect copyright.PENANAUljiyRAOLL
zip_close(zip); return zip_path;39Please respect copyright.PENANABYZI4KVEpJ
}
4. add -lzip when compile
Refs:39Please respect copyright.PENANAALVHHl8Faj
gemini ai39Please respect copyright.PENANAwEN8z8jmN3
https://www.cnblogs.com/charlee44/p/18299531
39Please respect copyright.PENANAUoYy7UlY9L
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.com39Please respect copyright.PENANAiy3Bu5orGU
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: 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.18da2