mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-08-09 11:06:01 +08:00
feat: Use zip instead of 7zip
This commit is contained in:
@@ -173,7 +173,7 @@ namespace swift::gui::components
|
||||
|
||||
// if possible we will unzip
|
||||
QStringList stdOutAndError;
|
||||
if (CCompressUtils::zip7Uncompress(destFile.absoluteFilePath(), xSwiftBusDirectory, &stdOutAndError))
|
||||
if (CCompressUtils::zipUncompress(destFile.absoluteFilePath(), xSwiftBusDirectory, &stdOutAndError))
|
||||
{
|
||||
// capture values by copy!
|
||||
const CStatusMessage msg =
|
||||
|
||||
@@ -26,86 +26,54 @@ namespace swift::misc
|
||||
return lengthHeader;
|
||||
}
|
||||
|
||||
//! Returns the platform specific 7za command
|
||||
QString getZip7Executable()
|
||||
{
|
||||
QString executable;
|
||||
if (CBuildConfig::isRunningOnMacOSPlatform())
|
||||
{
|
||||
executable += CSwiftDirectories::binDirectory();
|
||||
executable += '/';
|
||||
}
|
||||
executable += QStringLiteral("7za");
|
||||
return executable;
|
||||
}
|
||||
|
||||
bool CCompressUtils::zip7Uncompress(const QString &file, const QString &directory, QStringList *stdOutAndError)
|
||||
bool CCompressUtils::zipUncompress(const QString &file, const QString &directory, QStringList *stdOutAndError)
|
||||
{
|
||||
const QFileInfo fi(file);
|
||||
if (!fi.exists()) { return false; }
|
||||
if (!CCompressUtils::hasZip7(stdOutAndError)) { return false; }
|
||||
if (fi.suffix() != "zip")
|
||||
{
|
||||
if (stdOutAndError) { stdOutAndError->push_back("Not a zip file"); }
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool win = CBuildConfig::isRunningOnWindowsNtPlatform();
|
||||
const QString d = directory.isEmpty() ? directory : win ? CFileUtils::toWindowsLocalPath(directory) : directory;
|
||||
const QString f = win ? CFileUtils::toWindowsLocalPath(file) : file;
|
||||
|
||||
// 7za.exe x -o"P:\Temp\XPlane" c:\Users\Foo\Downloads\xswiftbus-allos-0.8.4.802111947.7z
|
||||
|
||||
QStringList args;
|
||||
args << "x";
|
||||
args << "-aoa";
|
||||
if (!d.isEmpty()) { args << "-o" + d; }
|
||||
args << f;
|
||||
|
||||
QProcess zipProcess;
|
||||
zipProcess.setProgram(getZip7Executable());
|
||||
zipProcess.setArguments(args);
|
||||
return runZip7Process(&zipProcess, stdOutAndError);
|
||||
}
|
||||
|
||||
bool CCompressUtils::hasZip7(QStringList *stdOutAndError)
|
||||
{
|
||||
// just display info
|
||||
if (CBuildConfig::isRunningOnLinuxPlatform()) { return CCompressUtils::whichZip7(stdOutAndError); }
|
||||
|
||||
QStringList args;
|
||||
args << "i";
|
||||
QProcess zipProcess;
|
||||
zipProcess.setProgram(getZip7Executable());
|
||||
zipProcess.setArguments(args);
|
||||
return runZip7Process(&zipProcess, stdOutAndError);
|
||||
}
|
||||
|
||||
bool CCompressUtils::whichZip7(QStringList *stdOutAndError)
|
||||
{
|
||||
const QString cmd("which 7za");
|
||||
QProcess zipProcess;
|
||||
zipProcess.start(cmd);
|
||||
if (!zipProcess.waitForStarted()) { return false; }
|
||||
if (!zipProcess.waitForFinished()) { return false; }
|
||||
|
||||
const QString pStdout = zipProcess.readAllStandardOutput();
|
||||
const QString pStderr = zipProcess.readAllStandardError();
|
||||
if (stdOutAndError)
|
||||
if constexpr (CBuildConfig::isRunningOnWindowsNtPlatform())
|
||||
{
|
||||
stdOutAndError->clear();
|
||||
stdOutAndError->push_back(pStdout);
|
||||
stdOutAndError->push_back(pStderr);
|
||||
zipProcess.setProgram("powershell");
|
||||
QStringList args;
|
||||
args << "-Command";
|
||||
args << "Expand-Archive";
|
||||
args << "-Path" << f;
|
||||
if (!d.isEmpty()) { args << "-DestinationPath" << d; }
|
||||
args << "-Force";
|
||||
zipProcess.setArguments(args);
|
||||
}
|
||||
const int r = zipProcess.exitCode();
|
||||
return r == 0 && pStdout.contains("7za", Qt::CaseInsensitive);
|
||||
else
|
||||
{
|
||||
zipProcess.setProgram("unzip");
|
||||
QStringList args;
|
||||
args << f;
|
||||
if (!d.isEmpty()) { args << "-d" << d; }
|
||||
zipProcess.setArguments(args);
|
||||
}
|
||||
return runZipProcess(&zipProcess, stdOutAndError);
|
||||
}
|
||||
|
||||
bool CCompressUtils::runZip7Process(QProcess *zipProcess, QStringList *stdOutAndError)
|
||||
bool CCompressUtils::runZipProcess(QProcess *zipProcess, QStringList *stdOutAndError)
|
||||
{
|
||||
zipProcess->start();
|
||||
|
||||
// If process does not even start, e.g. because no 7za exe found.
|
||||
// If process does not even start, e.g. because unzip program found.
|
||||
if (!zipProcess->waitForStarted())
|
||||
{
|
||||
if (stdOutAndError)
|
||||
{
|
||||
stdOutAndError->push_back("7za");
|
||||
stdOutAndError->push_back("unzip");
|
||||
stdOutAndError->push_back("Command not found");
|
||||
}
|
||||
return false;
|
||||
@@ -116,7 +84,7 @@ namespace swift::misc
|
||||
{
|
||||
if (stdOutAndError)
|
||||
{
|
||||
stdOutAndError->push_back("7za");
|
||||
stdOutAndError->push_back("unzip");
|
||||
stdOutAndError->push_back("Process did not finish.");
|
||||
}
|
||||
return false;
|
||||
@@ -131,6 +99,6 @@ namespace swift::misc
|
||||
stdOutAndError->push_back(pStderr);
|
||||
}
|
||||
|
||||
return zipProcess->exitStatus() == QProcess::NormalExit;
|
||||
return zipProcess->exitStatus() == QProcess::NormalExit && zipProcess->exitCode() == 0;
|
||||
}
|
||||
} // namespace swift::misc
|
||||
|
||||
@@ -25,21 +25,11 @@ namespace swift::misc
|
||||
//! \remark 4 bytes -> 32bit
|
||||
static QByteArray lengthHeader(qint32 size);
|
||||
|
||||
//! Unzip my using 7zip
|
||||
//! \remark relies on external 7zip command line
|
||||
static bool zip7Uncompress(const QString &file, const QString &directory,
|
||||
QStringList *stdOutAndError = nullptr);
|
||||
|
||||
//! External program existing?
|
||||
//! \remark relies on external 7zip command line
|
||||
static bool hasZip7(QStringList *stdOutAndError = nullptr);
|
||||
|
||||
//! Uses which to determine if 7Zip exists
|
||||
//! \remark for UNIX systems, using which
|
||||
static bool whichZip7(QStringList *stdOutAndError = nullptr);
|
||||
//! Unzip file
|
||||
static bool zipUncompress(const QString &file, const QString &directory, QStringList *stdOutAndError = nullptr);
|
||||
|
||||
private:
|
||||
static bool runZip7Process(QProcess *zipProcess, QStringList *stdOutAndError);
|
||||
static bool runZipProcess(QProcess *zipProcess, QStringList *stdOutAndError);
|
||||
};
|
||||
} // namespace swift::misc
|
||||
|
||||
|
||||
Reference in New Issue
Block a user