22#include <QDirIterator>
29 OnScopeExit(
delete mz);
30 mz_zip_zero_struct(mz);
32 mz_bool readOk = mz_zip_reader_init_file(mz, utf8Bytes.
constData(), 0);
34 mz_zip_error read_err = mz_zip_get_last_error(mz);
36 mz_bool closeOk = mz_zip_reader_end(mz);
38 mz_zip_error close_err = mz_zip_get_last_error(mz);
40 if (!readOk || !closeOk) {
43 dd <<
"\n[Miniz sanity check]\n";
44 if (read_err != MZ_ZIP_NO_ERROR) {
45 dd <<
QString(
"Found an error while reading the file. Error code: %2, reason: %3").
arg(
static_cast<int>(read_err)).
arg(mz_zip_get_error_string(read_err));
47 if (close_err != MZ_ZIP_NO_ERROR) {
48 dd <<
QString(
"Found an error while closing the file. Error code: %2, reason: %3").
arg(
static_cast<int>(close_err)).
arg(mz_zip_get_error_string(close_err));
50 return Status(Status::ERROR_MINIZ_FAIL, dd);
57size_t MiniZ::istreamReadCallback(
void *pOpaque, mz_uint64 file_ofs,
void * pBuf,
size_t n)
59 std::istream *stream =
static_cast<std::istream*
>(pOpaque);
60 mz_int64 cur_ofs = stream->tellg();
61 if ((mz_int64)file_ofs < 0 || (cur_ofs != (mz_int64)file_ofs && stream->seekg((mz_int64)file_ofs, std::ios_base::beg)))
63 stream->read(
static_cast<char*
>(pBuf), n);
64 return stream->gcount();
71 dd <<
"\n[Miniz COMPRESSION diagnostics]\n";
72 dd <<
QString(
"Creating Zip %1 from folder %2").
arg(zipFilePath, srcFolderPath);
76 dd <<
"Adding / to path";
85 mz_zip_zero_struct(mz);
87 mz_bool ok = mz_zip_writer_init_file(mz, zipFilePath.
toUtf8().
data(), 0);
91 mz_zip_error err = mz_zip_get_last_error(mz);
92 dd <<
QString(
"Error: Failed to init writer. Error code: %1, reason: %2").
arg(
static_cast<int>(err)).
arg(mz_zip_get_error_string(err));
93 return Status(Status::FAIL, dd);
96 mz_zip_writer_end(mz);
102 std::stringstream mimeStream(mimeData.
toStdString());
103 ok = mz_zip_writer_add_read_buf_callback(mz,
"mimetype", MiniZ::istreamReadCallback, &mimeStream, mimeData.
length(),
104 0,
"", 0, MZ_NO_COMPRESSION, 0, 0,
108 mz_zip_error err = mz_zip_get_last_error(mz);
109 dd <<
QString(
"ERROR: Unable to add mimetype. Error code: %1, reason: %2").
arg(
static_cast<int>(err)).
arg(mz_zip_get_error_string(err));
110 return Status(Status::FAIL, dd);
115 for (
const QString& filePath : fileList)
117 QString sRelativePath = filePath;
118 sRelativePath.
remove(srcFolderPath);
119 if (sRelativePath ==
"mimetype")
continue;
123 ok = mz_zip_writer_add_file(mz,
125 filePath.toUtf8().data(),
126 "", 0, MZ_BEST_SPEED);
129 mz_zip_error err = mz_zip_get_last_error(mz);
130 dd <<
QString(
"Error: Unable to add file: %3. Error code: %1, reason: %2 - Aborting!").
arg(
static_cast<int>(err)).
arg(mz_zip_get_error_string(err), sRelativePath);
131 return Status(Status::FAIL, dd);
134 ok &= mz_zip_writer_finalize_archive(mz);
137 mz_zip_error err = mz_zip_get_last_error(mz);
138 dd <<
QString(
"Error: Failed to finalize archive. Error code %1, reason: %2").
arg(
static_cast<int>(err)).
arg(mz_zip_get_error_string(err));
139 return Status(Status::FAIL, dd);
148 dd <<
"\n[Miniz EXTRACTION diagnostics]\n";
149 dd <<
QString(
"Unzip file %1 to folder %2").
arg(zipFilePath, destPath);
153 dd <<
QString(
"Error: Zip file does not exist.");
154 return Status::FILE_NOT_FOUND;
158 QDir baseDir(sBaseDir);
159 if (!baseDir.exists())
161 bool ok = baseDir.mkpath(
".");
165 baseDir.makeAbsolute();
172 mz_zip_zero_struct(mz);
174 mz_bool ok = mz_zip_reader_init_file(mz, zipFilePath.
toUtf8().
data(), 0);
176 mz_zip_error err = mz_zip_get_last_error(mz);
177 dd <<
QString(
"Error: Failed to init reader. Error code: %1, reason: %2").
arg(
static_cast<int>(err)).
arg(mz_zip_get_error_string(err));
178 return Status(Status::FAIL, dd);
181 mz_zip_reader_end(mz);
184 int num = mz_zip_reader_get_num_files(mz);
187 OnScopeExit(
delete stat);
189 for (
int i = 0; i < num; ++i)
191 ok &= mz_zip_reader_file_stat(mz, i, stat);
193 if (stat->m_is_directory)
198 bool mkDirOK = baseDir.mkpath(sFolderPath);
201 dd <<
"Make Dir failed.";
205 for (
int i = 0; i < num; ++i)
207 ok &= mz_zip_reader_file_stat(mz, i, stat);
209 if (!stat->m_is_directory)
211 if (
QString(stat->m_filename) ==
"mimetype")
continue;
217 bool extractOK = mz_zip_reader_extract_to_file(mz, i, sFullPath.
toUtf8(), 0);
221 mz_zip_error err = mz_zip_get_last_error(mz);
222 dd <<
QString(
"WARNING: Unable to extract file. Error code: %1, reason: %2").
arg(
static_cast<int>(err)).
arg(mz_zip_get_error_string(err));
229 return Status(Status::FAIL, dd);
const char * constData() const const
std::string toStdString() const const
bool mkpath(const QString &dirPath) const const
bool exists() const const
QDir absoluteDir() const const
QString absolutePath() const const
QString & append(QChar ch)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString fromUtf8(const char *str, int size)
QString & remove(int position, int n)
QByteArray toUtf8() const const