Add example how to compress a folder.

This commit is contained in:
Kuba Podgórski 2020-08-19 21:49:24 +02:00
parent 529eef88ad
commit 59b7942cd8

View File

@ -172,6 +172,38 @@ for (i = 0; i < n; ++i) {
zip_close(zip); 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 # Bindings
Compile zip library as a dynamic library. Compile zip library as a dynamic library.
```shell ```shell