-
Notifications
You must be signed in to change notification settings - Fork 16
/
fileextract.cpp
186 lines (154 loc) · 5.95 KB
/
fileextract.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
/*
The MIT License (MIT)
Copyright (c) 2013 The ioquake Group
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <math.h>
#include <time.h>
#include <QFile>
#include <QFileInfo>
#include "minizip/unzip.h"
#include "fileextract.h"
FileExtractWorker::FileExtractWorker(const QString &archiveFilename, const QList<FileOperation> &filesToExtract) : archiveFilename(archiveFilename), filesToExtract(filesToExtract), isCancelled(false)
{
}
void FileExtractWorker::extract()
{
qsrand(time(NULL));
#ifdef Q_OS_WIN32
archiveFilename = archiveFilename.replace('/', '\\');
#endif
unzFile zipFile = unzOpen(archiveFilename.toLatin1().constData());
if (!zipFile)
{
emit errorMessage(QString("Error opening '%1' for extraction").arg(archiveFilename));
return;
}
unz_global_info gi;
if (unzGetGlobalInfo(zipFile, &gi) != UNZ_OK)
{
unzClose(zipFile);
emit errorMessage(QString("Error getting global info from archive '%1'").arg(archiveFilename));
return;
}
const int bufferSize = 32 * 1024;
char buffer[bufferSize];
for (unsigned int i = 0; i < gi.number_entry; i++)
{
unz_file_info fi;
char zipFilename[256];
if (unzGetCurrentFileInfo(zipFile, &fi, zipFilename, sizeof(zipFilename), NULL, 0, NULL, 0) != UNZ_OK)
{
unzClose(zipFile);
emit errorMessage(QString("Error getting file info from archive '%1'").arg(archiveFilename));
return;
}
// See if this is one of the files to be extracted.
int extractFileIndex = -1;
for (int j = 0; j < filesToExtract.size(); j++)
{
if (filesToExtract.at(j).source == zipFilename)
{
extractFileIndex = j;
break;
}
}
// Extract a file.
if (extractFileIndex != -1)
{
if (unzOpenCurrentFile(zipFile) != UNZ_OK)
{
unzClose(zipFile);
emit errorMessage(QString("'%1': error opening '%2'").arg(archiveFilename).arg(zipFilename));
goto cleanup;
}
// Write to the destination file if it doesn't exist, otherwise write to a uniquely named file.
QString filename(filesToExtract.at(extractFileIndex).dest);
const bool fileExists = QFile::exists(filename);
if (fileExists)
{
filename = FileUtils::uniqueFilename(filesToExtract.at(extractFileIndex).dest);
}
QFile file(filename);
if (!file.open(QIODevice::WriteOnly))
{
unzCloseCurrentFile(zipFile);
unzClose(zipFile);
emit errorMessage(QString("'%1': %2").arg(file.fileName()).arg(file.errorString()));
goto cleanup;
}
// Don't add a rename operation if the destination file doesn't exist - not trying to overwrite, don't need to rename anything to complete the transaction.
if (fileExists)
{
FileOperation fo;
fo.source = filename;
fo.dest = filesToExtract.at(extractFileIndex).dest;
renameOperations.append(fo);
}
emit fileChanged(QFileInfo(file.fileName()).fileName());
qint64 totalBytesRead = 0;
for (;;)
{
const int bytesRead = unzReadCurrentFile(zipFile, buffer, bufferSize);
if (bytesRead < 0)
{
unzCloseCurrentFile(zipFile);
unzClose(zipFile);
emit errorMessage(QString("'%1': error %2 extracting '%3'").arg(archiveFilename).arg(bytesRead).arg(zipFilename));
goto cleanup;
}
else if (bytesRead == 0)
{
break;
}
else
{
file.write(buffer, bytesRead);
totalBytesRead += bytesRead;
emit progressChanged(totalBytesRead, fi.uncompressed_size);
}
QMutexLocker locker(&cancelMutex);
if (isCancelled)
{
unzCloseCurrentFile(zipFile);
unzClose(zipFile);
goto cleanup;
}
}
unzCloseCurrentFile(zipFile);
}
if (i + 1 < gi.number_entry && unzGoToNextFile(zipFile) != UNZ_OK)
{
emit errorMessage(QString("'%1': error getting next file'").arg(archiveFilename));
unzClose(zipFile);
goto cleanup;
}
}
unzClose(zipFile);
emit finished(renameOperations);
return;
// Delete all the destination files.
cleanup:
for (int i = 0; i < renameOperations.size(); i++)
{
QFile::remove(renameOperations.at(i).source);
}
}
void FileExtractWorker::cancel()
{
QMutexLocker locker(&cancelMutex);
isCancelled = true;
}