From 59b7942cd8fc30ce046c57f0483c248180dcb92c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Podg=C3=B3rski?= Date: Wed, 19 Aug 2020 21:49:24 +0200 Subject: [PATCH] Add example how to compress a folder. --- README.md | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4031c1e..1f6a94b 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,38 @@ for (i = 0; i < n; ++i) { zip_close(zip); ``` +* Compress folder (recursively) +```c +void zip_walk(struct zip_t *zip, const char *path) { + DIR *dir; + struct dirent *entry; + char fullpath[MAX_PATH]; + struct stat s; + + memset(fullpath, 0, MAX_PATH); + dir = opendir(path); + assert(dir); + + while ((entry = readdir(dir))) { + // skip "." and ".." + if (!strcmp(entry->d_name, ".\0") || !strcmp(entry->d_name, "..\0")) + continue; + + snprintf(fullpath, sizeof(fullpath), "%s/%s", path, entry->d_name); + stat(path, &s); + if (S_ISDIR(s.st_mode)) + zip_walk(zip, fullpath); + else { + zip_entry_open(zip, fullpath); + zip_entry_fwrite(zip, fullpath); + zip_entry_close(zip); + } + } + + closedir(dir); +} +``` + # Bindings Compile zip library as a dynamic library. ```shell @@ -338,9 +370,9 @@ extern "libzip.so" func zip_entry_fwrite(zip: c_ptr, filename: string) -> int func main() -> int { let content = "Test content" - + let zip = zip_open("/tmp/never.zip", 6, 'w'); - + zip_entry_open(zip, "test.file"); zip_entry_fwrite(zip, "/tmp/test.txt"); zip_entry_close(zip);