This repository was archived by the owner on Oct 8, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathZipEntry.cpp
More file actions
134 lines (110 loc) · 3.1 KB
/
Copy pathZipEntry.cpp
File metadata and controls
134 lines (110 loc) · 3.1 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// Created by noom on 10/29/15.
//
#include <algorithm>
#include <iostream>
#include <map>
#include <wx/file.h>
#include <wx/mstream.h>
#include "ZipEntry.h"
ZipEntry::ZipEntry(zip_t *zipFile, wxMutex *mutex, const wxString &innerPath)
: zipFile(zipFile), mutex(mutex), innerPath(innerPath) {
if (innerPath == "") {
SetIsDirectory(true);
SetName("/");
return;
}
wxString name = innerPath;
if (name.EndsWith("/")) {
name = innerPath.BeforeLast('/');
SetIsDirectory(true);
} else {
SetIsDirectory(false);
}
if (name.Contains("/"))
name = name.AfterLast('/');
SetName(name);
}
wxInputStream *ZipEntry::GetInputStream() {
mutex->Lock();
if (IsDirectory() || !zipFile)
return nullptr;
if (!buffer) {
struct zip_stat stat;
zip_stat(zipFile, innerPath, 0, &stat);
auto file = zip_fopen(zipFile, innerPath, 0);
size = stat.size;
buffer = new unsigned char[size];
auto read = zip_fread(file, buffer, size);
zip_fclose(file);
}
auto output = new wxMemoryInputStream(buffer, size);
mutex->Unlock();
return output;
}
ZipEntry::~ZipEntry() {
if (IsRoot()) {
mutex->Lock();
zip_close(zipFile);
zipFile = nullptr;
mutex->Unlock();
delete mutex;
}
delete[] buffer;
}
ZipEntry *ZipEntry::Create(const wxFileName &filename,
std::function<bool()> updateFnc) {
wxFile file(filename.GetFullPath());
int error;
auto zipFile = zip_fdopen(file.fd(), ZIP_RDONLY, &error);
file.Detach();
if (zipFile == nullptr) {
throw error;
}
auto mutex = new wxMutex();
ZipEntry *root = new ZipEntry(zipFile, mutex, "");
std::vector<wxString> innerPaths;
int num_entries = zip_get_num_entries(zipFile, ZIP_FL_UNCHANGED);
for (int i = 0; i < num_entries; i++) {
auto path = wxString(zip_get_name(zipFile, i, ZIP_FL_UNCHANGED));
innerPaths.push_back(path);
}
std::sort(innerPaths.begin(), innerPaths.end());
std::map<wxString, ZipEntry *> entryMap;
entryMap[""] = root;
bool success = true;
for (auto &path : innerPaths) {
success = updateFnc();
if (!success)
break;
AddChildrenFromPath(zipFile, mutex, entryMap, path);
}
if (!success) {
delete root;
return nullptr;
}
root->SortChildren();
return root;
}
ZipEntry *
ZipEntry::AddChildrenFromPath(zip_t *zipFile, wxMutex *mutex,
std::map<wxString, ZipEntry *> &entryMap,
const wxString &innerPath) {
auto iter = entryMap.find(innerPath);
if (iter != entryMap.end())
return iter->second;
auto path = innerPath;
bool dir = false;
if (path.EndsWith("/")) {
dir = true;
path = path.BeforeLast('/');
}
auto parentPath = path.BeforeLast('/');
if (parentPath != "")
parentPath += "/";
auto parent = AddChildrenFromPath(zipFile, mutex, entryMap, parentPath);
auto child = new ZipEntry(zipFile, mutex, innerPath);
parent->AddChild(child);
entryMap[innerPath] = child;
return child;
}