mirror of
https://github.com/QuasarApp/zip.git
synced 2025-04-30 22:34:33 +00:00
Add example how to compress a folder.
This commit is contained in:
parent
529eef88ad
commit
59b7942cd8
36
README.md
36
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user