-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathunzip.cpp
More file actions
79 lines (64 loc) · 1.84 KB
/
Copy pathunzip.cpp
File metadata and controls
79 lines (64 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "unzip.h"
#include "libzip/zip.h"
#include <assert.h>
#include <libgen.h>
#include <limits.h>
#include <string>
#include <sys/stat.h>
static void mkdir_rec(const char* dir)
{
char opath[PATH_MAX];
snprintf(opath, sizeof(opath), "%s", dir);
size_t len = strlen(opath);
if (opath[len - 1] == '/')
opath[len - 1] = 0;
for (char* p = opath + 1; *p; p++) {
if (*p == '/') {
*p = 0;
mkdir(opath, S_IRWXU);
*p = '/';
}
}
mkdir(opath, S_IRWXU);
}
int64_t unzip(const char* syncZipPath, const char* destination)
{
int err = 0;
auto z = zip_open(syncZipPath, 0, &err);
assert(z != nullptr);
zip_int64_t num = zip_get_num_entries(z, 0);
struct zip_stat sb;
struct zip_file* zf;
char buf[65536];
auto pathcopy = new char[PATH_MAX];
for (zip_int64_t i = 0; i < num; i++) {
zip_stat_index(z, i, ZIP_STAT_MTIME, &sb);
auto name = sb.name;
std::string assetFullname{ destination };
assetFullname.append("/");
assetFullname.append(name);
strcpy(pathcopy, name);
auto path = dirname(pathcopy);
std::string dirFullname(destination);
dirFullname.append("/");
dirFullname.append(path);
mkdir_rec(dirFullname.c_str());
zf = zip_fopen_index(z, i, 0);
assert(zf != nullptr);
auto fd = fopen(assetFullname.c_str(), "w");
if (fd != nullptr) {
zip_int64_t sum = 0;
while (sum != sb.size) {
zip_int64_t len = zip_fread(zf, buf, sizeof(buf));
assert(len > 0);
fwrite(buf, 1, static_cast<size_t>(len), fd);
sum += len;
}
fclose(fd);
}
zip_fclose(zf);
}
delete[] pathcopy;
zip_close(z);
return num;
}