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) {
42 if (read_err != MZ_ZIP_NO_ERROR) {
43 dd <<
QString(
"Miniz found an error while reading the file. - %1, %2").
arg(
static_cast<int>(read_err)).
arg(mz_zip_get_error_string(read_err));
45 if (close_err != MZ_ZIP_NO_ERROR) {
46 dd <<
QString(
"Miniz found an error while closing file file. - %1, %2").
arg(
static_cast<int>(close_err)).
arg(mz_zip_get_error_string(close_err));
48 return Status(Status::ERROR_MINIZ_FAIL, dd);
55size_t MiniZ::istreamReadCallback(
void *pOpaque, mz_uint64 file_ofs,
void * pBuf,
size_t n)
57 std::istream *stream =
static_cast<std::istream*
>(pOpaque);
58 mz_int64 cur_ofs = stream->tellg();
59 if ((mz_int64)file_ofs < 0 || (cur_ofs != (mz_int64)file_ofs && stream->seekg((mz_int64)file_ofs, std::ios_base::beg)))
61 stream->read(
static_cast<char*
>(pBuf), n);
62 return stream->gcount();
69 dd <<
QString(
"Creating Zip %1 from folder %2").
arg(zipFilePath, srcFolderPath);
77 mz_zip_zero_struct(mz);
79 mz_bool ok = mz_zip_writer_init_file(mz, zipFilePath.
toUtf8().
data(), 0);
82 mz_zip_writer_end(mz);
88 mz_zip_error err = mz_zip_get_last_error(mz);
89 dd <<
QString(
"Miniz writer init failed: error %1, %2").
arg(
static_cast<int>(err)).
arg(mz_zip_get_error_string(err));
95 std::stringstream mimeStream(mimeData.
toStdString());
96 ok = mz_zip_writer_add_read_buf_callback(mz,
"mimetype", MiniZ::istreamReadCallback, &mimeStream, mimeData.
length(),
97 0,
"", 0, MZ_NO_COMPRESSION, 0, 0,
101 mz_zip_error err = mz_zip_get_last_error(mz);
102 dd <<
QString(
"Cannot add mimetype: error %1").
arg(
static_cast<int>(err)).
arg(mz_zip_get_error_string(err));
107 for (
const QString& filePath : fileList)
109 QString sRelativePath = filePath;
110 sRelativePath.
remove(srcFolderPath);
111 if (sRelativePath ==
"mimetype")
continue;
115 ok = mz_zip_writer_add_file(mz,
117 filePath.toUtf8().data(),
118 "", 0, MZ_BEST_SPEED);
121 mz_zip_error err = mz_zip_get_last_error(mz);
122 dd <<
QString(
"Cannot add %3: error %1, %2").
arg(
static_cast<int>(err)).
arg(mz_zip_get_error_string(err), sRelativePath);
125 ok &= mz_zip_writer_finalize_archive(mz);
128 mz_zip_error err = mz_zip_get_last_error(mz);
129 dd <<
QString(
"Miniz finalize archive failed: error %1, %2").
arg(
static_cast<int>(err)).
arg(mz_zip_get_error_string(err));
130 return Status(Status::FAIL, dd);
133 ok &= mz_zip_writer_end(mz);
135 mzScopeGuard.dismiss();
140 mz_zip_error err = mz_zip_get_last_error(mz);
141 dd <<
QString(
"Miniz writer end failed: error %1, %2").
arg(
static_cast<int>(err)).
arg(mz_zip_get_error_string(err));
142 return Status(Status::FAIL, dd);
151 dd <<
QString(
"Unzip file %1 to folder %2").
arg(zipFilePath, destPath);
155 return Status::FILE_NOT_FOUND;
159 QDir baseDir(sBaseDir);
160 if (!baseDir.exists())
162 bool ok = baseDir.mkpath(
".");
166 baseDir.makeAbsolute();
169 mz_zip_zero_struct(mz);
171 mz_bool ok = mz_zip_reader_init_file(mz, zipFilePath.
toUtf8().
data(), 0);
174 mz_zip_reader_end(mz);
179 return Status(Status::FAIL, dd);
181 int num = mz_zip_reader_get_num_files(mz);
184 OnScopeExit(
delete stat);
186 for (
int i = 0; i < num; ++i)
188 ok &= mz_zip_reader_file_stat(mz, i, stat);
190 if (stat->m_is_directory)
195 bool mkDirOK = baseDir.mkpath(sFolderPath);
198 dd <<
"Make Dir failed.";
202 for (
int i = 0; i < num; ++i)
204 ok &= mz_zip_reader_file_stat(mz, i, stat);
206 if (!stat->m_is_directory)
208 if (
QString(stat->m_filename) ==
"mimetype")
continue;
214 bool extractOK = mz_zip_reader_extract_to_file(mz, i, sFullPath.
toUtf8(), 0);
218 dd <<
"File extraction failed.";
223 ok &= mz_zip_reader_end(mz);
225 mzScopeGuard.dismiss();
232 dd <<
"Unzip error!";
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