forked from Streptoco/EmbeddedProgrammingInLinux
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzip.cpp
More file actions
66 lines (56 loc) · 1.93 KB
/
zip.cpp
File metadata and controls
66 lines (56 loc) · 1.93 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
#include <dirent.h>
#include <zip.h>
#include <sys/stat.h>
#include <iostream>
#include "zip.h"
using namespace std;
void addFilesToArchive(zip_t* archive, const string& path, const string& baseDirectory) {
DIR *dir = opendir(path.c_str());
if (!dir) {
throw runtime_error("Failed to open directory!");
return;
}
string baseDirRelative = baseDirectory;
if (!baseDirectory.empty() && baseDirectory[0] == '/') {
baseDirRelative = baseDirectory.substr(1);
}
struct dirent *entry;
while ((entry = readdir(dir))) {
string filePath = path + "/" + entry->d_name;
struct stat st;
stat(filePath.c_str(), &st);
if (S_ISDIR(st.st_mode)) {
if (string(entry->d_name) == "." || string(entry->d_name) == ".." || !isValidInput(entry->d_name)) {
continue;
}
string nextPath = path + "/" + entry->d_name;
string nextBaseDirectory = baseDirRelative + "/" + entry->d_name;
addFilesToArchive(archive, nextPath, nextBaseDirectory);
} else {
string entryString(entry->d_name);
size_t dotPosition = entryString.find_last_of('.');
string result = entryString.substr(dotPosition + 1);
if (result != "arv" && result != "dpt")
{
continue;
}
string entryName = baseDirRelative + "/" + entry->d_name;
zip_source_t *source = zip_source_file(archive, filePath.c_str(), 0, 0);
if (zip_file_add(archive, entryName.c_str(), source, ZIP_FL_OVERWRITE) < 0) {
throw runtime_error("Failed to add file to zip!");
return;
}
}
}
closedir(dir);
}
bool isValidInput(const char * ICAO) {
int i = 0;
while (ICAO[i] != '\0') {
if (ICAO[i] > 'Z' || ICAO[i] < 'A') {
return false;
}
i++;
}
return true;
}