forked from Securepoint/openvpn-client
/
zip.cpp
244 lines (178 loc) · 5.51 KB
/
zip.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "zip.h"
#include <QtGui>
Zip::Zip()
{
}
bool Zip::extract(const QString & filePath, const QString & extDirPath, const QString & singleFileName)
{
QuaZip zip(filePath);
if (!zip.open(QuaZip::mdUnzip)) {
return false;
}
zip.setFileNameCodec("IBM866");
QuaZipFileInfo info;
QuaZipFile file(&zip);
QFile out;
QString name;
char c;
for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {
if (!zip.getCurrentFileInfo(&info)) {
return false;
}
if (!singleFileName.isEmpty())
if (!info.name.contains(singleFileName))
continue;
if (!file.open(QIODevice::ReadOnly)) {
return false;
}
name = QString("%1/%2").arg(extDirPath).arg(file.getActualFileName());
if (file.getZipError() != UNZ_OK) {
return false;
}
out.setFileName(name);
// this will fail if "name" contains subdirectories, but we don't mind that
out.open(QIODevice::WriteOnly);
// Slow like hell (on GNU/Linux at least), but it is not my fault.
// Not ZIP/UNZIP package's fault either.
// The slowest thing here is out.putChar(c).
while (file.getChar(&c)) out.putChar(c);
out.close();
if (file.getZipError() != UNZ_OK) {
return false;
}
if (!file.atEnd()) {
return false;
}
file.close();
if (file.getZipError() != UNZ_OK) {
return false;
}
}
zip.close();
if (zip.getZipError() != UNZ_OK) {
return false;
}
return true;
}
bool Zip::archive(const QString &filePath, const QDir &dir, const QString &comment)
{
QuaZip zip(filePath);
if (!zip.open(QuaZip::mdCreate)) {
return false;
}
if (!dir.exists()) {
return false;
}
QFile inFile;
QStringList sl;
recurseAddDir(dir, &sl);
QFileInfoList files;
foreach (QString fn, sl) {
files << QFileInfo(fn);
}
QuaZipFile outFile(&zip);
char c;
foreach(QFileInfo fileInfo, files) {
if (!fileInfo.isFile()) {
continue;
}
QString fileNameWithRelativePath (fileInfo.filePath().remove(0, dir.absolutePath().length() + 1));
inFile.setFileName(fileInfo.filePath());
//KeyProvider kp;
if (!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileNameWithRelativePath, fileInfo.filePath())/*, kp.GetKey().toAscii()*/)) {
return false;
}
if (!inFile.open(QIODevice::ReadOnly)) {
return false;
}
while (inFile.getChar(&c) && outFile.putChar(c));
if (outFile.getZipError() != UNZ_OK) {
return false;
}
outFile.close();
if (outFile.getZipError() != UNZ_OK) {
return false;
}
inFile.close();
}
if (!comment.isEmpty()) {
zip.setComment(comment);
}
zip.close();
if (zip.getZipError() != 0) {
return false;
}
return true;
}
bool Zip::recurseAddDir(const QDir &dir, QStringList *sl)
{
bool result (true);
Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
if (info.isDir()) {
sl->append(info.absoluteFilePath());
QDir tmpdir(info.absoluteFilePath());
result = recurseAddDir(tmpdir, sl);
} else {
sl->append(info.absoluteFilePath());
}
if (!result) {
return result;
}
}
return result;
}
bool Zip::archiveFile(const QString &FileArchPath, const QString &FileSourcePath, const bool &AddMode , const QString &comment)
{
// Keine Datei oder Pfad ohne Suffix werfen keinen Fehler
if (FileSourcePath.isEmpty() || FileSourcePath.indexOf(".") == -1) {
return true;
}
QuaZip::Mode mode;
QuaZip zip(FileArchPath);
if (AddMode) {
QFile src(FileArchPath);
if (!src.exists()) {
mode = QuaZip::mdCreate;
} else {
mode = QuaZip::mdAdd;
}
} else {
QFile src(FileArchPath);
if (src.exists()) {
src.remove();
}
mode = QuaZip::mdCreate;
}
if (!zip.open(mode)) {
return false;
}
QFile inFile;
QFileInfo fileInfo(FileSourcePath);
QuaZipFile outFile(&zip);
char c;
QString fileNameWithRelativePath = fileInfo.filePath().remove(0, fileInfo.filePath().length() - fileInfo.fileName().length());
inFile.setFileName(fileInfo.filePath());
if (!inFile.open(QIODevice::ReadOnly)) {
return false;
}
if (!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileNameWithRelativePath, fileInfo.filePath()))) {
return false;
}
while (inFile.getChar(&c) && outFile.putChar(c));
if (outFile.getZipError() != UNZ_OK) {
return false;
}
outFile.close();
if (outFile.getZipError() != UNZ_OK) {
return false;
}
inFile.close();
if (!comment.isEmpty()) {
zip.setComment(comment);
}
zip.close();
if (zip.getZipError() != 0) {
return false;
}
return true;
}