|
1 | 1 | #include "ScmEditorDialog.hpp" |
2 | 2 | #include "ui_scmEditorDialog.h" |
3 | 3 | #include "SkyCultureConverter.hpp" |
4 | | -#include "qmicroz.h" |
| 4 | +#include "unarr.h" |
5 | 5 | #include <QFileDialog> |
6 | 6 | #include <QFileSystemModel> |
7 | 7 | #include <QMessageBox> |
8 | 8 | #include <QMimeDatabase> |
9 | 9 | #include <QtConcurrent/QtConcurrent> |
10 | 10 | #include <QFutureWatcher> |
| 11 | +#include <QFile> |
| 12 | +#include <QFileInfo> |
| 13 | +#include <QDir> |
| 14 | + |
| 15 | +ar_archive *ar_open_any_archive(ar_stream *stream, const char *fileext) |
| 16 | +{ |
| 17 | + ar_archive *ar = ar_open_rar_archive(stream); |
| 18 | + if (!ar) |
| 19 | + ar = ar_open_zip_archive(stream, |
| 20 | + fileext && (strcmp(fileext, ".xps") == 0 || strcmp(fileext, ".epub") == 0)); |
| 21 | + if (!ar) |
| 22 | + ar = ar_open_7z_archive(stream); |
| 23 | + if (!ar) |
| 24 | + ar = ar_open_tar_archive(stream); |
| 25 | + return ar; |
| 26 | +} |
| 27 | + |
| 28 | +QString extractArchive(const QString &archivePath, const QString &destinationPath) |
| 29 | +{ |
| 30 | + ar_stream *stream = ar_open_file(archivePath.toUtf8().constData()); |
| 31 | + if (!stream) |
| 32 | + { |
| 33 | + return QString("Failed to open archive: %1").arg(archivePath); |
| 34 | + } |
| 35 | + ar_archive *archive = ar_open_any_archive(stream, QFileInfo(archivePath).suffix().toUtf8().constData()); |
| 36 | + if (!archive) |
| 37 | + { |
| 38 | + ar_close(stream); |
| 39 | + return QString("Failed to open archive: %1").arg(archivePath); |
| 40 | + } |
| 41 | + |
| 42 | + // iterate entries and decompress each |
| 43 | + while (ar_parse_entry(archive)) |
| 44 | + { |
| 45 | + QString name = QString::fromUtf8(ar_entry_get_name(archive)); |
| 46 | + QString outPath = destinationPath + "/" + name; |
| 47 | + QDir().mkpath(QFileInfo(outPath).path()); |
| 48 | + QFile outFile(outPath); |
| 49 | + if (outFile.open(QIODevice::WriteOnly)) |
| 50 | + { |
| 51 | + qint64 remaining = ar_entry_get_size(archive); |
| 52 | + const qint64 bufSize = 8192; |
| 53 | + while (remaining > 0) |
| 54 | + { |
| 55 | + qint64 chunk = qMin<qint64>(remaining, bufSize); |
| 56 | + QByteArray buffer(chunk, 0); |
| 57 | + if (!ar_entry_uncompress( |
| 58 | + archive, reinterpret_cast<unsigned char *>(buffer.data()), chunk)) |
| 59 | + break; |
| 60 | + outFile.write(buffer); |
| 61 | + remaining -= chunk; |
| 62 | + } |
| 63 | + outFile.close(); |
| 64 | + } |
| 65 | + } |
| 66 | + ar_close_archive(archive); |
| 67 | + ar_close(stream); |
| 68 | +} |
11 | 69 |
|
12 | 70 | ScmEditorDialog::ScmEditorDialog() |
13 | 71 | : StelDialog("ScmEditorDialog") |
@@ -160,7 +218,15 @@ void ScmEditorDialog::createDialogContent() |
160 | 218 |
|
161 | 219 | try |
162 | 220 | { |
163 | | - QMicroz::extract(path, tempDir); |
| 221 | + // Extract the archive to the temporary directory |
| 222 | + qDebug() << "Extracting archive:" << path << "to" << tempDir; |
| 223 | + |
| 224 | + QString error = extractArchive(path, tempDir); |
| 225 | + if (!error.isEmpty()) |
| 226 | + { |
| 227 | + return error; |
| 228 | + } |
| 229 | + |
164 | 230 | qDebug() << "Archive extracted to:" << tempDir; |
165 | 231 | } |
166 | 232 | catch (const std::exception &e) |
|
0 commit comments