swift
Loading...
Searching...
No Matches
compressutils.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QFileInfo>
7#include <QProcess>
8
10#include "misc/fileutils.h"
11#include "misc/stringutils.h"
13
14using namespace swift::config;
15
16namespace swift::misc
17{
19 {
20 // Length header, unsigned, big-endian, 32-bit integer
24 stream << size;
25 Q_ASSERT_X(lengthHeader.size() == 4, Q_FUNC_INFO, "Wrong header size");
26 return lengthHeader;
27 }
28
29 bool CCompressUtils::zipUncompress(const QString &file, const QString &directory, QStringList *stdOutAndError)
30 {
31 const QFileInfo fi(file);
32 if (!fi.exists())
33 {
34 return false;
35 }
36 if (fi.suffix() != "zip")
37 {
38 if (stdOutAndError)
39 {
40 stdOutAndError->push_back("Not a zip file");
41 }
42 return false;
43 }
44
46 const QString d = directory.isEmpty() ? directory : win ? CFileUtils::toWindowsLocalPath(directory) : directory;
47 const QString f = win ? CFileUtils::toWindowsLocalPath(file) : file;
48
49 QProcess zipProcess;
50
52 {
53 zipProcess.setProgram("powershell");
54 QStringList args;
55 args << "-Command";
56 args << "Expand-Archive";
57 args << "-Path" << f;
58 if (!d.isEmpty())
59 {
60 args << "-DestinationPath" << d;
61 }
62 args << "-Force";
63 zipProcess.setArguments(args);
64 }
65 else
66 {
67 zipProcess.setProgram("unzip");
68 QStringList args;
69 args << f;
70 if (!d.isEmpty())
71 {
72 args << "-d" << d;
73 }
74 zipProcess.setArguments(args);
75 }
76 return runZipProcess(&zipProcess, stdOutAndError);
77 }
78
79 bool CCompressUtils::runZipProcess(QProcess *zipProcess, QStringList *stdOutAndError)
80 {
81 zipProcess->start();
82
83 // If process does not even start, e.g. because unzip program found.
84 if (!zipProcess->waitForStarted())
85 {
86 if (stdOutAndError)
87 {
88 stdOutAndError->push_back("unzip");
89 stdOutAndError->push_back("Command not found");
90 }
91 return false;
92 }
93
94 // If process does not finish. Very unlikely.
95 if (!zipProcess->waitForFinished())
96 {
97 if (stdOutAndError)
98 {
99 stdOutAndError->push_back("unzip");
100 stdOutAndError->push_back("Process did not finish.");
101 }
102 return false;
103 }
104
105 if (stdOutAndError)
106 {
107 stdOutAndError->clear();
108 const QString pStdout = zipProcess->readAllStandardOutput();
109 const QString pStderr = zipProcess->readAllStandardError();
110 stdOutAndError->push_back(pStdout);
111 stdOutAndError->push_back(pStderr);
112 }
113
114 return zipProcess->exitStatus() == QProcess::NormalExit && zipProcess->exitCode() == 0;
115 }
116} // namespace swift::misc
static constexpr bool isRunningOnWindowsNtPlatform()
Running on Windows NT platform?
static QByteArray lengthHeader(qint32 size)
Length header.
static bool zipUncompress(const QString &file, const QString &directory, QStringList *stdOutAndError=nullptr)
Unzip file.
static QString toWindowsLocalPath(const QString &path)
To Windows path using "\" delimiter.
Free functions in swift::misc.
void setByteOrder(QDataStream::ByteOrder bo)
bool exists(const QString &path)
QString suffix() const const
void clear()
void push_back(QList< T >::parameter_type value)
int exitCode() const const
QProcess::ExitStatus exitStatus() const const
QByteArray readAllStandardError()
QByteArray readAllStandardOutput()
void setArguments(const QStringList &arguments)
void setProgram(const QString &program)
void start(QIODeviceBase::OpenMode mode)
bool waitForFinished(int msecs)
bool waitForStarted(int msecs)
bool isEmpty() const const