Skip to content

Commit 817fc7b

Browse files
generelized uncompression to not be limited to zip
1 parent 9a5a5f2 commit 817fc7b

File tree

4 files changed

+80
-13
lines changed

4 files changed

+80
-13
lines changed

plugins/SkyCultureMaker/CMakeLists.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ CPMAddPackage(
2121
)
2222

2323
# download https://github.com/artemvlas/qmicroz for archives
24+
#CPMAddPackage(
25+
# NAME qmicroz
26+
# GITHUB_REPOSITORY artemvlas/qmicroz
27+
# GIT_TAG v0.4
28+
#)
29+
30+
# download https://github.com/selmf/unarr for archives
2431
CPMAddPackage(
25-
NAME qmicroz
26-
GITHUB_REPOSITORY artemvlas/qmicroz
27-
GIT_TAG v0.4
32+
NAME unarr
33+
GITHUB_REPOSITORY selmf/unarr
34+
GIT_TAG v1.1.1
2835
)
2936

3037

plugins/SkyCultureMaker/src/CMakeLists.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ ENDIF()
3333

3434
ADD_LIBRARY(SkyCultureMaker-static STATIC ${SkyCultureMaker_SRCS} ${SkyCultureMaker_RES_CXX} ${SCM_UIS})
3535
SET_TARGET_PROPERTIES(SkyCultureMaker-static PROPERTIES OUTPUT_NAME "SkyCultureMaker")
36-
TARGET_LINK_LIBRARIES(SkyCultureMaker-static Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Widgets skyculture_converter_lib qmicroz)
37-
38-
target_include_directories(SkyCultureMaker-static
39-
PRIVATE
40-
${qmicroz_SOURCE_DIR}/src
41-
${qmicroz_SOURCE_DIR}/miniz
42-
)
36+
TARGET_LINK_LIBRARIES(SkyCultureMaker-static Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Widgets skyculture_converter_lib unarr)
4337

4438
SET_TARGET_PROPERTIES(SkyCultureMaker-static PROPERTIES COMPILE_FLAGS "-DQT_STATICPLUGIN")
4539
ADD_DEPENDENCIES(AllStaticPlugins SkyCultureMaker-static)

plugins/SkyCultureMaker/src/gui/ScmEditorDialog.cpp

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,71 @@
11
#include "ScmEditorDialog.hpp"
22
#include "ui_scmEditorDialog.h"
33
#include "SkyCultureConverter.hpp"
4-
#include "qmicroz.h"
4+
#include "unarr.h"
55
#include <QFileDialog>
66
#include <QFileSystemModel>
77
#include <QMessageBox>
88
#include <QMimeDatabase>
99
#include <QtConcurrent/QtConcurrent>
1010
#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+
}
1169

1270
ScmEditorDialog::ScmEditorDialog()
1371
: StelDialog("ScmEditorDialog")
@@ -160,7 +218,15 @@ void ScmEditorDialog::createDialogContent()
160218

161219
try
162220
{
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+
164230
qDebug() << "Archive extracted to:" << tempDir;
165231
}
166232
catch (const std::exception &e)

plugins/SkyCultureMaker/src/gui/scmEditorDialog.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@
248248
<item>
249249
<widget class="QLabel" name="label">
250250
<property name="text">
251-
<string>Select an archive (.zip, .rar, ...) with an old format to convert to the new format</string>
251+
<string>Select an archive (.zip, .rar, .z7 or tar) with an old format to convert to the new format</string>
252252
</property>
253253
<property name="wordWrap">
254254
<bool>true</bool>

0 commit comments

Comments
 (0)